Movie player failed to play - objective-c

I am trying to play video with this guide :http://www.techotopia.com/index.php/Video_Playback_from_within_an_iOS_7_Application
so i have :
//movieplayer initialization
NSURL *url = [NSURL URLWithString:
#"http://www.youtube.com/watch?v=CS-6gJkNAo4"];
self.moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer];
self.moviePlayer.controlStyle = MPMovieControlStyleDefault;
self.moviePlayer.shouldAutoplay = YES;
[self.movieView addSubview:self.moviePlayer.view];
[self.moviePlayer setFullscreen:YES animated:YES];
Where the player is loaded to some view .
I don't see nothing and i only get this error message :
_itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}

MPMoviePlayerController can't play Youtube video. You have to paste direct link to file to play video in such way.

Related

Make MPMoviePlayerController main view

I have got a horizontal scrollview like the Springboard with 16 buttons on each of these pages. When you click a button I want to only see the MPMoviePlayerController which plays a video and nothing else, like if you are watching a movie in the videos application. Therefore, I've got the following code:
- (void)button: (id) sender {
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"video" ofType:#"MOV"];
NSURL *videoURL = [NSURL fileURLWithPath:moviePath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
moviePlayerController.fullscreen = YES;
[self.view addSubview:moviePlayerController.view];
[moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}
Unfortunately, I only get to hear sound and there is no video showing. Can anybody help me, please?
Update: I found out that you would have to make a frame. I used the following code:
[moviePlayerController.view setFrame:CGRectMake(0, 0, 768, 1004)];
But it isn't in fullscreen that way. Even if I've set the fullscreen property to YES. I can see that because there are no video-controls at the top of the view but at the bottom of the view.
I found the solution. You should use the following code:
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"1" ofType:#"MOV"];
NSURL *videoURL = [NSURL fileURLWithPath:moviePath];
MPMoviePlayerViewController *playerView = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[self presentModalViewController:playerView animated:YES];

Can not detect MPMoviePlayerDidExitFullscreenNotification

In my project, I used MPMoviePlayerController to stream video from an http url. It plays fullscreen. When the video is playing, if you tap on "Done" button the video stops and it disappears, but the problem is; if you pinch to close video screen the video screen disappears but it still plays, sound of video continues to play.
I tried to detect exit fullscreen notification and manually stop the video but it didn't work. My moviePlayerDidExitFullScreen method didn't called.
To control that if I am getting the notifications on the right way I tried to get another notification : MPMoviePlayerPlaybackStateDidChangeNotification, and it is working. It calls the method on video launching.
I searched many forums and Apple documentations but I couldn't find enough information.
Here is my code to open a fullscreen video and detect exit fullscreen :
- (void)openFullVideo
{
NSString* path = #"http://trtvizyon.mysys.com/test/leyla_ile_mecnun.mp4";
NSURL *fileURL = [NSURL URLWithString:path];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerDidExitFullScreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
player.controlStyle = MPMovieControlStyleDefault;
player.movieSourceType = MPMovieSourceTypeStreaming;
[self.view addSubview:player.view];
[player setFullscreen:YES animated:YES];
[player play];
}
- (void) moviePlayerDidExitFullScreen:(id)sender {
NSLog(#"moviePlayerDidExitFullScreen");
}
OK, I played with your code for a while, and finally shot that little bug in the gut.
Your first problem is not retaining the player object (assuming you are using ARC, if not, then skip this). So, just make sure you retain it as an instance variable for example:
//Header File
#interface ViewController : UIViewController {
MPMoviePlayerController* _player;
}
// Implementation File
- (void)openFullVideo {
// ...
_player = player;
}
Now, if that just works, then great!! But I am getting a dreaded unsolved bug on apple's side:
An AVPlayerItem can occupy only one position in a player's queue at a time
To solve this issue, do it like so:
NSString* path = #"http://trtvizyon.mysys.com/test/leyla_ile_mecnun.mp4";
NSURL *fileURL = [NSURL URLWithString:path];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerDidExitFullScreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
player.controlStyle = MPMovieControlStyleDefault;
player.movieSourceType = MPMovieSourceTypeStreaming;
[self.view addSubview:player.view];
[player setContentURL:fileURL];
[player setInitialPlaybackTime:-1.f];
[player setFullscreen:YES animated:YES];
[player prepareToPlay];
[player play];
_player = player;
That should do it!
Some Other Friendly Advice:
Make sure you remove yourself from NSNotificationCenter before playing the movie again.
I would suggest adding something like if (_player != nil) to avoid recreating the object.

How to play a video with MPMoviePlayerController

I'm using this code to play a video streaming from the apple site
- (IBAction)playMovie:(UIButton *)sender {
NSLog(#"start playing");
//NSURL *url = [NSURL URLWithString:#"http://spatie.be/test.mov"];
NSURL *url = [NSURL URLWithString:#"http://stream.qtv.apple.com/events/mar/123pibhargjknawdconwecown/12oihbqeorvfhbpiubqnfv3_650_ref.mov"];
moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
NSError *error = [[notification userInfo] objectForKey:#"error"];
if (error) {
NSLog(#"Did finish with error: %#", error);
}
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player
respondsToSelector:#selector(setFullscreen:animated:)])
{
[player.view removeFromSuperview];
}
}
When playMovie is invoked, moviePlayBackDidFinish is immediatelty called and the error message is logged:
Did finish with error: Error Domain=MediaPlayerErrorDomain Code=-11800
"The operation could not be completed" UserInfo=0x78d25d0
{NSLocalizedDescription=The operation could not be completed}
How can I solve this error?
You didn't tag this as iOS, but MPMoviePlayer is iOS only. This should work just fine with a video that will play on iOS. Unfortunately the video you are trying to play is not iOS compatible. Try this URL:
http://stream.qtv.apple.com/events/mar/123pibhargjknawdconwecown/12oihbqeorvfhbpiubqnfv3_ipad_vod_ref.mov

When my movie is finished to play, the app crashes

I tried to launch a movie by clicking in a button. When I click the Movie player appears and works fine. The movie plays until the end and the Movie player disappears. After it disappears, my app crashes...
I use my View Controller in Tab Bar Application with this code :
- (void)moviePlayBackDidFinish:(NSNotification *) aNotification{
MPMoviePlayerController *player = [aNotification object];
[player setFullscreen:NO animated:YES];
[player.view removeFromSuperview];
[player stop];
player.initialPlaybackTime = -1.0;
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player release];
player=nil;
}
- (IBAction)playVideo:(UIButton *)playButton{
NSString *url = [[NSBundle mainBundle] pathForResource:#"Teaser 04" ofType:#"mov"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
player.shouldAutoplay = YES;
player.view.frame = CGRectMake(0., 44., self.view.bounds.size.width, self.view.bounds.size.height-44);
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[self.view addSubview:player.view];
[player play];
}
I don't know what I'm doing wrong. I just want configure a button that start the video and when the video is finished, the MoviePlayer View disappears and the app come back to my initial .xib
Thank you for your help
I found the solution, I don't know if it is the best but for what I want it's working :
- (void)movieFinishedCallBack:(NSNotification *) aNotification{
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player.view removeFromSuperview];
[player stop];
[player release];
}
- (IBAction)playVideo:(UIButton *)playButton{
NSString *url = [[NSBundle mainBundle] pathForResource:#"Teaser 04" ofType:#"mov"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallBack:) name:MPMoviePlayerPlaybackDidFinishNotification object:player];
player.view.frame = CGRectMake(0, 0, 950, 600);
[self.view addSubview:player.view];
[player play];
}
I could be wrong, but my guess is that the player gets used somewhere in the backtrace for the call to moviePlayBackDidFinish: ... If I'm right, then deallocing the object there - which it looks like you're doing - is bad news, since the object is still needed somewhere up in the stack.
Check out this post for more details: How to release MPMoviePlayerController?

Playing video and audio in iPhone not working

So we have buttons linked up to display images/videos/audio on click depending on a check we do earlier. That part works fine. It knows which one to play, however, when we click the buttons for video and audio, nothing happens. The image one works fine.
The video and audio are being taken for a URL online, they are not local, but everywhere said this was still possible. Here is a little snippet of the code where we play the two files:
if ( [fName hasSuffix:#".png"])
{
NSLog(#"PICTURE");
NSURL *url = [NSURL URLWithString:
fName];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
// self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"MainBG.jpg"]];
[self.view addSubview:[[UIImageView alloc] initWithImage:image]];
}
if ( [fName hasSuffix:#".mp4"])
{
NSLog(#"VIDEO");
//NSString *path = [[NSBundle mainBundle] pathForResource:fName ofType:#"mp4"];
//NSLog(path);
NSURL *url = [NSURL fileURLWithPath:fName];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[player play];
}
if ( [fName hasSuffix:#".mp3"])
{
NSLog(#"AUDIO");
NSURL *url = [NSURL fileURLWithPath:fName];
NSData *soundData = [NSData dataWithContentsOfURL:url];
AVAudioPlayer *avPlayer = [[AVAudioPlayer alloc] initWithData:soundData error: nil];
[avPlayer play];
}
See anything wrong? By the way it compiles and runs, but nothing happens when we hit the button that executes that code.
I am not sure why they are not playing, does it ever reach the video and audio conditons?
Either way your currently missing before you play the movie
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification object:player];
You will then need the function to handle the callback
-(void)movieFinishedCallback:(NSNotification *)notification{
NSLog(#"MOVIE FINISHED");
if(player == [notification object]){
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player release];
player = nil;
}
}