简体   繁体   中英

AVPlayer streaming audio in background

I have a problem with the AVPlayer playing in background mode, like a lot of people here and everywhere on the web. I've done what I think is supposed to work, but is still doesn't... I think my problem might be where I set and use my AudioSession and AVPlayer.

1) The "audio" key is in UIBackgroundModes of my Info.plist

2) AudioSession set like this in AppDelegate (initialised in didFinishLaunchingWithOptions):

AVAudioSession *audio = [[AVAudioSession alloc]init];
[audio setCategory:AVAudioSessionCategoryPlayback error:nil];
[audio setActive:YES error:nil];

3) I use an AVPlayer (not AVAudioPlayer) also implemented in AppDelegate. (initialised in didFinishLaunchingWithOptions, after the AudioSession), right after the AudioSession

// Load the array with the sample file
NSString *urlAddress = @"http://mystreamadress.mp3";

//Create a URL object.
self.urlStream = [NSURL URLWithString:urlAddress];  
self.player = [AVPlayer playerWithURL:urlStream];
//Starts playback
[player play];

And still, the audio is suspended everytime the app goes in background (when I press the "Home" button).

By the way, the problem was just with the simulator. Works fine on the iOS devices

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM