PreviewVideoActivity.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * Copyright (C) 2015 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.ui.preview;
  21. import android.content.Intent;
  22. import android.media.MediaPlayer;
  23. import android.media.MediaPlayer.OnCompletionListener;
  24. import android.media.MediaPlayer.OnErrorListener;
  25. import android.media.MediaPlayer.OnPreparedListener;
  26. import android.net.Uri;
  27. import android.os.Bundle;
  28. import com.google.android.exoplayer2.ExoPlayer;
  29. import com.google.android.exoplayer2.MediaItem;
  30. import com.google.android.exoplayer2.ui.StyledPlayerView;
  31. import com.nextcloud.client.media.ErrorFormat;
  32. import com.owncloud.android.R;
  33. import com.owncloud.android.databinding.ActivityPreviewVideoBinding;
  34. import com.owncloud.android.datamodel.OCFile;
  35. import com.owncloud.android.lib.common.utils.Log_OC;
  36. import com.owncloud.android.ui.activity.FileActivity;
  37. import com.owncloud.android.utils.MimeTypeUtil;
  38. import androidx.annotation.NonNull;
  39. import androidx.appcompat.app.AlertDialog;
  40. /**
  41. * Activity implementing a basic video player.
  42. *
  43. * Used as an utility to preview video files contained in an ownCloud account.
  44. *
  45. * Currently, it always plays in landscape mode, full screen. When the playback ends,
  46. * the activity is finished.
  47. */
  48. public class PreviewVideoActivity extends FileActivity implements OnCompletionListener, OnPreparedListener, OnErrorListener {
  49. /** Key to receive a flag signaling if the video should be started immediately */
  50. public static final String EXTRA_AUTOPLAY = "AUTOPLAY";
  51. /** Key to receive the position of the playback where the video should be put at start */
  52. public static final String EXTRA_START_POSITION = "START_POSITION";
  53. public static final String EXTRA_STREAM_URL = "STREAM_URL";
  54. private static final String TAG = PreviewVideoActivity.class.getSimpleName();
  55. private long mSavedPlaybackPosition = -1; // in the unit time handled by MediaPlayer.getCurrentPosition()
  56. private boolean mAutoplay; // when 'true', the playback starts immediately with the activity
  57. private ExoPlayer exoPlayer; // view to play the file; both performs and show the playback
  58. private Uri mStreamUri;
  59. private ActivityPreviewVideoBinding binding;
  60. @Override
  61. public void onCreate(Bundle savedInstanceState) {
  62. super.onCreate(savedInstanceState);
  63. Log_OC.v(TAG, "onCreate");
  64. binding = ActivityPreviewVideoBinding.inflate(getLayoutInflater());
  65. setContentView(binding.getRoot());
  66. Bundle extras = getIntent().getExtras();
  67. if (savedInstanceState == null && extras != null) {
  68. mSavedPlaybackPosition = extras.getLong(EXTRA_START_POSITION);
  69. mAutoplay = extras.getBoolean(EXTRA_AUTOPLAY);
  70. mStreamUri = (Uri) extras.get(EXTRA_STREAM_URL);
  71. } else if (savedInstanceState != null) {
  72. mSavedPlaybackPosition = savedInstanceState.getLong(EXTRA_START_POSITION);
  73. mAutoplay = savedInstanceState.getBoolean(EXTRA_AUTOPLAY);
  74. mStreamUri = (Uri) savedInstanceState.get(EXTRA_STREAM_URL);
  75. }
  76. StyledPlayerView playerView = findViewById(R.id.videoPlayer);
  77. exoPlayer = new ExoPlayer.Builder(this).build();
  78. playerView.setPlayer(exoPlayer);
  79. binding.getRoot().findViewById(R.id.exo_exit_fs).setOnClickListener(v -> onBackPressed());
  80. binding.getRoot().findViewById(R.id.exo_pause).setOnClickListener(v -> exoPlayer.pause());
  81. binding.getRoot().findViewById(R.id.exo_play).setOnClickListener(v -> exoPlayer.play());
  82. if (mSavedPlaybackPosition >= 0) {
  83. exoPlayer.seekTo(mSavedPlaybackPosition);
  84. }
  85. if (getSupportActionBar() != null) {
  86. getSupportActionBar().hide();
  87. }
  88. }
  89. /**
  90. * {@inheritDoc}
  91. */
  92. @Override
  93. public void onSaveInstanceState(@NonNull Bundle outState) {
  94. super.onSaveInstanceState(outState);
  95. outState.putLong(PreviewVideoActivity.EXTRA_START_POSITION, exoPlayer.getCurrentPosition());
  96. outState.putBoolean(PreviewVideoActivity.EXTRA_AUTOPLAY, exoPlayer.isPlaying());
  97. outState.putParcelable(PreviewVideoActivity.EXTRA_STREAM_URL, mStreamUri);
  98. }
  99. @Override
  100. public void onBackPressed() {
  101. Log_OC.v(TAG, "onBackPressed");
  102. Intent i = new Intent();
  103. i.putExtra(EXTRA_AUTOPLAY, exoPlayer.isPlaying());
  104. i.putExtra(EXTRA_START_POSITION, exoPlayer.getCurrentPosition());
  105. setResult(RESULT_OK, i);
  106. exoPlayer.stop();
  107. exoPlayer.release();
  108. super.onBackPressed();
  109. }
  110. /**
  111. * Called when the file is ready to be played.
  112. *
  113. * Just starts the playback.
  114. *
  115. * @param mp {@link MediaPlayer} instance performing the playback.
  116. */
  117. @Override
  118. public void onPrepared(MediaPlayer mp) {
  119. Log_OC.v(TAG, "onPrepare");
  120. exoPlayer.seekTo(mSavedPlaybackPosition);
  121. if (mAutoplay) {
  122. exoPlayer.play();
  123. }
  124. }
  125. /**
  126. * Called when the file is finished playing.
  127. *
  128. * Rewinds the video
  129. *
  130. * @param mp {@link MediaPlayer} instance performing the playback.
  131. */
  132. @Override
  133. public void onCompletion(MediaPlayer mp) {
  134. exoPlayer.seekTo(0);
  135. }
  136. /**
  137. * Called when an error in playback occurs.
  138. *
  139. * @param mp {@link MediaPlayer} instance performing the playback.
  140. * @param what Type of error
  141. * @param extra Extra code specific to the error
  142. */
  143. @Override
  144. public boolean onError(MediaPlayer mp, int what, int extra) {
  145. Log_OC.e(TAG, "Error in video playback, what = " + what + ", extra = " + extra);
  146. String message = ErrorFormat.toString(this, what, extra);
  147. new AlertDialog.Builder(this)
  148. .setMessage(message)
  149. .setPositiveButton(android.R.string.VideoView_error_button,
  150. (dialog, whichButton) -> PreviewVideoActivity.this.onCompletion(null))
  151. .setCancelable(false)
  152. .show();
  153. return true;
  154. }
  155. @Override
  156. protected void onStart() {
  157. super.onStart();
  158. if (getAccount() != null) {
  159. OCFile file = getFile();
  160. /// Validate handled file (first image to preview)
  161. if (file == null) {
  162. throw new IllegalStateException("Instanced with a NULL OCFile");
  163. }
  164. if (!MimeTypeUtil.isVideo(file)) {
  165. throw new IllegalArgumentException("Non-video file passed as argument");
  166. }
  167. file = getStorageManager().getFileById(file.getFileId());
  168. if (file != null) {
  169. if (file.isDown()) {
  170. exoPlayer.addMediaItem(MediaItem.fromUri(file.getStorageUri()));
  171. } else {
  172. exoPlayer.addMediaItem(MediaItem.fromUri(mStreamUri));
  173. }
  174. exoPlayer.prepare();
  175. exoPlayer.play();
  176. } else {
  177. finish();
  178. }
  179. } else {
  180. finish();
  181. }
  182. }
  183. @Override
  184. protected void onStop() {
  185. super.onStop();
  186. if (exoPlayer.isPlaying()) {
  187. exoPlayer.pause();
  188. }
  189. }
  190. }