MPMoviePlayerController interrupting playing audio - iphone

I'm developing an iPhone app that can play online videos and
I want to play an ad first when the video data is loading.
First, I request an AdColony video Ad and begin to play it, then use MPMoviePlayerController to load video data, But when the video is prepared to play, it interrupts the previous Ad sound, and causes the ad to stop.
Here are the codes Im using:
// Play Ad first
[AdColony playVideoAdForSlot:1 withDelegate:self]; // It also use MPMoviePlayerController to play video
// Load video for playing
moviePlayer = [[MPMoviePlayerController alloc]
init];
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
moviePlayer.controlStyle = MPMovieControlStyleNone;
moviePlayer.shouldAutoplay = NO;
moviePlayer.view.frame = CGRectMake(0,44,320,320);
moviePlayer.view.userInteractionEnabled = YES;
[moviePlayer prepareToPlay]; // Interrupt Ad playing
The offical doc said "calling prepareToPlay may interrupt the movie player’s audio session", so the Ad was interrupted. If I remove prepareToPlay, the video data will not be preloaded
I have tried to put
[AdColony playVideoAdForSlot:1 withDelegate:self]; below [moviePlayer prepareToPlay];
but it does not work. Does anyone know how to resolve it?

There are ways to handle interruptions,
read through he Apples guide
http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/HandlingAudioInterruptions/HandlingAudioInterruptions.html#//apple_ref/doc/uid/TP40007875-CH11-SW1

Related

MP4 file not playing in MPMoviePlayerController?

I am using MPMoviePlayerController to play .mp4 file, while I'm testing .mp4 recorded in windows platform its working fine, however .mp4 recorded using Android is not playing in my iOS device(iPod touch), Player keeps on buffering it.. Any Idea what could be the problem? And please help me, how to debug the issue in MPMoviePlayerController.
NSURL *file_url = [NSURL fileURLWithPath:filepath];
moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:file_url];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
// moviePlayer.useApplicationAudioSession = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
thanks
I have found the problem, iOS not supporting the audio format, which is recorded in Android. Now I recorded using AAC it is playing fine.

playing a video with MPMoviePlayerController while recording with AudioQueue

i'm developing an app which requires to play a video from an URL using MPMoviePlayerController while acquiring audio samples using AudioQueue from the microphone to further analyze them.
The problem is that I cannot record when the video starts playing (and also when it finishes). Simply the audio sampling stops. Instead, if I disable the video play, audio recording goes well.
I've tried setting up an AudioSession with property kAudioSessionProperty_OverrideCategoryMixWithOthers but with no success (it returns error). Moreover I think that it's useless setting a property in AudioSession when using AudioQueue. Even setting useApplicationAudioSession = NO for MPMoviePlayerController does not give any help.
Hereby the core code where the player is created:
audioManager = [[AudioController alloc] init];
//setting AudioQueue: audio buffer, sample rate, format ID (PCM), bits per channel
audioManager.delegate = self;
[audioManager startAudioRecording]; //starts recording with AudioQueue
self.playerVC = [[[MPMoviePlayerController alloc] init] autorelease];
layerVC.view.frame = self.viewPlayer.bounds;
[self.viewPlayer addSubview:playerVC.view];
playerVC.useApplicationAudioSession = YES; //if NO nothing changes
[playerVC setContentURL:[NSURL URLWithString:#"http://www......."]];
[playerVC prepareToPlay];
[playerVC play];
You're on the right way.
First, set the kAudioSessionProperty_OverrideCategoryMixWithOther to true. This will allow your app's sound mixes with sounds from another apps.
UInt32 allowMixing = true;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(allowMixing), &allowMixing);
After that, you will need to set MPMoviePlayerController to not use your app's AudioSession:
[yourMPMoviePlayerControllerInstance setUseApplicationAudioSession:NO];

Can't play mp4 in Cocoa-Touch App

I am trying to play an mp4 after detecting a signal in the audio jack.
The video is playing once, after a delay of 1 sec which i dont want, and then application is freezing and i get this warning on the debugger: (only on the iphone, its working on simulation)
(8F190)/Symbols/System/Library/VideoDecoders/H264H4.videodecoder (file not found).
(8F190)/Symbols/System/Library/VideoDecoders/MP4VH4.videodecoder (file not found).
my code for the video is this :
//play video1
url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"sample1" ofType:#"mp4"]];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayer.useApplicationAudioSession=NO;
[moviePlayer prepareToPlay];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
[moviePlayer setMovieControlMode:MPMovieControlModeHidden];
moviePlayer.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:moviePlayer.view];
[moviePlayer play];
I couldnt find anything on this warning on the net.
I have tried any kind of video encoding,according to the Apple docs.
We just cant play movies, does anyone have any idea how to fix it?
thanks .
If you run that code twice, the video won't play the second time, because you are creating a second MPMoviePlayerController and a second view, and only one MPMoviePlayerController view in your app can play video. So it works the first time but not the second time. You should be retaining your MPMovieVideoController in a property so that you can remove its view and release the MPMovieVideoController before trying to make a new one.

How to show "Loading Movie..." message with MPMoviePlayerController

I'm working with the MPMoviePlayerController for the iOS platform and am not sure why the "Done" and "Loading Movie..." controls are not displaying automatically when loading a new video.
My original implementation of this was to use the UIWebView control to stream the videos, and when the videos are accessed, the "Done" and "Loading Movie..." controls are displayed until the video is loaded and ready to play. I would like to reproduce the same user experience with the MPMoviePlayerController.
Should I get the "Done" and "Loading Movie..." control overlays for free? If not, what do I have to set to get them to show up while the video is loading?
NSURL *videoUrl = [NSURL URLWithString:urlString];
self.moviePlayer = [[[MPMoviePlayerController alloc] init] autorelease];
self.moviePlayer.movieControlMode = MPMovieControlModeDefault;
[self.moviePlayer setContentURL:videoUrl];
[self.moviePlayer prepareToPlay];
[self.view addSubview:self.moviePlayer.view];
[[self.moviePlayer view] setFrame: [self.view bounds]];
[self.moviePlayer setFullscreen:YES animated:YES];
This is answer to your follow-up question, but anyways... You can speed-up things a little bit with this:
prepareToPlay
Prepares the current item for playback. (required)
- (void)prepareToPlay
This method is called automatically when you call the play method. Calling it before you call play gives the receiver a chance to prepare items sooner and may result in decreased latency when starting playback. However, calling this method may also interrupt any active audio sessions.
Available in iOS 3.2 and later.
Declared In MPMediaPlayback.h

MPMoviePlayerController questions, best practices

I have any number of thumbnail images that, when tapped, will play a different video (fullscreen). I have never been clear on whether I should keep one MPMoviePlayerController object in my view controller and have it play whichever url according to the thumbnail that was tapped, or create a new MPMoviePlayerController each time. What is the best practice?
I am also having problems where tapping on different thumbs crashes the app, I believe because the MPMoviePlayerController tries to stream a video while it is already trying to stream. There seems to be no way to cancel a MPMoviePlayerController and clear out what it was doing, then start loading a new video.
Here's how I create it:
MPMoviePlayerController* moviePlayer = [[MPMoviePlayerController alloc] init];
self.player = moviePlayer;
[moviePlayer release];
Then to play a video I do this:
//would like to do something like this first - [self.player clear];
self.player.contentURL = someURL;
[self.view addSubview:player.view];
[self.player prepareToPlay];
[self.player play];
Any advice is welcome... thanks.
When you are changing the video in an MPMovieplayerController,then you can remove the mpmoviecontrollerplayer view from super view using removeFromSuperView and again add it's subview to the super view initializing it with new URL.
No need to create new object every time.