ios - 当用户终止应用程序时如何停止 MPMusicPlayerController 播放?

标签 ios objective-c mpmusicplayercontroller

我的申请中有以下情况:

我的应用程序中有一个音乐系统,我使用 MPMusicPlayerController 来播放音乐;到目前为止一切正常。

我的问题:当用户开始在我的应用程序中播放音乐并在一段时间后终止时,音乐无法停止,因为我正在使用 [MPMusicPlayerController systemMusicPlayer]目的。我知道还有一个选项是applicationMusicPlayer,但是它停止在后台播放音乐,这不满足我的要求。

如何在用户终止应用程序时停止播放音乐?

我有一些代码试图在 applicationWillTerminate: 中停止它,但它只在某些情况下有效:

  1. If I press home button twice and terminate the app from the multitasking UI, then the app can stop the music player.

  2. If I press the home button once and then go to the home screen, and after that I press the home button twice and terminate the application then it can not stop my music player.

我试图在 applicationWillTerminate: 中放置一个断点,但在第二个示例(从上面)中,应用程序崩溃并且没有执行我的代码,这与第一种情况不同。

更新

而且我知道当我使用 MPMusicPlayerController 时不需要后台模式,因为它会在 native 音乐播放器中启动音乐。

如有任何帮助,我们将不胜感激。

最佳答案

不幸的是,根据您的要求,您唯一的选择是使用您自己的应用程序播放音频,例如 AVAudioPlayer 并设置音频背景模式要求。

由于当您的应用程序被操作系统或用户终止时无法收到通知,您无法绕过它,您将无法从您的进程中停止系统或应用程序播放器。

注册 NSUncaughtExceptionHandler 也不会捕获 SIGKILL 或 SIGSTOP,因此您不能依赖系统音乐播放器。

这应该不是问题,因为在您的应用中设置音频播放很容易。如果您使用系统播放器以便用户可以播放自己的音乐,您应该使用选择器并访问他们的音乐库以便他们可以选择音乐。

关于ios - 当用户终止应用程序时如何停止 MPMusicPlayerController 播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30619501/

相关文章:

iphone - 更改 MPMediaPickerController 导航栏的颜色,可能吗?

ios - swift 3 : Is there a way to cast an object to a class and protocol at the same time?

android - React Native - AccessibilityInfo.setAccessibilityFocus() 中的 reactTag 参数是什么?

objective-c - 不同颜色的多边形叠加

ios - Core-Plot:如何将自定义图像绘图符号居中

ios - MPMusicPlayerController 不会寻求给定的播放时间

cocoa-touch - iOS 应用程序启动时占用近 40mb

iphone - UIActivityIndi​​cator 无法正常工作?

ios一个接一个播放3个视频

ios - 重新排列 MediaPlayer 音乐队列 [iOS]