Music stops when the screen rotates using MediaPlayer in Android

I ran into a problem in Android where it seems to me that I need to save a link to a MediaPlayer object.

In my Activity I start by creating an instance of the MediaPlayer object, and I can start playing the selected music. The problem is that the screen rotates, the activity is destroyed, it is created again, and then I lose the link to MediaPlayer (since I do not save it anywhere). A new object is created, and the next time the user interacts with the phone, the music stops playing. What is the right way to hold a pen? Should I be? It is not Serializable, so I cannot put it in onSaveInstanceState(Bundle) .

Thanks for the help.

+4
source share
2 answers

I would put MediaPlayer in the service by setting the binding and controlling it from the application.

+5
source

I also had the same problem. Since I didn't need to rotate the screen, I added android:screenOrientation="portrait" to the action in the manifest file.

0
source

Source: https://habr.com/ru/post/1346926/


All Articles