首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IOS:应用程序在后台播放声音

问IOS:应用程序在后台播放声音
EN

Stack Overflow用户
提问于 2013-04-26 16:50:17
回答 3查看 10.7K关注 0票数 9

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

为什么不呢?

这是我播放该文件的代码:

代码语言:javascript
复制
- (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__);
}

在日志中,我得到了这一点。

代码语言:javascript
复制
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

EN

回答 3

Stack Overflow用户

发布于 2014-04-19 00:31:17

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

代码语言:javascript
复制
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];

播放完声音后,不要忘了停用音频会话:

代码语言:javascript
复制
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);
}
票数 12
EN

Stack Overflow用户

发布于 2015-05-15 14:16:45

在开始在后台播放声音之前,请设置一个后台任务。如果剩余的后台时间小于10秒,iOS似乎会拒绝在后台启动声音。

票数 1
EN

Stack Overflow用户

发布于 2013-04-26 17:08:10

在iOS上,只允许部分应用程序在后台运行。其中之一是音频播放器应用程序,但对于它们,你需要在你的Info.plist中设置“必要的背景模式”与“音频”(应用程序可以播放音频)。

是的,如果你能够在后台定位,那么你必须在你的Plist中设置这个关键点。在plist中也添加音频权限。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16232315

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档