Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 918
Patrice Cote Avatar asked Mar 02 '11 02:03

Patrice Cote


People also ask

Can you listen to audible in the background?

A: You must declare your app plays audible content while in the background, assign an appropriate category to your audio session ( AVAudioSession ), and make sure your app has an active audio session before entering the background. See also Special Considerations for Video Media.

What is background audio mode?

A common capability required by playback apps is to play background audio. With this capability enabled, your app's audio can continue when users switch to another app or when they lock their iOS devices.


1 Answers

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

like image 184
Patrice Cote Avatar answered Oct 05 '22 23:10

Patrice Cote