MPMoviePlayerViewController quits while playing the video - iphone

I am recording the video in my application. I am saving that video inside Documents directory. After taking the video if I try to play the video using the following code. The player opens and quits in one seconds. However if I Quit and then open my app fresh the video I took earlier is playing.
+ (void) playMovieAtURL: (NSURL*) theURL :(id)sender{
NSLog(#"playMovieAtURL");
//senderID = (id *)sender;
if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 3.2) {
NSLog(#"> 3.2");
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:theURL];
if (mp)
{
// save the movie player object
//self.moviePlayerViewController = mp;
//[mp release];
[sender presentMoviePlayerViewControllerAnimated:mp];
//mp.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
mp.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[mp.moviePlayer play];
}
[mp release];
}
else if ([[[UIDevice currentDevice] systemVersion] doubleValue] < 3.2) {
NSLog(#"< 3.2");
MPMoviePlayerController* theMovie = [[MPMoviePlayerController alloc] initWithContentURL: theURL];
theMovie.scalingMode = MPMovieScalingModeAspectFill;
// Register for the playback finished notification
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: #selector(myMovieFinishedCallback:)
name: MPMoviePlayerPlaybackDidFinishNotification
object: theMovie];
// Movie playback is asynchronous, so this method returns immediately.
[theMovie play];
[theMovie release];
}
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *aMoviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:aMoviePlayer];
// If the moviePlayer.view was added to the view, it needs to be removed
if ([aMoviePlayer respondsToSelector:#selector(setFullscreen:animated:)]) {
[aMoviePlayer.view removeFromSuperview];
}
}
This is the code I use for storing the video inside documents.
NSURL *videoURL = [imageInfo objectForKey:UIImagePickerControllerMediaURL];
NSData *webData = [NSData dataWithContentsOfURL:videoURL];
self.itsVideoName = fileName;
[webData writeToFile:[NSString stringWithFormat:#"%#/%#",dataPath,fileName] atomically:TRUE];

I finally figured out the answer after few hours of searching. This is the code I am using now..
-(IBAction)playMovie:(NSString *) theURL
{
NSURL *fileURL = [NSURL fileURLWithPath:theURL];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
//After putting the following line the problem solved.
moviePlayerController.useApplicationAudioSession = NO;
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}
the code moviePlayerController.useApplicationAudioSession = NO; is used to solve this problem. I am not sure how this problem solved I guess I am using session for audio I guess that was causing problem to this.
Also the code which I originally posted was taking lots of memory like if I opened 2 video it cost 104 mb allocation for me. This new code was perfect and it is taking only less memory. Hope this will help for some one..

*NSURL fileURL = [NSURL fileURLWithPath:theURL];
should use NSURL *fileURL = [NSURL URLWithString:theURL];

Related

repeatmode doesn't work in MPMoviePlayerViewController?

How to display a recorded video as repeat as an'n' number of times like in Vine Application.
Here I use the MPMoviePlayerViewController, and works great display the recorded video. But the problem is, it doesn't repeating.
Here the currently using code is,
NSURL *url = [NSURL fileURLWithPath:videoPath];
playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playerController];
[playerController.moviePlayer prepareToPlay];
playerController.view.frame = CGRectMake(200, 402, 300, 200);
playerController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
playerController.moviePlayer.controlStyle = MPMovieControlStyleNone;
playerController.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
playerController.moviePlayer.repeatMode = MPMovieRepeatModeOne;
[self.view addSubview: playerController.view];
[playerController.moviePlayer play];
NSLog(#"repeatMode: %d",playerController.moviePlayer.repeatMode);
[playerController.view addSubview:customview];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[playerController moviePlayer]];
And the NSNotification code,
- (void) movieFinishedCallback:(NSNotification*) aNotification
{
NSLog( #"myMovieFinishedCallback: %#", aNotification );
MPMoviePlayerController *movieController = aNotification.object;
NSLog( #"player.playbackState = %d", movieController.playbackState );
}
Can anyone please give the solution..
Note:
I'm using the XCode 4.5.2 tool and ios simulater 6.0
Try this code. It is working.
NSURL *fileUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Video" ofType:#"mp4"]];
MPMoviePlayerViewController *moviePlayerController = [[MPMoviePlayerViewController alloc]initWithContentURL:fileUrl];
[moviePlayerController.moviePlayer prepareToPlay];
[moviePlayerController.moviePlayer setRepeatMode:MPMovieRepeatModeOne];
[moviePlayerController.moviePlayer setControlStyle:MPMovieControlStyleEmbedded];
[self.view addSubview:moviePlayerController.view];
you can try this code
-(void) movieFinishedCallback:(NSNotification *) aNotification
{
if (aNotification.object == self.moviePlayer) {
NSInteger reason = [[aNotification.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
if (reason == MPMovieFinishReasonPlaybackEnded)
{
[playerController play];
}
}
}
You have to keep the MPMoviePlayerController object as a member, otherwise the app would lose context to it and the movie will not loop.

MPMoviePlayer controlStyle

I want to hide the controls from the MPMoviePlayer with this code:
-(IBAction)video:(id)sender {
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"Intro" ofType:#"mov"];
NSURL *movie = [NSURL fileURLWithPath:moviePath];
MPMoviePlayerController *control = [[MPMoviePlayerController alloc]initWithContentURL:movie];
//[self.view addSubview: control.view];
control.scalingMode = MPMovieScalingModeFill;
control.controlStyle = MPMovieControlStyleNone;
control.shouldAutoplay = YES;
[control play];
MPMoviePlayerViewController *movieplayer = [[MPMoviePlayerViewController alloc]initWithContentURL:movie];
[self presentMoviePlayerViewControllerAnimated:movieplayer]; }
But that does not work.
You are repeating code. MPMoviePlayerViewController has MPMoviePlayerController. So use it as movieplayervc.moviePlayer.controlStyle = MPMovieControlStyleNone;
have you tried this
[videoPlayerobj setControlStyle:MPMovieControlStyleNone];
My player is set up in the viewDidLoad and this line hides the MPMoviePlayerController. I have intialised my MPMoviePlayer controller as *stream.
stream.view.hidden = YES;
Hope this helps!
You can play video and stop video and remove from your custom view with this code. and MPMoviePlayerController is movie player.
Hope this is useful for you.thank you
-(void)playMovie:(id)sender {
UIButton *buttonThatWasPressed = (UIButton *)sender;
buttonThatWasPressed.enabled = NO;
NSString * str=[[NSBundle mainBundle]pathForResource:#"yo2" ofType:#"mov"];
NSURL * url=[NSURL fileURLWithPath:str];
MPMoviePlayerController * movieController=[[MPMoviePlayerController alloc]initWithContentURL:url];
movieController.controlStyle=MPMovieControlStyleFullscreen;
[movieController.view setFrame:self.view.bounds];
[self.view addSubview:movieController.view];
[movieController prepareToPlay];
[movieController play];
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDonePressed:)
name:MPMoviePlayerDidExitFullscreenNotification
object:_moviePlayer];
_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES]; }
This method is called when your video or movie is stop from user or video playback has finish.
-(void) moviePlayBackDonePressed:(NSNotification*)notification {
[_moviePlayer stop];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:_moviePlayer];
if ([_moviePlayer respondsToSelector:#selector(setFullscreen:animated:)])
{
[_moviePlayer.view removeFromSuperview];
}
_moviePlayer=nil;
[self dismissViewControllerAnimated:YES
completion:^{
[self performSegueWithIdentifier:#"show" sender:self];
}];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification { // Remove observer
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[self dismissViewControllerAnimated:YES
completion:^{
[self performSegueWithIdentifier:#"show" sender:self];
}];
}

Playing video in iPad application from server

I want to play a video which is uploaded to the server from an iPad application, but when the screen loads it gives an error:
An AVPlayerItem cannot be associated with more than one instance of AVPlayer
I am using the following code:
-(void)playVideo{
NSURL *url = [NSURL URLWithString:#"http://celeritas.com.pk/emrapp/test.mp4"];
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:mp];
mp.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[self presentMoviePlayerViewControllerAnimated:mp];
[mp release];
NSLog(#"Successfully playing thanks");
}
-(void)playbackFinishedCallback:(NSNotification *)notification{
MPMoviePlayerController *movie = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:movie];
[movie release];
}
http://celeritas.com.pk/emrapp/test.mp4 the URL seems to be wrong...
The requested URL /emrapp/test.mp4 was not found on this server.
Any way this will work
.h
MPMoviePlayerViewController * plyr;
NSURL * url;
#property (nonatomic,retain) MPMoviePlayerViewController *plyr;
#property (nonatomic,retain) NSURL *url;
.m
#synthesize plyr ;
#synthesize url;
url = [NSURL URLWithString:#"valid url"];
plyr = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
Url might be wrong double check that video url link is accessible, secondly do see your network firewall this might also cause problem.
The one tweak you can change instead of:
mp.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
use:
mp.moviePlayer.movieSourceType = MPMovieSourceTypeUnknown;
try this
create object of MPMoviePlayerViewController in .h like MPMoviePlayerViewController *mp;
#import<MediaPlayer/MediaPlayer.h>
mp = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:url]];
[[mp moviePlayer] prepareToPlay];
[[mp moviePlayer] setUseApplicationAudioSession:NO];
[[mp moviePlayer] setShouldAutoplay:YES];
[[mp moviePlayer] setControlStyle:2];
[[mp moviePlayer] setRepeatMode:MPMovieRepeatModeOne];
[self presentMoviePlayerViewControllerAnimated:mp];
try this Code
-(IBAction)playMovie:(id)sender
{
NSURL *fileURL = [NSURL URLWithString:#"http://www.youtube.com/watch?v=3Qjh56woQMw"];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotificationobject:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}

Video won't play on iPhone 5.0 simulator

Im new to xCode programming, ive got this code from an ebook tutorial playing mp4 xCode.
function triggered from a button
(IBAction)playMovie:(id)sender {
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"videoSample" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(moviePlaybackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen=YES;
[moviePlayerController play];
moviePlayerController.scalingMode = MPMovieScalingModeFill;
}
called from play movie function
(void)moviePlaybackComplete:(NSNotification *)notification{
MPMoviePlayerController *moviePlayerController = [notification
object];
[[NSNotificationCenter defaultCenter]removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
}
Ive got no error and warning after i build it. it just output a blank screen after i click the button that triggers the playMovie function. Im confuse ive google it and still got no idea on how to solve the proble.
Im using xCode 4.2 iOS SDK 5.0
try this....replace "example" with the name of your file and "m4v" with the type of file e.g. "mp4" (not .mp4 and dont put the .mp4 bit in the name of the file)
-(IBAction)playVideo:(id)sender;
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"example" ofType:#"m4v"];
MPMoviePlayerViewController* tmpMoviePlayViewController=[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
if (tmpMoviePlayViewController) {
tmpMoviePlayViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:tmpMoviePlayViewController animated:YES];
tmpMoviePlayViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(myMovieViewFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:tmpMoviePlayViewController];
[tmpMoviePlayViewController.moviePlayer play];
}
}
-(void)myMovieFinishedCallback:(NSNotification*)theNotification
{
MPMoviePlayerController *moviePlayer=[theNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer.view removeFromSuperview];
}
I don't know if you are still looking for the answer. Here's what you can try - declare the MPMoviePlayerController in your header(.h) file (i.e. make it a member variable).
MPMoviePlayerController *moviePlayerController;
and add this to your .m file
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
If it still does not work then try to declare it as a #property.

A problem with Media Player base on iOS4 and deploy iOS3

when Run on Device 3.1.2, why it also pass if(NSClassFromString(#"MPMoviePlayerViewController") != nil)
and do code of iOS4 then it will crash , how to fix this issues?
if(NSClassFromString(#"MPMoviePlayerViewController") != nil) {
// iOS 4 code
NSLog(#"MPMoviePlayerViewController");
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:AppDelegate.PushLink]];
if (mp) {
// save the movie player object
self.theMovie4 = mp;
[mp release];
//Present
[self presentMoviePlayerViewControllerAnimated:self.theMovie4];
// Play the movie!
self.theMovie4.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[self.theMovie4.moviePlayer play];
}
}
else {
//iOS 3 Code
AppDelegate = nil;
AppDelegate = [[UIApplication sharedApplication] delegate];
[AppDelegate ForceHideNavigationBar];
theMovie3 = nil;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePreloadDidFinish:)
name:MPMoviePlayerContentPreloadDidFinishNotification
object:theMovie3];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie3];
// Register to receive a notification when the movie scaling mode has changed.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieScalingModeDidChange:)
name:MPMoviePlayerScalingModeDidChangeNotification
object:theMovie3];
theMovie3 = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:AppDelegate.PushLink]];
[theMovie3 play];
}
I am doing a very similar thing on my app which is using 4.0 as the base SDK and yet I am targeting iOS3 devices too. I had to check the OS version to properly provide the right code for the video playback. For example:
- (void) playMovieAtURL: (NSURL*) theURL {
NSLog(#"playMovieAtURL");
if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 3.2) {
NSLog(#"> 3.2");
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:theURL];
if (mp)
{
// save the movie player object
//self.moviePlayerViewController = mp;
//[mp release];
[self presentMoviePlayerViewControllerAnimated:mp];
mp.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[mp.moviePlayer play];
[mp release];
}
}
else if ([[[UIDevice currentDevice] systemVersion] doubleValue] < 3.2) {
NSLog(#"< 3.2");
MPMoviePlayerController* theMovie = [[MPMoviePlayerController alloc] initWithContentURL: theURL];
theMovie.scalingMode = MPMovieScalingModeAspectFill;
// Register for the playback finished notification
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: #selector(myMovieFinishedCallback:)
name: MPMoviePlayerPlaybackDidFinishNotification
object: theMovie];
// Movie playback is asynchronous, so this method returns immediately.
[theMovie play];
}
}
Hope this helps!!
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
NSString *movpath = [[NSBundle mainBundle] pathForResource:#"splash" ofType:#"mp4"];
MPMoviePlayerViewController* mpviewController = [[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL fileURLWithPath:movpath]];
[window addSubview:mpviewController.view];
[window makeKeyAndVisible];
MPMoviePlayerController *mp = [mpviewController moviePlayer];
[mp prepareToPlay];
[[mpviewController moviePlayer] play];
This code will work in iOS4
It however does quit once the movie completes because you must create the method for the notification. Also you need to release the movie there so that you can catch the memory leak.