如何在后台循环播放声音而不被中断?

发布于 2024-12-23 17:55:31 字数 558 浏览 1 评论 0原文

以下代码应循环播放无声音频剪辑,以使应用程序在后台保持活动状态:

//set sessions ....
.........
//play
audio_player = [[AVAudioPlayer alloc] initWithContentsOfURL: url error:NULL];
audio_player.numberOfLoops= -1; //always repeat
bool ret = [audio_player play];

按下主页按钮,它会在后台播放,从而使应用程序保持活动状态。

但是,当您拨打电话时,播放器将停止,并且 [audio_player play] 将返回 false。

那么问题是,在后台时,如何防止AVAudioPlayer的播放被中断或允许其在中断时恢复?否则,应用程序将在最后一次播放无声音频循环 10 分钟后终止。

我知道有一个解决方案,因为有很多闹钟应用程序可以在后台运行而不受影响。我已经测试过了,它们确实在后台运行。

谁能想出一个在后台运行而不被App Store拒绝的解决方案?

The following code should play a silent audio clip on a loop to keep an application active in the background:

//set sessions ....
.........
//play
audio_player = [[AVAudioPlayer alloc] initWithContentsOfURL: url error:NULL];
audio_player.numberOfLoops= -1; //always repeat
bool ret = [audio_player play];

You press the home button and it plays in background, keeping your application active.

However, when you make a phone call, the player will stop and [audio_player play] will return false after that.

So the question is, when in the background, how do you prevent AVAudioPlayer's playback from being interrupted or allow it to resume when interrupted? Otherwise, the application will terminate 10 minutes after the silent audio loop has last played.

I know there is a solution, because there are many alarm clock apps that can run in the background without being affected. I have tested and they are really running in the background.

Can anyone come up with a solution to run in the background without being rejected by the App Store?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

子栖 2024-12-30 17:55:31

答案是,一旦您的应用程序进入后台,您就无法再次打开音频设备,直到您的应用程序返回前台。

The answer is once your app is in the background you can not open the audio device again until your app returns to the foreground.

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