Majd Rashad
Majd Rashad

Reputation: 19

AVPlayer background issue

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

Upvotes: 0

Views: 1296

Answers (1)

bobnoble
bobnoble

Reputation: 5824

You also need to include the following in your -info.plist file:

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

See http://developer.apple.com/library/ios/#qa/qa1668/_index.html

Upvotes: 3

Related Questions