如何在用户终止应用程序时停止播放MPMusicPlayerController? [英] How to stop MPMusicPlayerController from playing when application terminate by user?

查看:191
本文介绍了如何在用户终止应用程序时停止播放MPMusicPlayerController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中存在以下情况:

I have the following situation in my application:

我的应用程序中有一个音乐系统,我使用 MPMusicPlayerController 播放音乐;到目前为止,每件事情都运转良好。

I have a music system in my application and I used MPMusicPlayerController to play music; every thing is working fine until now.

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

My problem: When a user starts playing music in my application and after some time it terminates, music cannot be stopped because I'm using the [MPMusicPlayerController systemMusicPlayer] object. I know there is another option which is applicationMusicPlayer, but it stops playing music in background, which doesn't satisfy my requirements.

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

How can I stop the music from playing when the application is terminated by user?

我有一些代码尝试在 applicationWillTerminate:但它仅适用于某些情况:

I have some code that attempts to stop it in applicationWillTerminate: but it only works in some situations:



  1. 如果我按两次主页按钮并从多任务UI终止应用程序,那么该应用程序可以停止音乐播放器。

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

如果我按下主页按钮一次然后转到主屏幕,之后我按两次主页按钮并终止应用程序然后它无法停止我的音乐播放器。

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:中设置一个断点但是在第二个例子中(从上面),应用程序崩溃了没有执行我的代码,不像在t中他是第一种情况。

I tried to put a breakpoint in applicationWillTerminate: but in the second example (from above), the application crashed and did not execute my code, unlike in the first situation.

UPDAT

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

And I know that when I use MPMusicPlayerController background mode is not required because it starts music in the native music player.

任何帮助都将不胜感激。

Any help would be appreciated.

推荐答案

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

Unfortunately your only option based on your requirements is to play the audio using your own app, AVAudioPlayer for example and set the audio background mode requirement.

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

As there is no way of receiving a notification when your app is terminated by the OS or by the user, you have no way around it, you will not be able to stop the system or application player from your process.

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

Registering for NSUncaughtExceptionHandler wont catch SIGKILL or SIGSTOP either so you cannot rely on the system music player.

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

This shouldnt be a problem though as its easy to setup audio playback in your app. If you are using the system player so that the user can play their own music, you should use a picker and access their music library so that they can choose the music.

这篇关于如何在用户终止应用程序时停止播放MPMusicPlayerController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆