如何在后台循环播放声音而不被中断?
以下代码应循环播放无声音频剪辑,以使应用程序在后台保持活动状态:
//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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案是,一旦您的应用程序进入后台,您就无法再次打开音频设备,直到您的应用程序返回前台。
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.