import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;
-import android.widget.MediaController;
import android.widget.Toast;
import java.io.IOException;
-import com.owncloud.android.AccountUtils;
import com.owncloud.android.R;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.ui.activity.FileDetailActivity;
/// Keys to add extras to the action
public static final String EXTRA_FILE = MY_PACKAGE + ".extra.FILE";
public static final String EXTRA_ACCOUNT = MY_PACKAGE + ".extra.ACCOUNT";
+ public static String EXTRA_START_POSITION = MY_PACKAGE + ".extra.START_POSITION";
+ public static final String EXTRA_PLAY_ON_LOAD = MY_PACKAGE + ".extra.PLAY_ON_LOAD";
+
/** Error code for specific messages - see regular error codes at {@link MediaPlayer} */
public static final int OC_MEDIA_ERROR = 0;
/** Account holding the file being played */
private Account mAccount;
+ /** Flag signaling if the audio should be played immediately when the file is prepared */
+ protected boolean mPlayOnPrepared;
+
+ /** Position, in miliseconds, where the audio should be started */
+ private int mStartPosition;
+
/** Interface to access the service through binding */
private IBinder mBinder;
if (mState != State.PREPARING) {
mFile = intent.getExtras().getParcelable(EXTRA_FILE);
mAccount = intent.getExtras().getParcelable(EXTRA_ACCOUNT);
+ mPlayOnPrepared = intent.getExtras().getBoolean(EXTRA_PLAY_ON_LOAD, false);
+ mStartPosition = intent.getExtras().getInt(EXTRA_START_POSITION, 0);
tryToGetAudioFocus();
playMedia();
}
/** Called when media player is done playing current song. */
public void onCompletion(MediaPlayer player) {
Toast.makeText(this, String.format(getString(R.string.media_event_done, mFile.getFileName())), Toast.LENGTH_LONG).show();
- processStopRequest(true);
+ if (mMediaController != null) {
+ // somebody is still bound to the service
+ player.seekTo(0);
+ processPauseRequest();
+ mMediaController.updatePausePlay();
+ } else {
+ // nobody is bound
+ processStopRequest(true);
+ }
return;
}
if (mMediaController != null) {
mMediaController.setEnabled(true);
}
+ player.seekTo(mStartPosition);
configAndStartMediaPlayer();
+ if (!mPlayOnPrepared) {
+ processPauseRequest();
+ }
+
if (mMediaController != null) {
mMediaController.updatePausePlay();
}
@Override
public boolean canPause() {
- //Log.e(TAG, TAG + " - canPause -> true");
return true;
}
@Override
public boolean canSeekBackward() {
- //Log.e(TAG, TAG + " - canSeekBackward -> true");
return true;
}
@Override
public boolean canSeekForward() {
- //Log.e(TAG, TAG + " - canSeekForward -> true");
return true;
}
public int getBufferPercentage() {
MediaPlayer currentPlayer = mService.getPlayer();
if (currentPlayer != null) {
- //Log.e(TAG, TAG + " - getBufferPercentage -> 100");
return 100;
// TODO update for streamed playback; add OnBufferUpdateListener in MediaService
} else {
- //Log.e(TAG, TAG + " - getBufferPercentage -> 0");
return 0;
}
}
MediaPlayer currentPlayer = mService.getPlayer();
if (currentPlayer != null) {
int pos = currentPlayer.getCurrentPosition();
- //Log.e(TAG, TAG + " - getCurrentPosition -> " + pos);
return pos;
} else {
- //Log.e(TAG, TAG + " - getCurrentPosition -> 0");
return 0;
}
}
MediaPlayer currentPlayer = mService.getPlayer();
if (currentPlayer != null) {
int dur = currentPlayer.getDuration();
- //Log.e(TAG, TAG + " - getDuration -> " + dur);
return dur;
} else {
- //Log.e(TAG, TAG + " - getDuration -> 0");
return 0;
}
}
@Override
public boolean isPlaying() {
MediaService.State currentState = mService.getState();
- //Log.e(TAG, TAG + " - isPlaying -> " + (currentState == State.PLAYING || currentState == State.PREPARING));
- return (currentState == State.PLAYING || currentState == State.PREPARING);
+ return (currentState == State.PLAYING || (currentState == State.PREPARING && mService.mPlayOnPrepared));
}
mService.processPlayRequest(); // this will finish the service if there is no file preloaded to play
}
-
- public void start(Account account, OCFile file) {
+ public void start(Account account, OCFile file, boolean playImmediately, int position) {
Log.d(TAG, "Loading and starting through binder...");
Intent i = new Intent(mService, MediaService.class);
i.putExtra(MediaService.EXTRA_ACCOUNT, account);
i.putExtra(MediaService.EXTRA_FILE, file);
+ i.putExtra(MediaService.EXTRA_PLAY_ON_LOAD, playImmediately);
+ i.putExtra(MediaService.EXTRA_START_POSITION, position);
i.setAction(MediaService.ACTION_PLAY_FILE);
mService.startService(i);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
-
outState.putParcelable(PreviewMediaFragment.EXTRA_FILE, mFile);
outState.putParcelable(PreviewMediaFragment.EXTRA_ACCOUNT, mAccount);
if (mFile.isVideo()) {
outState.putInt(PreviewMediaFragment.EXTRA_PLAY_POSITION , mVideoPreview.getCurrentPosition());
outState.putBoolean(PreviewMediaFragment.EXTRA_PLAYING , mVideoPreview.isPlaying());
+ } else {
+ outState.putInt(PreviewMediaFragment.EXTRA_PLAY_POSITION , mMediaServiceBinder.getCurrentPosition());
+ outState.putBoolean(PreviewMediaFragment.EXTRA_PLAYING , mMediaServiceBinder.isPlaying());
}
}
*/
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
- Log.e(TAG, "Error in video playback, what = " + what + ", extra = " + extra);
-
if (mVideoPreview.getWindowToken() != null) {
String message = MediaService.getMessageForMediaError(getActivity(), what, extra);
new AlertDialog.Builder(getActivity())
@Override
public void onStop() {
super.onStop();
+
if (mMediaServiceConnection != null) {
Log.d(TAG, "Unbinding from MediaService ...");
if (mMediaServiceBinder != null && mMediaController != null) {
}
@Override
- public void onDestroy() {
- super.onDestroy();
- }
-
-
- @Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN && v == mVideoPreview) {
startFullScreenVideo();
private void playAudio() {
if (!mMediaServiceBinder.isPlaying(mFile)) {
Log.d(TAG, "starting playback of " + mFile.getStoragePath());
- mMediaServiceBinder.start(mAccount, mFile);
+ mMediaServiceBinder.start(mAccount, mFile, mAutoplay, mSavedPlaybackPosition);
} else {
- if (!mMediaServiceBinder.isPlaying()) {
+ if (!mMediaServiceBinder.isPlaying() && mAutoplay) {
mMediaServiceBinder.start();
mMediaController.updatePausePlay();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
+ Log.e(TAG, "ACTIVITY\t\tonCreate");
setContentView(R.layout.video_layout);
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
+ Log.e(TAG, "ACTIVITY\t\tonSaveInstanceState");
outState.putParcelable(PreviewVideoActivity.EXTRA_FILE, mFile);
outState.putParcelable(PreviewVideoActivity.EXTRA_ACCOUNT, mAccount);
outState.putInt(PreviewVideoActivity.EXTRA_START_POSITION, mVideoPlayer.getCurrentPosition());
@Override
public void onBackPressed() {
- Log.e(TAG, "onBackPressed");
+ Log.e(TAG, "ACTIVTIY\t\tonBackPressed");
Intent i = new Intent();
i.putExtra(EXTRA_AUTOPLAY, mVideoPlayer.isPlaying());
i.putExtra(EXTRA_START_POSITION, mVideoPlayer.getCurrentPosition());
}
+ @Override
+ public void onResume() {
+ super.onResume();
+ Log.e(TAG, "ACTIVTIY\t\tonResume");
+ }
+
+
+ @Override
+ public void onStart() {
+ super.onStart();
+ Log.e(TAG, "ACTIVTIY\t\tonStart");
+ }
+
+ @Override
+ public void onDestroy() {
+ super.onDestroy();
+ Log.e(TAG, "ACTIVITY\t\tonDestroy");
+ }
+
+ @Override
+ public void onStop() {
+ super.onStop();
+ Log.e(TAG, "ACTIVTIY\t\tonStop");
+ }
+
+
+ @Override
+ public void onPause() {
+ super.onPause();
+ Log.e(TAG, "ACTIVTIY\t\tonPause");
+ }
+
+
/**
* Called when the file is ready to be played.
*
* @param mp {@link MediaPlayer} instance performing the playback.
*/
@Override
- public void onPrepared(MediaPlayer vp) {
+ public void onPrepared(MediaPlayer mp) {
+ Log.e(TAG, "ACTIVITY\t\tonPrepare");
mVideoPlayer.seekTo(mSavedPlaybackPosition);
if (mAutoplay) {
mVideoPlayer.start();