Kaynağa Gözat

Fixed undesired restart of finished audio previews when the device orientation changes

David A. Velasco 12 yıl önce
ebeveyn
işleme
fa80179106

+ 25 - 3
src/com/owncloud/android/media/MediaService.java

@@ -35,12 +35,10 @@ import android.net.wifi.WifiManager.WifiLock;
 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;
@@ -68,6 +66,9 @@ public class MediaService extends Service implements OnCompletionListener, OnPre
     /// 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;
@@ -129,6 +130,12 @@ public class MediaService extends Service implements OnCompletionListener, OnPre
     /** 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;
 
@@ -252,6 +259,8 @@ public class MediaService extends Service implements OnCompletionListener, OnPre
         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();
         }
@@ -482,7 +491,15 @@ public class MediaService extends Service implements OnCompletionListener, OnPre
     /** 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;
     }
     
@@ -498,7 +515,12 @@ public class MediaService extends Service implements OnCompletionListener, OnPre
         if (mMediaController != null) {
             mMediaController.setEnabled(true);
         }
+        player.seekTo(mStartPosition);
         configAndStartMediaPlayer();
+        if (!mPlayOnPrepared) {
+            processPauseRequest();
+        }
+        
         if (mMediaController != null) {
             mMediaController.updatePausePlay();
         }

+ 4 - 13
src/com/owncloud/android/media/MediaServiceBinder.java

@@ -66,19 +66,16 @@ public class MediaServiceBinder extends Binder implements MediaController.MediaP
     
     @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;
     }
 
@@ -86,11 +83,9 @@ public class MediaServiceBinder extends Binder implements MediaController.MediaP
     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;
         }
     }
@@ -100,10 +95,8 @@ public class MediaServiceBinder extends Binder implements MediaController.MediaP
         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;
         }
     }
@@ -113,10 +106,8 @@ public class MediaServiceBinder extends Binder implements MediaController.MediaP
         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;
         }
     }
@@ -132,8 +123,7 @@ public class MediaServiceBinder extends Binder implements MediaController.MediaP
     @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));
     }
 
     
@@ -159,12 +149,13 @@ public class MediaServiceBinder extends Binder implements MediaController.MediaP
         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);
     }

+ 6 - 11
src/com/owncloud/android/ui/preview/PreviewMediaFragment.java

@@ -230,13 +230,15 @@ public class PreviewMediaFragment extends SherlockFragment implements
     @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());
         }
     }
     
@@ -382,8 +384,6 @@ public class PreviewMediaFragment extends SherlockFragment implements
          */
         @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())
@@ -407,6 +407,7 @@ public class PreviewMediaFragment extends SherlockFragment implements
     @Override
     public void onStop() {
         super.onStop();
+        
         if (mMediaServiceConnection != null) {
             Log.d(TAG, "Unbinding from MediaService ...");
             if (mMediaServiceBinder != null && mMediaController != null) {
@@ -418,12 +419,6 @@ public class PreviewMediaFragment extends SherlockFragment implements
         }
     }
     
-    @Override
-    public void onDestroy() {
-        super.onDestroy();
-    }
-    
-    
     @Override
     public boolean onTouch(View v, MotionEvent event) {
         if (event.getAction() == MotionEvent.ACTION_DOWN && v == mVideoPreview) {
@@ -458,10 +453,10 @@ public class PreviewMediaFragment extends SherlockFragment implements
     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();
             }

+ 38 - 2
src/com/owncloud/android/ui/preview/PreviewVideoActivity.java

@@ -85,6 +85,7 @@ public class PreviewVideoActivity extends Activity implements OnCompletionListen
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        Log.e(TAG, "ACTIVITY\t\tonCreate");
         
         setContentView(R.layout.video_layout);
     
@@ -143,6 +144,7 @@ public class PreviewVideoActivity extends Activity implements OnCompletionListen
     @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());
@@ -152,7 +154,7 @@ public class PreviewVideoActivity extends Activity implements OnCompletionListen
     
     @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());
@@ -161,6 +163,39 @@ public class PreviewVideoActivity extends Activity implements OnCompletionListen
     }
 
     
+    @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.
      * 
@@ -169,7 +204,8 @@ public class PreviewVideoActivity extends Activity implements OnCompletionListen
      * @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();