MPMoviePlayerController is functioning wrong - ios

When i tried a video saved on the Xcode project , it worked.
then when i changed it with a url , it always returns this error :
_itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}
Here's my code :
NSURL *url = [NSURL fileURLWithPath:#"https://m.youtube.com/watch?v=9MNAeGWd_04"];
movieController = [[MoviePlayerVC alloc]initWithContentURL:url];
movieController.view.frame = self.view.bounds;
[movieController.moviePlayer setShouldAutoplay:YES];
[movieController.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
[[movieController view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
[self presentViewController:movieController animated:NO completion:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playbackStateDidChange:)
name:#"MPAVControllerPlaybackStateChangedNotification"
object:nil];

fileURLWithPath: expects a local path url - /User/blah/blah.mp3
Try to use [NSURL URLWithString:#"https://m.youtube.com/watch?v=9MNAeGWd_04"]

IF YOU PLAY FROM URL THEN TRY THIS
-(void)playFromLiveUrl
{
NSURL *aUrl = [NSURL URLWithString:#"*YOUR URL*"];
_moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:aUrl];
_moviePlayer.controlStyle = MPMovieControlStyleNone;
_moviePlayer.shouldAutoplay = YES;
_moviePlayer.view.frame = asyvideoImage.frame;
_moviePlayer.scalingMode=MPMovieScalingModeFill;
[self.view addsubview _moviePlayer.view];
[_moviePlayer prepareToPlay];
[_moviePlayer setFullscreen:NO animated:NO];
}
IF YOU ARE USE LOCAL FILE THEN USE THIS
-(void)playFromLocal
{
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:#"** LOCAL PATH ** "]];
_moviePlayer.controlStyle = MPMovieControlStyleNone;
_moviePlayer.shouldAutoplay = YES;
_moviePlayer.view.frame = asyvideoImage.frame;
_moviePlayer.scalingMode=MPMovieScalingModeFill;
[self.view addsubview _moviePlayer.view];
[_moviePlayer prepareToPlay];
[_moviePlayer setFullscreen:NO animated:NO];
}

The MPMoviewPlayerController Can't get your format. U r try to load the MPMoviePlayerController with youTube link, it won't work (as u can see :)
Here u can find the iPhone video format support.
Try to load your MoviePlayer something like this (for example .m3u8 format):
[movieController setContentURL:[NSURL URLWithString:#"http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8"]];

MPMoviePlayerController cannot play a Flash Youtube URL. Use a web view instead. UIWebView on iOS is smart enough play youtube content.
You can see the video formats MPMoviePlayer and AVFoundation support here

Related

AVPlayer not showing FullScreen

I am working on playing a recorded video recorded by AVCapture.I am saving the video URL in string named outputFileURL. I tried playing back the video using AVPlayerLayer concept. The code I used is
AVPlayer *avPlayerq = [AVPlayer playerWithURL:outputFileURL];
avPlayerq.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *videoLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayerq];
videoLayer.frame= self.view.bounds;
[self.view.layer addSublayer:videoLayer];
[avPlayerq play];
But the video I am getting is not full screen.
Can anyone can help me to solve?
I added the following code and I am able to get the full screen.
videoLayer.videoGravity=AVLayerVideoGravityResizeAspectFill;
Hope this may help.
AVPlayer *player = [[AVPlayer alloc] initWithURL:url];
AVPlayerViewController *playerViewController = [AVPlayerViewController new]; playerViewController.delegate = self;
playerViewController.player = player;
[playerViewController.player play];
[self presentViewController:playerViewController animated:YES completion:nil];
Try This
Ok than you have to use MPMoviePlayerController
NSString *path = [[NSBundle mainBundle] pathForResource:#"Video_Intro" ofType:#"mov"];
moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:path]];
moviePlayer.view.frame = ivVideoThumbnail.frame;
//moviePlayer.view.top += 20; // Add to fix 20 pixel diff of moviePlayer view
// Register this class as an observer instead.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[svIntro addSubview:moviePlayer.view];
moviePlayer.fullscreen = true;
[moviePlayer play];
Also implement Observer method
-(void)movieFinishedCallback:(id)mPlayer
{
[moviePlayer.view removeFromSuperview];
}

How to play Video in UIView using URL getting from server in iOS

I want to play video in my UIView from the URL that i got from the JSON Response from server.
This is code that i wrote to play video but nothing happens.
NSURL *movieURL = [NSURL URLWithString:objAlbum.strVideoURL];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[moviePlayerController.view setFrame:self.movieView.bounds];
[self.movieView addSubview:moviePlayerController.view];
// Configure the movie player controller
moviePlayerController.controlStyle = MPMovieControlStyleNone;
[moviePlayerController prepareToPlay];
[moviePlayerController play];
In strVideoURL i'm getting video url.
Check that URL has any spaces. Better to Add below line of code
NSURL *movieURL = [NSURL URLWithString:[objAlbum.strVideoURL stringByAddingPercentEscapesUsingEncoding:
NSUTF8StringEncoding]];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[moviePlayerController.view setFrame:self.movieView.bounds];
// Configure the movie player controller
moviePlayerController.controlStyle = MPMovieControlStyleNone;
//[moviePlayerController prepareToPlay];
[moviePlayerController play];
[self.movieView addSubview:moviePlayerController.view];
Note: Print the movie URL and play using browser to check it is valid URL or not..!
Hope it helps you...!
i have solve this using following code
NSURL *vidURL = [[NSURL alloc]initWithString:objAlbum.strVideoURL];
self.movie = [[MPMoviePlayerController alloc]initWithContentURL:vidURL];
self.movie.controlStyle = MPMovieControlStyleDefault;
self.movie.shouldAutoplay = YES;
[self.view addSubview:self.movie.view];
[self.movie setFullscreen:YES animated:YES];
but i want to open it now in UIView defined in TableViewCell like video open in FB(Facebook)

Looping video using MPMoviePlayerController somewhere between startTime and endTime of the Video

Suppose the video duration is 50 seconds. In need to loop part of the video between 10th sec to 20th sec.
Very first time when I play the video it works fine. But for 2nd iteration onwards the video plays from the start (i.e from 0th second rather than 10th second).
Following is my code:
self.moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:currentVideoURL];
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
self.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
[self.moviePlayer.view setFrame: movieView.bounds];
[self.moviePlayer.view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
[self.moviePlayer setInitialPlaybackTime:10.0];
[self.moviePlayer setEndPlaybackTime:20.0];
[self.moviePlayer prepareToPlay];
self.moviePlayer.shouldAutoplay = YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerDidFinish:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:self.moviePlayer];
[self.movieView addSubview:moviePlayer.view];
- (void)moviePlayerDidFinish:(NSNotification *)note {
if (note.object == self.moviePlayer) {
NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
if (reason == MPMovieFinishReasonPlaybackEnded) {
// [self.moviePlayer setInitialPlaybackTime:10.0];
// [self.moviePlayer setEndPlaybackTime:20.0];
[self.moviePlayer play];
}
}
}
Also I tried to reset the InitialPlaybackTime and EndPlaybackTime in by moviePlayerDidFinish Method, but didn't worked.
Depending on the requirement I only need to loop only particular part of the video.
Please help me.. Thanks!!!
set the property repeatMode = MPMovieRepeatModeOne of MPMoviePlayerController self.moviePlayer.
self.moviePlayer repeatMode = MPMovieRepeatModeOne;
See Example
-(void)playMovie:(id)sender
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"1" ofType:#"MOV"]];
_moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url];
_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
_moviePlayer.initialPlaybackTime = 3.0;
_moviePlayer.endPlaybackTime = 5.0;
_moviePlayer.repeatMode = MPMovieRepeatModeOne;
[self.view addSubview:_moviePlayer.view];
[_moviePlayer setFullscreen:YES animated:YES];
}

How to play video on MPMoviePlayerController using ALAsset URL?

How to play video on MPMoviePlayerController using ALAsset URL?
I tried to directly give the ALAsset URL to MPMoviePlayer using setContentURL: method, but it did not work.
if (!self.moviePlayer)
{
self.moviePlayer = [[MPMoviePlayerController alloc] init];
}
[self.moviePlayer setContentURL:[NSURL fileURLWithPath:playPath]];
self.moviePlayer.allowsAirPlay=YES;
self.moviePlayer.controlStyle=MPMovieControlStyleDefault;
[ScrollView addSubview: [self.moviePlayer view]];
self.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[self.moviePlayer prepareToPlay];
[self.moviePlayer play];
#Mahesh can you please try with this :
self.moviePlayer.moviePlayer.movieSourceType = MPMovieSourceTypeFile ;

MPMoviePlayerController gives me a black empty view,no video playing

iOS 5.1 ,here is the code:
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:#"map.mp4"]];
[player prepareToPlay];
[player.view setFrame: self.view.bounds]; // player's frame must match parent's
[self.view addSubview: player.view];
// ...
[player play];
in fact ,this is just the same as the apple reference say,but when I click the button to use this function,there is just a black Rectangle stay there,no vedio playing ,no sound happening,and no crush,so I want to know how to make this work
Sorry for very late reply. The Solution is kind of wierd. But it helped me keep going while facing the same issue.
MPMovieSourceTypeStreaming is the thing you have missed.
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:#"map.mp4"]];
[player prepareToPlay];
[player.view setFrame: self.view.bounds]; // player's frame must match parent's
player.movieSourceType = MPMovieSourceTypeStreaming;
[self.view addSubview: player.view];
// ...
[player play];
You may be sending your
[player play]
message too soon.
Use the following notification observation
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playMovie:) name:MPMoviePlayerLoadStateDidChangeNotification object:player];
To listen for the loadState to change to playable. To check for that update do this :
- (void)playMovie:(NSNotification *)notification {
MPMoviePlayerController *player = notification.object;
if (player.loadState & MPMovieLoadStatePlayable)
{
NSLog(#"Movie is Ready to Play");
[player play];
}
}
The movie should begin playing once it's ready.
Try this below code for play video from your project Resource :
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"map" ofType:#"mp4"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[player prepareToPlay];
[player.view setFrame: self.view.bounds]; // player's frame must match parent's
[self.view addSubview: player.view];
// ...
[player play];
Thanks..!
Building on #Dinesh's answer, your URL creation is incorrect. The value of
[NSURL URLWithString:#"map.mp4"]
will be nil since #"map.mp4" is not a valid URL. To create a valid url from the app's bundle do what Dinesh says. To create a remote URL you would do something like
[NSURL URLWithString:#"http://www.jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v"]
The URL should contain all it's parts: protocol, host, path, file.
Cheers, Felipe
Add your movie player controller view on main windows like:
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:#"map.mp4"]];
[player prepareToPlay];
[player.view setFrame: self.view.bounds];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.window addSubview: player.view];
[player play];
Hope so it will work!
Your player should be a property of your view controller, something like this:
.h:
#property(nonatomic,weak)MPMoviePlayerController *moviePlayer;
.m:
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:#"map.mp4"]];
[player prepareToPlay];
[player.view setFrame: self.view.bounds]; // player's frame must match parent's
//set the player to your property
self.moviePlayer=player;
[self.view addSubview: self.moviePlayer.view];
// ...
[self.moviePlayer play];
Please use MPMoviePlayerViewController Because of MP4 file. When you use MOV then working perfect!!
MPMoviePlayerViewController *moviePlayerViewController;
-(void)PlayVideoController:(NSString*)videoUrl1 {
NSURL *fileURL = [NSURL URLWithString:videoUrl1];
moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
// Register for the playback finished notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController.moviePlayer];
//Present
[self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];
// Play the movie!
moviePlayerViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[moviePlayerViewController.moviePlayer play];
}
-(void)myMovieFinishedCallback:(NSNotification*)aNotification {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerViewController.moviePlayer];
[moviePlayerViewController release], moviePlayerViewController = nil;
}
You might want to cross check video url for blank spaces. Following code works for me.
- (IBAction)btnPlayVideo:(id)sender {
self.strVideoUrl = [self.strVideoUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *fileURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#", self.strVideoUrl]];
NSLog(#"Url to play = %#", self.strVideoUrl);
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
[self.moviePlayerController.view setFrame:self.view.bounds];
[self.view addSubview:self.moviePlayerController.view];
self.moviePlayerController.shouldAutoplay = YES;
self.moviePlayerController.fullscreen = YES;
self.moviePlayerController.controlStyle=NO;
[self.moviePlayerController prepareToPlay];
self.moviePlayerController.movieSourceType = MPMovieSourceTypeStreaming;
[self.moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
self.moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
[self.moviePlayerController.view removeFromSuperview];
self.closeVideAdProp.hidden = NO;
btnCloseAd.hidden=FALSE;
}

Resources