Android:在活动屏幕方向更新期间保持 MediaPlayer 运行
我使用 MediaPlayer 来播放 MP3。目前,我通过
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation"
在清单中使用禁用了屏幕方向更改。我现在确实想支持横向模式 - 即删除了这些标签 - 但有一个问题,在销毁/创建周期期间,播放器停止然后重新启动。这没关系,实际上我什至在 onPause() 中手动执行此操作,以在活动进入后台时停止播放器。
现在,为了使其在方向更改期间保持运行,我尝试将其设为静态(并使用应用程序上下文创建一次)。当然,当我现在删除 onPause()
中的 player.stop()
时,它会执行我想要的操作 - 好吧,直到 Activity进入后台。
因此,有两个问题:
- 如何确定在调用
onStop()
之后是否会直接重新创建 Activity - 或者:如何在该周期内保持
MediaPlayer
运行,但当应用程序进入后台时停止它?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您是否看过使用 onConfigurationChanged () 回调来处理一些这样的逻辑?
Have you looked at using the onConfigurationChanged() callback to handle some of this logic?
关于您的问题如何可靠地确定您的 Activity 是否由于配置更改而被销毁,请参阅我的答案:如何在屏幕期间保存/恢复(更新)对话框的引用旋转?(我需要在活动的 onCreate 方法中引用。)
有了这个,你的第二个问题的答案应该很容易。
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.
尝试在不同的线程中运行
MediaPlayer
。您可以向该线程添加一个更复杂的 API,您可以从
onCreate
/onStop
/on*
调用该 APITry running
MediaPlayer
in different Thread.You can add to this thread a more complex API to which you can call from
onCreate
/onStop
/on*
如果您正在使用片段(您应该:P),那么在片段 onCreate() 调用中调用
setRetainInstance(true)
将使这个问题完全消失。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.请参阅以下答案: https://stackoverflow.com/a/31466602/994021
这是我的一个 POC为此目的而创建。厌倦了为不同的项目一遍又一遍地解决这个问题。我希望它有帮助。
我真的建议切换到第三方播放器,例如 Google 的 ExoPlayer: https://github.com/google/ Exo播放器
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