AVPlayer背景问题

时间:2013-02-10 13:44:58

标签: iphone ios background avplayer

我遇到AVPlayer的问题,当我尝试让播放器在后台运行时,iPhone 6.0模拟器工作正常,但当应用程序进入后台时,真实设备上的声音消失了!< / p>

- (IBAction)RayaFM { 
    NSURL *URLA = [NSURL URLWithString:@"http://live.raya.fm:8032/;stream.mp3"];
    self.player = [AVPlayer playerWithPlayerItem:[AVPlayerItem playerItemWithURL:URLA]];

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];

    UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid;
    [self.player play];
    newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];

    if (newTaskId !=  bgTaskId != UIBackgroundTaskInvalid)
        [[UIApplication sharedApplication] endBackgroundTask: bgTaskId];

    bgTaskId = newTaskId;
}

1 个答案:

答案 0 :(得分:3)

您还需要在-info.plist文件中包含以下内容:

<key>UIBackgroundModes</key>
<array>
    <string>audio</string>
</array>

请参阅http://developer.apple.com/library/ios/#qa/qa1668/_index.html