MPMoviePlayerController plays an audio stream in the background

I am having problems playing the audio stream when the application enters the background.

I use the code to start the stream:

NSURL *mediaURL = [NSURL URLWithString:@"http://url.to.my.stream"];

MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:mediaURL];

[[NSNotificationCenter defaultCenter] addObserver:self 

                                         selector:@selector(moviePlayBackDidFinish:) 

                                             name:MPMoviePlayerPlaybackDidFinishNotification 

                                           object:nil]; 



[mp setControlStyle:MPMovieControlStyleFullscreen];

[mp setMovieSourceType:MPMovieSourceTypeStreaming];

[mp setFullscreen:YES];



[self.view addSubview:[mp view]];



[mp prepareToPlay];

[mp play];

It works great. But despite the fact that I set the flag "App play audio" in the list of properties, when the application enters the background, the stream stops playing.

How to make the application play the audio stream in the background?

Best wishes and thanks for the help!

+5
source share
2 answers

I have not tried it myself, but it looks promising: iOS Multitasking: background audio

Once the project has been created, go to APP-Info.plist and add UIBackgroundModes as a new line. Then it should create an array.

0 .

AVAudioSession?

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive:YES error:nil];
+9

, , ( .plis), , .

#import <MediaPlayer/MPNowPlayingInfoCenter.h>
#import <MediaPlayer/MPMediaItem.h>
#import <AVFoundation/AVFoundation.h>

---- o ----

-(void) playMusic{

     [[AVAudioSession sharedInstance] setDelegate: self];

     NSError *myErr;

     // Initialize the AVAudioSession here.
    if (![[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&myErr]) {
       // Handle the error here.
       NSLog(@"Audio Session error %@, %@", myErr, [myErr userInfo]);
    }else{
       // Since there were no errors initializing the session, we'll allow       begin receiving remote control events
       [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    }

    //initialize our audio player
    audioPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@"http://www.cocoanetics.com/files/Cocoanetics_031.mp3"]];

    [audioPlayer setShouldAutoplay:NO];
    [audioPlayer setControlStyle: MPMovieControlStyleEmbedded];
    audioPlayer.view.hidden = YES;

    [audioPlayer prepareToPlay];

    [audioPlayer play];
}//end playmusic
0

All Articles