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];
}
Related
I wanted to play a video as background of my view so I decided to use MPMoviePlayerController to play endless video and I made something like that:
NSString *pathForFile = [[NSBundle mainBundle] pathForResource:#"clear" ofType:#"mp4"];
player = [[MPMoviePlayerController alloc] init];
player.shouldAutoplay = YES;
player.repeatMode = MPMovieRepeatModeNone;
player.fullscreen = YES;
player.movieSourceType = MPMovieSourceTypeFile;
player.scalingMode = MPMovieScalingModeAspectFill;
player.contentURL =[NSURL fileURLWithPath:pathForFile];
player.controlStyle = MPMovieControlStyleNone;
[player.view setFrame:self.view.bounds];
[player.view setUserInteractionEnabled:NO];
[player.view setAlpha:0.0f];
[self.view addSubview:player.view];
[self.view sendSubviewToBack:player.view];
[player prepareToPlay];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
- (void)moviePlayerDidFinish:(NSNotification *)note {
if (note.object == player) {
NSInteger reason = [[note.userInfo objectForKey:#"MPMoviePlayerPlaybackDidFinishReasonUserInfoKey"] integerValue];
if (reason == MPMovieFinishReasonPlaybackEnded) {
[player play];
}
}
My method called every time the video ends, but player don't playing video again. What am I doing wrong?
Thanks in advance.
- (void)moviePlayerDidFinish:(NSNotification *)note {
if (note.object == player) {
NSInteger reason = [[note.userInfo objectForKey:#"MPMoviePlayerPlaybackDidFinishReasonUserInfoKey"] integerValue];
if (reason == MPMovieFinishReasonPlaybackEnded) {
player.seekToTime[kCMTimeZero];
// you must have to give time to start video again so use seekToTime
[player play];
}
}
}
My application capture video. I have checked that video file has a sound but in application it is not playing with MPMoviePlayerController. I have noticed that for short video, less than 5 seconds, sound is playing.
-(void) showMovie:(NSString*)moviePath{
[self initializeMoviePlayer:moviePath];
[self setMoviePath:moviePath];
[movieController play];
}
-(void) initializeMoviePlayer:(NSString*)moviePath{
if (movieController == nil) {
movieController = [[MPMoviePlayerController alloc] initWithContentURL:[[NSURL alloc] initFileURLWithPath:moviePath]];
movieController.repeatMode = MPMovieRepeatModeOne;
[movieController prepareToPlay];
[movieController setUseApplicationAudioSession:NO];
[movieController setShouldAutoplay:YES];
[movieController setControlStyle:2];
movieController.fullscreen = YES;
movieController.scalingMode =MPMovieScalingModeAspectFill;
movieController.controlStyle = MPMovieControlStyleNone;
movieController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight |UIViewAutoresizingFlexibleWidth;
[movieController.view setFrame: self.frame];
[self insertSubview:self.movieController.view atIndex:0];
}
}
Update:
Have added a bug in apple bug reporter with id 18823656
I want to play video automatically without on click of play button.
So i found a property of MPMovieplayerController "BOOl Shouldautoplay"
But don't know how to use it.
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:fileName]];
moviePlayer.initialPlaybackTime = 0;
moviePlayer.view.frame = self.view.frame ;
[self.view addSubview:moviePlayer.view] ;
moviePlayer.shouldAutoplay = YES;
[moviePlayer prepareToPlay];
Hope it helps
this is another way
-(void) mpVideo:(NSString *)fileName {
videoFileName=fileName;
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:fileName]];
moviePlayer.initialPlaybackTime = 0;
moviePlayer.view.frame = self.view.frame ;
[orientationHandler VideoStart];
[self.view addSubview:moviePlayer.view] ;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.shouldAutoplay = NO;
[moviePlayer prepareToPlay];
}
-(void)moviePlaybackDidFinish:(NSNotification *)notification {
// your custom code which you want to play after the player did finish playing
}
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
Making an app for my church and need mp3 file to play after being downloaded from xml. NSLog displays the url correctly, but when I run it, I just get a spinning "loading" and a black box. Any suggestions as to why?
Thanks!
-(void)viewDidAppear:(BOOL)animated
{
RSSItem* item = (RSSItem*)self.detailItem;
self.title = item.title;
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:item.link];
NSLog(#"The url is %#", item.link);
moviePlayerController.view.frame = CGRectMake(73, 203, 640, 360);
[self.view addSubview:moviePlayerController.view];
[moviePlayerController prepareToPlay];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
TRY this one..
RSSItem* item = (RSSItem*)self.detailItem;
self.title = item.title;
if(item.link)
{
//NSURL *url = [NSURL fileURLWithPath:item.link];
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:item.link];
moviePlayer.view.frame = CGRectMake(0, 0, 500, 500);
moviePlayer.moviePlayer.shouldAutoplay=YES;
moviePlayer.moviePlayer.controlStyle = MPMediaTypeMusicVideo;
[moviePlayer.moviePlayer setFullscreen:YES animated:YES];
[self.view addSubview:moviePlayer.view];
[moviePlayer.moviePlayer play];
}
else
{
NSLog(#"no url");
}