iOS - Playing more than one video - ios

I am using this code with the MediaPlayer framework to play a video:
-(void)GrommeVideoExcerpt1
{
NSURL *url1 = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"GrommeVideoExcerpt1" ofType:#"mp4"]];
grommePlayer1 = [[MPMoviePlayerController alloc]
initWithContentURL:url1];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:grommePlayer1];
grommePlayer1.controlStyle = MPMovieControlStyleDefault;
grommePlayer1.shouldAutoplay = YES;
[self.view addSubview:grommePlayer1.view];
[grommePlayer1 setFullscreen:YES animated:YES];
}
But when I play one video, then navigate to another video and try to play it, the app crashes. It gives me this error:
http://pastebin.com/nUGLXEAi

The problem here is you are adding a notification for when the video finishes playing, and that notification triggers a method you do not have (moviePlayBackDidFinish:) causing the crash.
You could implement this method or remove the notification, depends what you want to do after the video is done playing i.e. remove a viewController that plays the video, etc.
//Register notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
//Then the method ...
-(void) moviePlaybackDidFinish:(NSNotification *)aNotification{
[grommePlayer1.view removeFromSuperview];
grommePlayer1 = nil;
}

Related

MPMoviePlayerViewController Dismiss Without playing

The problem video can be seen in :
http://app.bowerchat.com/images/118_1435046739658.mp4
MPMoviePlayerViewController Dismissing immediately without play.
What can be reason?Some videos are loaded nicely and playing.
How can i check the video can be played or not?
I tried
NSLog(#"%lu",(unsigned long)self.mpController.moviePlayer.loadState);
NSLog(#"%d",self.mpController.moviePlayer == nil);
Results always 0 - 0
MY Code to play video
self.mpController = [[MPMoviePlayerViewController alloc] initWithContentURL:moveUrl];
[self.mpController.moviePlayer prepareToPlay];
NSLog(#"%lu",(unsigned long)self.mpController.moviePlayer.loadState);
NSLog(#"%d",self.mpController.moviePlayer == nil);
[parent presentMoviePlayerViewControllerAnimated:self.mpController];
The reason could be that movie that has extention *.mp4 is actually of different type.
Also try to add the following:
self.mpController.moviePlayer.shouldAutoplay = YES;
[self.mpController.moviePlayer play];
Try this snippet :
-(void)setupMovie
{
NSURL *movieURL = [NSURL fileURLWithPath:movieURL];
self.mpController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
self. mpController.scalingMode = MPMovieScalingModeAspectFill;
self. mpController.controlStyle = MPMovieControlStyleNone;
self.mpController.view.frame = self.view.frame;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerPlaybackStateDidChange:) name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification object:nil];
[self.view addSubview:self.mpController.view];
[self.mpController prepareToPlay];
}
- (void)moviePlayBackDidFinish:(MPMoviePlayerController *)player
{
[self.mpController.view removeFromSuperview];
}
- (void)moviePlayerPlaybackStateDidChange:(NSNotification*)notification
{
if (self. mpController.isPreparedToPlay) {
[self. mpController play];
}
}
And make sure movieURL is correct and video exist by this provided url

MPMoviePlayerController never stops loading

Having trouble with MPMoviePlayerController. When I attempt to load the video I just get the activity indicator ir the navbar and it says loading. You see the controllers at the bottom, so the controller loads just not the video. I have copied the link into a browser and the video plays fine. I am using the same code from the app that is already up in iTunes, I am just revamping it. So when I click on the videos in the app that is in iTunes the videos work fine. Just not sure what is going on now here is my code:
-(void)loadVideo {
NSURL *url = [NSURL URLWithString:self.videoUrl];
NSLog(#"url %#", self.videoUrl);
self.movie = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.movie];
self.movie.controlStyle = MPMovieControlStyleDefault;
self.movie.shouldAutoplay = YES;
[self.view addSubview:self.movie.view];
[self.movie setFullscreen:YES animated:YES];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player
respondsToSelector:#selector(setFullscreen:animated:)])
{
[player.view removeFromSuperview];
}
}
Any ideas why this is happening?
Solved the issue. When I used the property self.videoUrl it was a string that had the #"" still attached. I needed to strip that from the string so only the url was left.

Play video from url in objective c with buffering

Hi everyone I am working on an application in which I have a url of video and I have to play a video from that url. I have done this job from this code
- (IBAction)btnPlayVideo:(id)sender
{
NSString *fileName = #"Server Address/Vdieo.flv";
NSURL *streamURL = [NSURL URLWithString:fileName];
mPlayerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:streamURL];
[self.view addSubview:mPlayerVC.view];
//play movie
MPMoviePlayerController *player = [mPlayerVC moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
player.view.frame = self.view.frame;
[player setFullscreen:YES animated:YES];
[self.view addSubview:player.view];
[player prepareToPlay];
[player play];
}
//============Other Methods====================
- (void)willEnterFullscreen:(NSNotification*)notification {
NSLog(#"willEnterFullscreen");
}
- (void)enteredFullscreen:(NSNotification*)notification {
NSLog(#"enteredFullscreen");
}
- (void)willExitFullscreen:(NSNotification*)notification {
NSLog(#"willExitFullscreen");
}
- (void)exitedFullscreen:(NSNotification*)notification {
NSLog(#"exitedFullscreen");
[mPlayerVC.view removeFromSuperview];
mPlayerVC = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)playbackFinished:(NSNotification*)notification {
NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
switch ([reason intValue]) {
case MPMovieFinishReasonPlaybackEnded:
NSLog(#"playbackFinished. Reason: Playback Ended");
break;
case MPMovieFinishReasonPlaybackError:
NSLog(#"playbackFinished. Reason: Playback Error");
break;
case MPMovieFinishReasonUserExited:
NSLog(#"playbackFinished. Reason: User Exited");
NSLog(#"exitedFullscreen");
[[NSNotificationCenter defaultCenter] removeObserver:self];
break;
default:
break;
}
[mPlayerVC.view removeFromSuperview];
mPlayerVC = nil;
}
My problem is that when this code run video player open and start loading and it takes too much time to run a video. Can anybody guide me how to run video in fast way from internet?
There's nothing in the code that suggests that lag is anything other than the time it takes to make the request and sufficiently buffer. The most common technique to improve UE is to start loading as early as possible, even before the user requests playback.
If this is possible, the code should be reorganized as follows:
// hang on to the movie player
#property(nonatomic,retain) MPMoviePlayerController *mp;
// call this as soon as its possible to know the user might want to see the video
- (void)primeVideo {
NSString *fileName = #"Server Address/Vdieo.flv";
NSURL *streamURL = [NSURL URLWithString:fileName];
MPMoviePlayerController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:streamURL];
// do this also in dealloc
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
mp.shouldAutoplay = NO;
[mp prepareToPlay];
self.mp = mp;
}
That does as much prep as possible without changing the UI. The rest of your code from the button action is left, tweaked a little...
- (IBAction)btnPlayVideo:(id)sender {
// if there's any way that the user can request playback before
// you've called primeVideo, check for that here. But hopefully you
// can call primeVideo before user even sees the play button
if (!self.mp) [self primeVideo];
self.mp.view.frame = self.view.frame;
[self.mp setFullscreen:YES animated:YES];
[self.view addSubview:self.mp.view];
MPMovieLoadState state = [self.mp loadState];
if (state & MPMovieLoadStatePlaythroughOK) [self.mp play];
else self.mp.shouldAutoplay = YES;
}

How to Open quicktime player on iPad

I am programming an iPad app. In this app I want to open a movie URL in default Quicktime player. When I tried to open the URL in the browser the movie starts playing in the browser. How can I open the movie in the default player (so i can get play pause controls..)
Any help would be much appreciated.
Thanks
Saurabh
Finally I think it is not possible to open the quicktime player on iPad. So I searched on google and found MPMoviePlayerViewController class, which solves my problem quite well.
- (void)viewDidLoad {
NSString *url = [[NSBundle mainBundle]
pathForResource:#"Stock_Footage_Demobroadband"
ofType:#"mp4"];
MPMoviePlayerViewController *playerViewController =
[[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[playerViewController moviePlayer]];
[self.view addSubview:playerViewController.view];
//---play movie---
MPMoviePlayerController *player = [playerViewController moviePlayer];
[player play];
[super viewDidLoad]; }
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player stop];
[self.view removeFromSuperView];
[player autorelease]; }
I have posted a complete tutorial my blog http://www.makebetterthings.com/blogs/iphone/play-video-on-iphone-and-ipad/

Why is it not possible to use the MPMoviePlayerController more than once?

In MonoTouch, we ran into this problem with the Movie Player sample in that it would only play the video once, but would not play it a second time.
I am asking this question to post an answer, since it has been hitting various folks.
MPMoviePlayerController is a singleton underneath the hood. If you have not properly release'd (ObjC) or Dispose()'d (MonoTouch) and you create a second instance, it will either not play, or play audio only.
Additionally if you subscribe to MPMoviePlayerScalingModeDidChangeNotification or MPMoviePlayerPlaybackDidFinishNotification or MPMoviePlayerContentPreloadDidFinishNotification, be warned that the posted NSNotification takes a reference to the MPMoviePlayerController as well, so if you keep it around, you will have a reference the player.
Although Mono's Garbage Collector will eventually kick-in, this is a case where deterministic termination is wanted (you want the reference gone now, not gone when the GC decides to perform a collection).
This is why you want to call the Dispose () method on the controller, and the Dispose() method on the notification.
For example:
// Deterministic termination, do not wait for the GC
if (moviePlayer != null){
moviePlayer.Dispose ()
moviePlayer = null;
}
If you were listening to notifications, call Dispose in your notification handler at the end, to release the reference that it keeps to your MPMoviePlayerController for example:
var center = NSNotificationCenter.DefaultCenter;
center.AddObserver (
"MPMoviePlayerPlaybackDidFinishNotification"),
(notify) => { Console.WriteLine ("Done!"); notify.Dispose (); });
Can't see your code Nir and I don't have edit privileges so here it is again:
The secret lays in the endPlay with setting the: moviePlayer.initialPlaybackTime = -1; before releasing it. Try it out : :)
-(void)playMovie:(NSString *)urlString{ movieURL = [NSURL URLWithString:urlString];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayer.initialPlaybackTime = 0;
//Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(endPlay:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
moviePlayer.movieControlMode = MPMovieControlModeDefault;
moviePlayer.backgroundColor = [UIColor blackColor];
[moviePlayer play];
}
-(void)endPlay: (NSNotification*)notification{
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
moviePlayer.initialPlaybackTime = -1;
[moviePlayer stop];
[moviePlayer release];
}
The secret lays in the endPlay with setting the: moviePlayer.initialPlaybackTime = -1;
before releasing it.
Try it out :
:)
-(void)playMovie:(NSString *)urlString{
movieURL = [NSURL URLWithString:urlString];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayer.initialPlaybackTime = 0;
//Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(endPlay:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
moviePlayer.movieControlMode = MPMovieControlModeDefault;
moviePlayer.backgroundColor = [UIColor blackColor];
[moviePlayer play];
}
-(void)endPlay: (NSNotification*)notification{
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
moviePlayer.initialPlaybackTime = -1;
[moviePlayer stop];
[moviePlayer release];
}

Resources