From: David A. Velasco Date: Thu, 14 Mar 2013 15:55:57 +0000 (+0100) Subject: Added full screen video when embedded video is touched X-Git-Tag: oc-android-1.4.3~39^2~12 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/bc5c3f5f060b93f0314dc4f03c7e69ba24a22369?ds=inline;hp=--cc Added full screen video when embedded video is touched --- bc5c3f5f060b93f0314dc4f03c7e69ba24a22369 diff --git a/src/com/owncloud/android/ui/activity/PreviewVideoActivity.java b/src/com/owncloud/android/ui/activity/PreviewVideoActivity.java deleted file mode 100644 index a3c30f6a..00000000 --- a/src/com/owncloud/android/ui/activity/PreviewVideoActivity.java +++ /dev/null @@ -1,194 +0,0 @@ -/* ownCloud Android client application - * Copyright (C) 2012-2013 ownCloud Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ - -package com.owncloud.android.ui.activity; - -import android.accounts.Account; -import android.app.Activity; -import android.app.AlertDialog; -import android.content.DialogInterface; -import android.content.Intent; -import android.media.MediaPlayer; -import android.media.MediaPlayer.OnCompletionListener; -import android.media.MediaPlayer.OnErrorListener; -import android.media.MediaPlayer.OnPreparedListener; -import android.net.Uri; -import android.os.Bundle; -import android.util.Log; -import android.view.MotionEvent; -import android.widget.MediaController; -import android.widget.VideoView; - -import com.owncloud.android.AccountUtils; -import com.owncloud.android.R; -import com.owncloud.android.datamodel.OCFile; -import com.owncloud.android.media.MediaService; - -/** - * Activity implementing a basic video player. - * - * Used as an utility to preview video files contained in an ownCloud account. - * - * Currently, it always plays in landscape mode, full screen. When the playback ends, - * the activity is finished. - * - * @author David A. Velasco - */ -public class PreviewVideoActivity extends Activity implements OnCompletionListener, OnPreparedListener, OnErrorListener { - - /** Key to receive an {@link OCFile} to play as an extra value in an {@link Intent} */ - public static final String EXTRA_FILE = "FILE"; - /** Key to receive the ownCloud {@link Account} where the file to play is saved as an extra value in an {@link Intent} */ - public static final String EXTRA_ACCOUNT = "ACCOUNT"; - - private static final String TAG = null; - - private OCFile mFile; // video file to play - private Account mAccount; // ownCloud account holding mFile - private VideoView mVideoPlayer; // view to play the file; both performs and show the playback - private MediaController mMediaController; // panel control used by the user to control the playback - - /** - * Called when the activity is first created. - * - * Searches for an {@link OCFile} and ownCloud {@link Account} holding it in the starting {@link Intent}. - * - * The {@link Account} is unnecessary if the file is downloaded; else, the {@link Account} is used to - * try to stream the remote file - TODO get the streaming works - * - * {@inheritDoc} - */ - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - setContentView(R.layout.video_layout); - - mFile = getIntent().getExtras().getParcelable(EXTRA_FILE); - mAccount = getIntent().getExtras().getParcelable(EXTRA_ACCOUNT); - - mVideoPlayer = (VideoView) findViewById(R.id.videoPlayer); - - // set listeners to get more contol on the playback - mVideoPlayer.setOnPreparedListener(this); - mVideoPlayer.setOnCompletionListener(this); - mVideoPlayer.setOnErrorListener(this); - - // keep the screen on while the playback is performed (prevents screen off by battery save) - mVideoPlayer.setKeepScreenOn(true); - - if (mFile != null) { - if (mFile.isDown()) { - mVideoPlayer.setVideoPath(mFile.getStoragePath()); - - } else if (mAccount != null) { - String url = AccountUtils.constructFullURLForAccount(this, mAccount) + mFile.getRemotePath(); - mVideoPlayer.setVideoURI(Uri.parse(url)); - - } else { - onError(null, MediaService.OC_MEDIA_ERROR, R.string.media_err_no_account); - } - - // create and prepare control panel for the user - mMediaController = new MediaController(this); - mMediaController.setMediaPlayer(mVideoPlayer); - mMediaController.setAnchorView(mVideoPlayer); - mVideoPlayer.setMediaController(mMediaController); - - } else { - onError(null, MediaService.OC_MEDIA_ERROR, R.string.media_err_nothing_to_play); - } - } - - - /** - * Called when the file is ready to be played. - * - * Just starts the playback. - * - * @param mp {@link MediaPlayer} instance performing the playback. - */ - @Override - public void onPrepared(MediaPlayer vp) { - mVideoPlayer.start(); - mMediaController.show(5000); - } - - - /** - * Called when the file is finished playing. - * - * Finishes the activity. - * - * @param mp {@link MediaPlayer} instance performing the playback. - */ - @Override - public void onCompletion(MediaPlayer mp) { - this.finish(); - } - - - /** - * Called when an error in playback occurs. - * - * @param mp {@link MediaPlayer} instance performing the playback. - * @param what Type of error - * @param extra Extra code specific to the error - */ - @Override - public boolean onError(MediaPlayer mp, int what, int extra) { - Log.e(TAG, "Error in video playback, what = " + what + ", extra = " + extra); - - if (mMediaController != null) { - mMediaController.hide(); - } - - if (mVideoPlayer.getWindowToken() != null) { - String message = MediaService.getMessageForMediaError(this, what, extra); - new AlertDialog.Builder(this) - .setMessage(message) - .setPositiveButton(android.R.string.VideoView_error_button, - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { - PreviewVideoActivity.this.onCompletion(null); - } - }) - .setCancelable(false) - .show(); - } - return true; - } - - - /** - * Screen touches trigger the appearance of the control panel for a limited time. - * - * {@inheritDoc} - */ - @Override - public boolean onTouchEvent (MotionEvent ev){ - if (ev.getAction() == MotionEvent.ACTION_DOWN) { - mMediaController.show(MediaService.MEDIA_CONTROL_SHORT_LIFE); - return true; - } else { - return false; - } - } - - -} \ No newline at end of file diff --git a/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java b/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java index fa173bd5..6dd58eab 100644 --- a/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java @@ -167,8 +167,8 @@ public class PreviewMediaFragment extends SherlockFragment implements mView = inflater.inflate(R.layout.file_preview, container, false); mImagePreview = (ImageView)mView.findViewById(R.id.image_preview); - mImagePreview.setOnTouchListener(this); mVideoPreview = (VideoView)mView.findViewById(R.id.video_preview); + mVideoPreview.setOnTouchListener(this); mMediaController = (MediaControlView)mView.findViewById(R.id.media_controller); @@ -368,7 +368,8 @@ public class PreviewMediaFragment extends SherlockFragment implements */ @Override public void onCompletion(MediaPlayer mp) { - // nothing, right now + mVideoPreview.seekTo(0); + mMediaController.updatePausePlay(); } @@ -404,18 +405,6 @@ public class PreviewMediaFragment extends SherlockFragment implements @Override - public void onResume() { - super.onResume(); - } - - - @Override - public void onPause() { - super.onPause(); - } - - - @Override public void onStop() { super.onStop(); if (mMediaServiceConnection != null) { @@ -437,31 +426,34 @@ public class PreviewMediaFragment extends SherlockFragment implements @Override public boolean onTouch(View v, MotionEvent event) { - if (event.getAction() == MotionEvent.ACTION_DOWN) { - if (v == mImagePreview && - mMediaServiceBinder != null && mFile.isAudio() && mMediaServiceBinder.isPlaying(mFile)) { - toggleMediaController(MediaService.MEDIA_CONTROL_PERMANENT); - return true; - - } else if (v == mVideoPreview) { - toggleMediaController(MediaService.MEDIA_CONTROL_SHORT_LIFE); - return true; - } + if (event.getAction() == MotionEvent.ACTION_DOWN && v == mVideoPreview) { + startFullScreenVideo(); + return true; } return false; } - private void toggleMediaController(int time) { - /* - if (mMediaController.isShowing()) { - mMediaController.hide(); - } else { - mMediaController.show(time); - } - */ + private void startFullScreenVideo() { + Intent i = new Intent(getActivity(), PreviewVideoActivity.class); + i.putExtra(PreviewVideoActivity.EXTRA_ACCOUNT, mAccount); + i.putExtra(PreviewVideoActivity.EXTRA_FILE, mFile); + i.putExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, mVideoPreview.isPlaying()); + mVideoPreview.pause(); + i.putExtra(PreviewVideoActivity.EXTRA_START_POSITION, mVideoPreview.getCurrentPosition()); + startActivityForResult(i, 0); } + + @Override + public void onActivityResult (int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (resultCode == Activity.RESULT_OK) { + mSavedPlaybackPosition = data.getExtras().getInt(PreviewVideoActivity.EXTRA_START_POSITION); + mAutoplay = data.getExtras().getBoolean(PreviewVideoActivity.EXTRA_AUTOPLAY); + } + } + private void playAudio() { if (!mMediaServiceBinder.isPlaying(mFile)) { diff --git a/src/com/owncloud/android/ui/preview/PreviewVideoActivity.java b/src/com/owncloud/android/ui/preview/PreviewVideoActivity.java new file mode 100644 index 00000000..0b12920b --- /dev/null +++ b/src/com/owncloud/android/ui/preview/PreviewVideoActivity.java @@ -0,0 +1,220 @@ +/* ownCloud Android client application + * Copyright (C) 2012-2013 ownCloud Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package com.owncloud.android.ui.preview; + +import android.accounts.Account; +import android.app.Activity; +import android.app.AlertDialog; +import android.content.DialogInterface; +import android.content.Intent; +import android.media.MediaPlayer; +import android.media.MediaPlayer.OnCompletionListener; +import android.media.MediaPlayer.OnErrorListener; +import android.media.MediaPlayer.OnPreparedListener; +import android.net.Uri; +import android.os.Bundle; +import android.util.Log; +import android.view.MotionEvent; +import android.widget.MediaController; +import android.widget.VideoView; + +import com.owncloud.android.AccountUtils; +import com.owncloud.android.R; +import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.media.MediaService; + +/** + * Activity implementing a basic video player. + * + * Used as an utility to preview video files contained in an ownCloud account. + * + * Currently, it always plays in landscape mode, full screen. When the playback ends, + * the activity is finished. + * + * @author David A. Velasco + */ +public class PreviewVideoActivity extends Activity implements OnCompletionListener, OnPreparedListener, OnErrorListener { + + /** Key to receive an {@link OCFile} to play as an extra value in an {@link Intent} */ + public static final String EXTRA_FILE = "FILE"; + + /** Key to receive the ownCloud {@link Account} where the file to play is saved as an extra value in an {@link Intent} */ + public static final String EXTRA_ACCOUNT = "ACCOUNT"; + + /** Key to receive a flag signaling if the video should be started immediately */ + public static final String EXTRA_AUTOPLAY = "AUTOPLAY"; + + /** Key to receive the position of the playback where the video should be put at start */ + public static final String EXTRA_START_POSITION = "START_POSITION"; + + private static final String TAG = PreviewVideoActivity.class.getSimpleName(); + + private OCFile mFile; // video file to play + private Account mAccount; // ownCloud account holding mFile + private int mSavedPlaybackPosition; // in the unit time handled by MediaPlayer.getCurrentPosition() + private boolean mAutoplay; // when 'true', the playback starts immediately with the activity + private VideoView mVideoPlayer; // view to play the file; both performs and show the playback + private MediaController mMediaController; // panel control used by the user to control the playback + + /** + * Called when the activity is first created. + * + * Searches for an {@link OCFile} and ownCloud {@link Account} holding it in the starting {@link Intent}. + * + * The {@link Account} is unnecessary if the file is downloaded; else, the {@link Account} is used to + * try to stream the remote file - TODO get the streaming works + * + * {@inheritDoc} + */ + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.video_layout); + + Bundle extras = getIntent().getExtras(); + mFile = extras.getParcelable(EXTRA_FILE); + mAccount = extras.getParcelable(EXTRA_ACCOUNT); + mSavedPlaybackPosition = extras.getInt(EXTRA_START_POSITION); + mAutoplay = extras.getBoolean(EXTRA_AUTOPLAY); + + mVideoPlayer = (VideoView) findViewById(R.id.videoPlayer); + + // set listeners to get more contol on the playback + mVideoPlayer.setOnPreparedListener(this); + mVideoPlayer.setOnCompletionListener(this); + mVideoPlayer.setOnErrorListener(this); + + // keep the screen on while the playback is performed (prevents screen off by battery save) + mVideoPlayer.setKeepScreenOn(true); + + if (mFile != null) { + if (mFile.isDown()) { + mVideoPlayer.setVideoPath(mFile.getStoragePath()); + + } else if (mAccount != null) { + // not working now + String url = AccountUtils.constructFullURLForAccount(this, mAccount) + mFile.getRemotePath(); + mVideoPlayer.setVideoURI(Uri.parse(url)); + + } else { + onError(null, MediaService.OC_MEDIA_ERROR, R.string.media_err_no_account); + } + + // create and prepare control panel for the user + mMediaController = new MediaController(this); + mMediaController.setMediaPlayer(mVideoPlayer); + mMediaController.setAnchorView(mVideoPlayer); + mVideoPlayer.setMediaController(mMediaController); + + } else { + onError(null, MediaService.OC_MEDIA_ERROR, R.string.media_err_nothing_to_play); + } + } + + + @Override + public void onBackPressed() { + Intent i = new Intent(); + i.putExtra(EXTRA_AUTOPLAY, mVideoPlayer.isPlaying()); + i.putExtra(EXTRA_START_POSITION, mVideoPlayer.getCurrentPosition()); + setResult(RESULT_OK, i); + super.onBackPressed(); + } + + + /** + * Called when the file is ready to be played. + * + * Just starts the playback. + * + * @param mp {@link MediaPlayer} instance performing the playback. + */ + @Override + public void onPrepared(MediaPlayer vp) { + mVideoPlayer.seekTo(mSavedPlaybackPosition); + if (mAutoplay) { + mVideoPlayer.start(); + } + mMediaController.show(5000); + } + + + /** + * Called when the file is finished playing. + * + * Rewinds the video + * + * @param mp {@link MediaPlayer} instance performing the playback. + */ + @Override + public void onCompletion(MediaPlayer mp) { + mVideoPlayer.seekTo(0); + } + + + /** + * Called when an error in playback occurs. + * + * @param mp {@link MediaPlayer} instance performing the playback. + * @param what Type of error + * @param extra Extra code specific to the error + */ + @Override + public boolean onError(MediaPlayer mp, int what, int extra) { + Log.e(TAG, "Error in video playback, what = " + what + ", extra = " + extra); + + if (mMediaController != null) { + mMediaController.hide(); + } + + if (mVideoPlayer.getWindowToken() != null) { + String message = MediaService.getMessageForMediaError(this, what, extra); + new AlertDialog.Builder(this) + .setMessage(message) + .setPositiveButton(android.R.string.VideoView_error_button, + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + PreviewVideoActivity.this.onCompletion(null); + } + }) + .setCancelable(false) + .show(); + } + return true; + } + + + /** + * Screen touches trigger the appearance of the control panel for a limited time. + * + * {@inheritDoc} + */ + @Override + public boolean onTouchEvent (MotionEvent ev){ + if (ev.getAction() == MotionEvent.ACTION_DOWN) { + mMediaController.show(MediaService.MEDIA_CONTROL_SHORT_LIFE); + return true; + } else { + return false; + } + } + + +} \ No newline at end of file