IOS:播放声音,而应用在后台(IOS: Play sound while app in backg

2019-09-01 04:36发布

我工作的一个小应用程序,我想在手机上播放从我的位置控制器事件的​​声音文件。 问题是,当应用程序在后台模式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?

希望有人能提供帮助。

/拉塞

Answer 1:

除了具有背景模式audio在你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);
}


Answer 2:

在后台启动播放声音之前,建立一个后台任务。 看来,iOS的拒绝,如果其余的背景时间小于10秒,在后台启动声音。



Answer 3:

在iOS上只有一些应用被允许在后台运行。 其中之一是音频播放器应用程序,但对他们来说,你需要设置“所需的背景模式”在你的Info.plist“音频”(应用程序可以播放音频)。

是的,如果你能在背景获得的位置,那么你必须设置你的plist此键。 还添加音频许可的plist中。



文章来源: IOS: Play sound while app in background