9

I use a MediaPlayer to play an MP3. Currently I disabled screen orientation changes by using

android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation"

in the manifest. I do want to support landscape mode now - i.e. removed those tags - but have the problem that during the destroy/create cycle the player gets stopped and then restarted. This is okay and I actually do this even manually in onPause() to stop the player when the activity goes in the background.

To keep it running during orientation changes now, I tried making it static (and using the Application Context to create it once). Of course, when I remove the player.stop() in onPause() now, it does what I want - well, until the Activity goes in the background.

So, two questions:

  • How can I determine if the Activity will be recreated directly after the call to onStop()
  • Or: How can I keep the MediaPlayer running during that cycle, yet stop it when the App goes in the background?
sunside
  • 8,069
  • 9
  • 51
  • 74

5 Answers5

3

Have you looked at using the onConfigurationChanged() callback to handle some of this logic?

Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102
  • It doesn't get called due to the removed `android:configChanges` in the manifest. – sunside Nov 04 '11 at 19:25
  • If you want to differentiate between when you just change configurations and when you actually stop, I don't know of any other way of doing it other than using onConfigurationChanged(). Which I guess means you'll have to keep the `android:configChanges="orientation"` in your manifest :\ – Kurtis Nusbaum Nov 04 '11 at 19:28
  • Ouch. Simply adding it again did it already; Seems the default implementation does indeed handle the layout changes. Thanks a million! :) – sunside Nov 04 '11 at 19:35
  • @Kurtis It's not true that you can't determine configuration changes reliably; see my answer. – class stacker Jan 25 '13 at 10:38
1

Regarding your question how you can reliably determine whether your Activity is destroyed due to a configuration change, see my answer here: How to save/restore(update) ref to the dialog during screen rotation?(I need ref in onCreate method of activity.)

With this, the answer to your second question should be easy.

Community
  • 1
  • 1
class stacker
  • 5,357
  • 2
  • 32
  • 65
1

Try running MediaPlayer in different Thread.

You can add to this thread a more complex API to which you can call from onCreate/onStop/on*

talel
  • 355
  • 2
  • 16
1

If you are using fragments (and you should :P), then calling setRetainInstance(true) inside your fragments onCreate() call will make this problem go completely go away.

Brian Attwell
  • 9,239
  • 2
  • 31
  • 26
0

Please see the following answer: https://stackoverflow.com/a/31466602/994021

It is a POC I've created for this purpose. Tired of solving this issue over and over again for different projects. I hope it helps.

I really recommend to switch to a third party player like ExoPlayer from google guys: https://github.com/google/ExoPlayer

Community
  • 1
  • 1
Gaspar de Elias
  • 244
  • 3
  • 12