Losing 'MediaPlayer' (& other Variables) when Device is Rotated

后端 未结 4 1482
独厮守ぢ
独厮守ぢ 2021-01-07 13:06

I\'m creating a music player for Android and it\'s mostly working. The problem is that when I turn the device horizontally I lose all the variables from the Activity (which

相关标签:
4条回答
  • 2021-01-07 13:20

    You should use a Service to Provides "background" audio playback capabilities, allowing the user to switch between activities or Rotate device without stopping playback.

    Check out android_packages_apps_Music which is opensource by CM on github , It use MediaPlaybackService extends Service to do this , checkout MediaPlaybackService.java

    0 讨论(0)
  • 2021-01-07 13:20

    Another way would be to :

    In your AndroidManifest.xml, find your entry for your activity and add the following attribute and value:

    android:configChanges="orientation|screenSize"
    

    This will stop your activity from being destroyed and recreated on orientation.

    0 讨论(0)
  • 2021-01-07 13:30

    Both of the methods below would allow you to keep your mediaplayer object through the rotation, but neither use bundles.

    • You could persist your media player by using onRetainNonConfigurationInstance() to save the variable and getLastNonConfigurationInstance() to retrieve it after the rotation, but this method isn't necessarily the best as it is not always called

      -See this SO post for more info https://stackoverflow.com/a/3916068/655822

    • Or you could persist your media player by extending your application class and storing it in there

      below info copied from the linked SO answer for the purpose of making this answer quicker to read

      You can pass data around in a Global Singleton if it is going to be used a lot.

      public class YourApplication extends Application 
      {     
           public SomeDataClass data = new SomeDataClass();
      }
      

      Then call it in any activity by:

      YourApplication appState = ((YourApplication)this.getApplication());
      appState.data.UseAGetterOrSetterHere(); // Do whatever you need to with the data here.
      

      -See this SO post for more info on that https://stackoverflow.com/a/4208947/655822

    0 讨论(0)
  • 2021-01-07 13:38

    For objects you couldn't pass via a bundle, I would suggest you to use the simple SharedPreference to store objects. Here you have a simple implementation:

    public class Data {
         private SharedPreferences preferences; 
         private int test; 
    
         public Data (Context context)
         {          
            preferences = context.getSharedPreferences("Data", 0);
            test = preferences.getInt("test", 0);
         }
    
         public int getTest()
         {       
                 return test;
         }
    
         public void setTest(int input)
         {       
            this.test = input;
            SharedPreferences.Editor editor = preferences.edit();
            editor.putInt("Test", input);
            editor.commit();
         }    
    }
    

    You have just to initialize the variable in the onCreate():

    Data mydata = new Data(this);

    And you can use set/get with mydata to store/retrieve your persistent data.

    Edit: It is maybe not suitable for MediaPlayer objects, but for other classical types (int, string, boolean...).

    0 讨论(0)
提交回复
热议问题