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
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];
}
}
}
I want to play video on clicking a button. My AVPlayerViewController is open but not playing an video .Please help.
- (IBAction)btnPlay_Click:(id)sender {
NSURL *videoURL = [NSURL URLWithString:#"https://player.vimeo.com/video/143506410"];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
[self presentViewController:playerViewController animated:YES completion:nil];
}
If you are using vimeo video you can use third party tools which helps you load the vimeo video.
Solution 1: Please try this, It works for me, just some lines of code.
- (void)viewDidLoad
{
[super viewDidLoad];
vimeoHelper = [[VimeoHelper alloc] init];
[vimeoHelper getVimeoRedirectUrlWithUrl:#"http://vimeo.com/52760742" delegate:(id)self];
}
- (void)finishedGetVimeoURL:(NSString *)url
{
_moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:url]];
[self presentViewController:_moviePlayerController animated:NO completion:nil];
}
Solution 2: YTVimeoExtractor
May be it will help you.
Use MPMoviePlayerController and add this line of code:
NSURL *videoURL = [NSURL URLWithString:#"https://player.vimeo.com/video/143506410"];
MPMoviePlayerViewController *player =[[MPMoviePlayerViewController alloc] initWithContentURL: videoURL];
[self.view addSubview: [player view]];
[self presentMoviePlayerViewControllerAnimated:player];
[player.moviePlayer prepareToPlay];
Add one more line : [self.view addSubview: [player view]];
movieController is an instance of MPMoviePlayerViewController declared in the .h file.
[movieController.moviePlayer play] is not required and the player will start regardless if you didn't set autoplay to NO, but I observed that if you put play in it it starts a bit quicker. This could be just a coincidence.
--------- If this is not working you can just do that following ----------
player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[player.view setFrame:CGRectMake(0, 0, 320, 320)];
[player play];
[self.view addSubview:player.view];
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];
}
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
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 ;