IOS:应用在后台播放声音

9

我正在开发一个小应用程序,希望当我的位置控制器触发事件时,手机可以播放声音文件。

问题是,当应用程序处于后台模式时,AVAudioPlayer不会启动音频文件,但是代码已经安排好了,并且有 NSLog 的输出。

另一件事是它在模拟器上运行正常,但在手机上无法工作。

为什么会这样呢?

以下是我播放文件的代码:

- (void)tryPlayTaleSound:(NSString*)fileName {

    NSString *backgroundMusicPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp3"];
    NSURL *backgroundMusicURL = [NSURL fileURLWithPath:backgroundMusicPath];
    NSError *error;

    backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];

    // Check to see if iPod music is already playing
    //UInt32 propertySize = sizeof(otherMusicIsPlaying);
    //AudioSessionGetProperty(kAudioSessionProperty_OtherAudioIsPlaying, &propertySize, &otherMusicIsPlaying);

    // Play the music if no other music is playing and we aren't playing already
    //if (otherMusicIsPlaying != 1 && !backgroundMusicPlaying) {
    //[backgroundMusicPlayer prepareToPlay];
    [backgroundMusicPlayer play];
    backgroundMusicPlaying = YES;
    //}
    NSLog(@"%s - ", __FUNCTION__);
}

在日志中,我得到了这个。
Apr 26 13:57:48 iPhone CommCenter[58] <Notice>: Client [com.apple.persistentconnection[dataaccessd,85]] is telling PDP context 0 to go active.
Apr 26 13:57:50 iPhone Herning100[1467] <Warning>: Playing audiofile - filename is Fil03
Apr 26 13:57:50 iPhone mediaserverd[39] <Warning>: 13:57:50.653 <AudioControl> WARNING translating CMSession error: -12985 
Apr 26 13:57:50 iPhone mediaserverd[39] <Warning>: 13:57:50.656 <AudioControl> WARNING translating CMSession error: -12985 
Apr 26 13:57:50 IPhone mediaserverd[39] <Error>: 13:57:50.734 <AudioControl> AudioQueue: Error -12985 from AudioSessionSetClientPlayState(1467)
Apr 26 13:57:50 iPhone Herning100[1467] <Warning>: -[AppDelegate tryPlayTaleSound:] - 
Apr 26 13:57:50 iPhone com.apple.debugserver-199[1465] <Warning>: 9 +28.832083 sec [05b9/0303]: error: ::mach_vm_region_recurse ( task = 0x1603, address => 0xfffffffe, size => 0, nesting_depth => 1024, info => 0x2fd7fe48, infoCnt => 12) addr = 0xfffffffe  err = (os/kern) invalid address (0x00000001)
Apr 26 13:57:51 iPhone com.apple.debugserver-199[1465] <Warning>: 10 +0.187961 sec [05b9/0303]: error: ::mach_vm_region_recurse ( task = 0x1603, address => 0xfffffffe, size => 0, nesting_depth => 1024, info => 0x2fd7fe48, infoCnt => 12) addr = 0xfffffffe  err = (os/kern) invalid address (0x00000001)

我应该使用AVAudioPlayer还是AVAudioSession,这有什么区别吗?

希望有人能够帮忙解答。

/Lasse


希望这个链接能对你有所帮助。 https://dev59.com/RVnUa4cB1Zd3GeqPa35l - rir
3个回答

12
除了在您的Info.plist中设置背景模式音频之外,您还需要将共享音频会话设置为适合您的应用程序的正确模式,并在后台播放声音之前激活它:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *error = nil;
NSLog(@"Activating audio session");
if (![audioSession setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionDuckOthers error:&error]) {
    NSLog(@"Unable to set audio session category: %@", error);
}
BOOL result = [audioSession setActive:YES error:&error];
if (!result) {
    NSLog(@"Error activating audio session: %@", error);
}
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

播放音频结束后,不要忘记停用音频会话:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *error = nil;
NSLog(@"Deactivating audio session");
BOOL result = [audioSession setActive:NO error:&error];
if (!result) {
    NSLog(@"Error deactivating audio session: %@", error);
}

我正在做和你完全相同的事情,这个解决方案完美地解决了我的问题。我还添加了一些其他代码,所以如果这对你不起作用,请告诉我,我可以分享一下我在glebd已经写的基础上所做的工作。 - Ryan Copley
谢谢,当应用程序在后台时,我需要将类别设置为“AVAudioSessionCategoryPlayback”才能播放音频。 - Niib Fouda

1
在开始播放背景音乐之前,需要设置一个后台任务。如果iOS剩余的后台时间少于10秒,似乎iOS会拒绝在后台启动音频。

0

iOS系统只允许部分应用在后台运行。其中之一是音频播放器应用,但您需要在Info.plist中设置“必需的后台模式”为“audio”(应用程序可以播放音频)。

如果您能够在后台获取位置信息,则必须在Plist中设置此键。还要在plist中添加音频权限。


谢谢,但实际上我已经在Plist文件中设置了位置和音频。如果在应用程序运行时启动音频,则在我最小化应用程序后它将继续播放。但是当此声音完成后,它永远不会在后台模式下启动新的音频。 - lskov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接