安卓:保持活动的屏幕方向升级过程中的MediaPlayer运行过程中、屏幕、方向、MediaPlayer

2023-09-12 04:23:34 作者:(放纵)

我用的MediaPlayer播放的MP3。通过使用目前我禁用屏幕方向变化

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

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

在清单中。我想现在支持横向模式 - 即删除了这些标签 - 但问题是,破坏/创建周期内的球员被停止,然后重新启动。这是正常的,我真正做到这一点,即使在手动的onPause()停止播放时,活动进入后台。

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.

要保持它在运行过程中的方向现在改变,我试图使其静态的(和使用应用程序上下文一次创建它)。当然,当我删除的onPause的 player.stop()()现在,它确实是我想要的 - 好了,,直到的活动进入后台。

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.

于是,两个问题:

如何确定活动将被调用后直接重新创建的onStop() 或者:我怎样才能在该周期保持的MediaPlayer 运行,但停止时,应用程序进入后台 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?

推荐答案

你看使用 onConfigurationChanged()回调来处理一些这种逻辑?

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