Question

Ok, so I have an app in which there are many videos of exercises, which do not have any sound at all.

Also, at the start of the exercises, I display all the mp3 files from the Music Player of the device for the User to select audio files.

But, then whenever the video starts, the Music Player gets paused. How do I make it work in such a way that Music Player keeps on playing and Video also plays simultaneously.

For Video, using the class -- MPMoviePlayerViewController

It is added as follows :

self.movieplayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:urlStr];
self.movieplayerController.moviePlayer.controlStyle =  MPMovieControlStyleFullscreen;
self.movieplayerController.moviePlayer.fullscreen = YES;
self.movieplayerController.moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[self presentModalViewController:movieplayerController animated:YES];

For Music Player, class is --- MPMusicPlayerController.

Audio is selected and played as follows :

 - (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
      [self dismissModalViewControllerAnimated: YES];

      [self.musicPlayer stop];

      [self.musicPlayer setQueueWithItemCollection:mediaItemCollection];

      [self.musicPlayer play];
}

EDIT :

Also, tried the AVPlayer for playing videos but no success!

Code is as follows :

AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:urlStr];
AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playerItem];

AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:player];

avPlayerLayer.frame = self.view.frame;
[self.view.layer addSublayer:avPlayerLayer];
[player play];
Was it helpful?

Solution

Got the answer finally....

Works with AVPlayer.

Initialize it as above in the question.

AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:urlStr];
AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playerItem];

AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:player];

avPlayerLayer.frame = self.view.frame;
[self.view.layer addSublayer:avPlayerLayer];
[player play];

But before this, in the AppDelegate, create a AudioSession which would allow the Mixing.

AudioSessionInitialize(NULL, NULL, NULL, NULL);  
UInt32 sessionCategory = kAudioSessionCategory_AmbientSound;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
UInt32 allowMixWithOthers = true;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(allowMixWithOthers), &allowMixWithOthers);
AudioSessionSetActive(true);    

Got the answer from here.

App with AVPlayer plays mp4 interrupt iPod music after launched

OTHER TIPS

You can use AudioToolbox for this purpose. I've developed a small demo (after reading your question) and its working.

Download the demo for MPMoviePlayerViewController from here : - http://mobile.tutsplus.com/tutorials/iphone/mediaplayer-framework_mpmovieplayercontroller_ios4/

Now add the any .wav file to your bundle.

Now modify this method :

-(IBAction)playMovie:(id)sender
{
    UIButton *playButton = (UIButton *) sender; 

    NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"big-buck-bunny-clip" ofType:@"m4v"];
    NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
    MPMoviePlayerViewController *moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlaybackComplete:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:moviePlayerController];

    [moviePlayerController.view setFrame:CGRectMake(playButton.frame.origin.x, 
                                                    playButton.frame.origin.y, 
                                                    playButton.frame.size.width, 
                                                    playButton.frame.size.height)];

    [self.view addSubview:moviePlayerController.view];
    //moviePlayerController.fullscreen = YES;

    //moviePlayerController.scalingMode = MPMovieScalingModeFill;

    [moviePlayerController.moviePlayer play];

    NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"wav"];

    SystemSoundID soundID;

    AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);

    AudioServicesPlaySystemSound (soundID);

    [soundPath release];
}

It just a demo. Hope it helps further...

Unfortunately, AudioSessionInitialize and friends were deprecated with iOS7. Its much easier to mix now with AVAudioSession. I use the audioSession object as a global - not sure what would happen if it was deallocated. AVPlayer sure gives up in a hurry if it's declared on the stack - so beware of that gotcha.

  _audioSession = [AVAudioSession sharedInstance];
  [_audioSession setCategory:AVAudioSessionCategoryAmbient error:&theError];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top