2

The songs I have are all MediaPlayers, and when I change from horizontal to vertical orientation, or vice versa, the song stops, and then begins playing from the start of the song. I suspect that when i change the layout to horizontal or to vertical that the onPause() method is called or something. What can I do to prevent the song from restarting, and to just keep playing normally when I change from vertical to horizontal and vice versa? Would this have anything to do with the fact that I implemented these methods?

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    music.get(SongPlaying).stop();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    music.get(SongPlaying)).stop();

}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    music.get(SongPlaying)).stop();

}
4

4 回答 4

4

Another way if you don't want to handle the orientation yourself. Save the mediaplayer current position in onSaveInstantState();

   @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("currPos", music.get(SongPlaying)).getCurrentPosition());
    }

then have a variable that holds the current position and in onCreate

public void onCreate(Bundle state){
         if( state != null ){
             playerCurrentPosition = state.getInt("currPos"));
         }
        //Then when you play your media use seekTo(playerCurrentPosition);
    }
于 2012-08-17T21:57:15.933 回答
2

When you switch orientations, the current activity is destroyed and created again for layout purposes. You can prevent this a few different ways, but throw android:configChanges="orientation" In your activity section of your Manifest file. It should prevent the orientation from restarting the activity

于 2012-08-17T20:40:09.143 回答
1

The Activity reloads when you change the orientation.Hence you can lock your orientation to prevent the activity from reloading.

于 2012-08-18T19:22:51.343 回答
0

Screen orientation change effectively means your current activity is restarted. Log your onCreate and onDestroy, youll see. I find this one of the rare but very unpleasant surprises with android development, youll have to save the current status of your activity and restore them manually.

于 2012-08-17T20:42:40.377 回答