Edd Turtle
Edd Turtle

Reputation: 1531

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

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 makes sense because it is destroyed and re-created).

I've tried using bundles to store the state of the player with onSaveInstanceState & onRestoreInstanceState but I still can't access the media player. Is there a way to pass objects like the MediaPlayer in bundles? Should I be using a database instead?

Thanks

Upvotes: 0

Views: 877

Answers (4)

petey
petey

Reputation: 17150

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.

Upvotes: 0

Wangchao0721
Wangchao0721

Reputation: 909

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

Upvotes: 4

jp36
jp36

Reputation: 1918

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

Upvotes: 1

Zappescu
Zappescu

Reputation: 1439

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...).

Upvotes: 1

Related Questions