Ryan
Ryan

Reputation: 3619

Music stops when screen rotates using MediaPlayer in Android

I am running into an issue in Android where I think I need to keep some sort of reference to a MediaPlayer object.

In my Activity I begin by instantiating the MediaPlayer object and I can begin playing chosen music. The problem lies when the screen rotates, the Activity gets destroyed, created again, and then I lose reference to the MediaPlayer (since nowhere do I save it). A new object gets created and the next time the user interacts with the phone, the music stops playing. What is the proper way of keeping a handle on it? Should I be? It's not Serializable so I can't put it in a onSaveInstanceState(Bundle).

Thanks for the help.

Upvotes: 4

Views: 1665

Answers (2)

Splitusa
Splitusa

Reputation: 1181

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

Upvotes: 0

Thorben
Thorben

Reputation: 6991

I would try putting the MediaPlayer in a service, setting up a bind and control it from withing your application.

Upvotes: 5

Related Questions