0
votes

So I have this app where I play videos and when you exit the app I would like the user to continue to listen to the audio of the video. I use MPMoviePlayerController to play the video and it works in the app perfectly fine. I also setup AVAudioSession up before I play the video and I get no error.

NSError *audioSessionError;
NSError *activationError;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&audioSessionError];
[audioSession setActive:YES error:&activationError];

I also set in plist background modes audio. But the video with the audio both stop playing when you close the app. I've also imported the AVFoundation framework.

4
try this AVAudioSessionCategorySoloAmbient instead of AVAudioSessionCategoryPlayback falcon143
So I replaced my video & audio stream, with an audio only stream. It works in background. But not my video & audio stream. Can I like extract audio stream from MPMoviePlayerController and play that separately?DemosJarco

4 Answers

0
votes

Simply set Application does not run in background to NO in .plsit file

0
votes

You need to make couple of changes in plist file.i.e.

1) Set Required background mode to App plays audio

2) set Application does not run in background to YES.

 NSError *setCategoryErr = nil;
 NSError *activationErr  = nil;
 [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback    error:&setCategoryErr];
 [[AVAudioSession sharedInstance] setActive:YES error:&activationErr];

Then, you need to write these much code in AppDelegate

Now, you can easily run audio while phone screen locks or goes in background.

0
votes

This code worked for me, first you must to give your app permissions to keep playing music in the background (In your .plis), after that go to the wished class and implement this code, first the imports and the the method to play the music.

#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
votes

You need to turn on Background Modes in capabilities.

As shown in figure checkmark the modes you want in background