IOS:在应用程序中播放声音 [英] IOS: Play sound while app in background

查看:152
本文介绍了IOS:在应用程序中播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个小应用程序上工作,我希望手机在我的位置控制器的事件上播放声音文件。
问题是,当应用程序处于后台模式时,AVAudioPlayer不会启动音频文件,但会调度代码,获取NSLog输出。
另一件事是它可以在模拟器中使用但不能在手机上使用。

I working on a little app where i want the phone to play a sound file on an event from my location controller. The problem is when the App is in Background mode the AVAudioPlayer not start the Audio file but the code is scheduled, getting NSLog output. Another thing is it works in the simulator but not on the phone.

不是吗?

这是我播放文件的代码:

This is my code to play the file:

- (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会有区别吗??

Is there a difference if I use the AVAudioPlayer or the AVAudioSession.?

希望有人可以提供帮助。

Hope someone can help.

/ Lasse

推荐答案

除了 Info.plist中的背景模式 audio 之外您需要将共享音频会话设置为适合您应用的模式,并在播放后台声音之前激活它:

In addition to having background mode audio in your Info.plist you need to set the shared audio session to the correct mode for your app and activate it just before playing your sound in the background:

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];

完成播放声音后别忘了停用音频会话:

Don't forget to deactivate the audio session when you are done playing sounds:

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);
}

这篇关于IOS:在应用程序中播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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