Android:在活动屏幕方向更新期间保持 MediaPlayer 运行

发布于 2024-12-13 21:29:43 字数 595 浏览 4 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无法言说的痛 2024-12-20 21:29:43

您是否看过使用 onConfigurationChanged () 回调来处理一些这样的逻辑?

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

帅哥哥的热头脑 2024-12-20 21:29:43

关于您的问题如何可靠地确定您的 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.

青萝楚歌 2024-12-20 21:29:43

尝试在不同的线程中运行MediaPlayer

您可以向该线程添加一个更复杂的 API,您可以从 onCreate/onStop/on* 调用该 API

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*

素手挽清风 2024-12-20 21:29:43

如果您正在使用片段(您应该: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.

ぃ双果 2024-12-20 21:29:43

请参阅以下答案: 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文