0

I have a problem with AVPlayer, when I tried to make the player working on the background, the iPhone 6.0 simulator works fine, but the sound on the real device disappears when the application goes to the background!

- (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;
}
4

1 回答 1

3

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

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

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

于 2013-02-10T13:54:44.123 回答