【问题标题】:Starting AVAudioPlayer when application is in background当应用程序在后台时启动 AVAudioPlayer
【发布时间】:2012-01-01 17:42:58
【问题描述】:

我有一个在.plist 中配置了App plays audioApp provides Voice over IP services 选项的应用程序。

应用程序正在接收套接字事件(即使在后台),并且对于这些事件,播放声音。当应用程序在前台时,一切正常。当应用在后台时,应用被触发,播放器的播放方法被调用,但是没有声音。

我尝试了许多来自 Internet 的示例代码,但没有任何效果。我尝试在前台开始播放声音,然后在后台移动我的应用程序,它正在工作。

应用在后台时开始播放声音有什么问题?

这是我的应用在后台触发时处理事件的代码:

NSError *error;
NSURL *audioFileLocationURL = [[NSBundle mainBundle] URLForResource:@"HeadspinLong" withExtension:@"caf"];
if (audioPlayer != nil) return;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileLocationURL error:&error];
audioPlayer.delegate=self;
if (error) {
    NSLog(@"%@", [error localizedDescription]);
}
audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive: YES error: &error];
audioSession.delegate=self;
if (error) {
    NSLog(@"");
}

【问题讨论】:

标签: ios audio background avaudioplayer playback


【解决方案1】:

这似乎是一个显而易见的问题,但您实际上是在实际应用程序中对audioPlayer 对象调用play 方法吗?可能在后台事件处理程序的末尾?

[audioPlayer play];

【讨论】:

  • 我提供的代码在“初始化方法”中(只调用一次)。当“网络事件”到来时,会调用 audioPlayer 上的播放方法。
【解决方案2】:

据我了解,当应用程序处于后台时似乎无法开始播放音乐...只能在后台继续播放现有播放的音乐

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 2018-06-17
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多