I'm working on an iPad project where i have to play short video files one after another smoothly. For playing the videos i'm using MPMoviePlayerController. The problem i'm facing is that when i call
[self.moviePlayer setContentURL:videoURL]
it does start the next video, but there is ±1 sec delay of black screen before it starts to play the next video (the videos are read from the disk, not streamed). I need to avoid this black screen as well as the delay.
So maybe some of you also experienced this problem and have some solutions? Thanks.
Btw, for now, as to at least avoid the black screen, I capture the last frame of the ending video, show it in a UIImageView, and remove it after 1 sec delay. But i'm hoping to find a more elegant fix.
The effect you are talking about is actually a combination of two problems: a black blink when you change the video (which doesn't happen upon assigning the video for the first time) and the delay before the controller starts playing video.
I'm currently screwed with the second one and don't know how to solve yet. As for the first one, just try to use another instance of MPMoviePlayerController. I mean when a video finishes playing (you can subscribe to a corresponding notification) just remove the old player, create a new one and put video there. This way you will avoid blinking, but there will be a delay (not sure, because of loading the video or because of player creation) before the next video starts playing.
Hope this helps a bit.
Fond solution here
http://joris.kluivers.nl/blog/2010/01/04/mpmovieplayercontroller-handle-with-care/
you need to use [self.moviePlayer prepareToPlay]; and catch MPMoviePlayerReadyForDisplayDidChangeNotification to use [self.moviePlayer play];
Old post but Googlers will still come. :)
Creating a new MPMoviePlayerController then assigning it back to my previous player worked for me, no more black screen!
...
[self playVideoWithFilename:#"video1.mp4"];
}
- (void)playVideoWithFilename:(NSString *)fileName
{
MPMoviePlayerController *player = [MPMoviePlayerController new];
_myVidPlayer = player;
player = nil;
NSURL *vidPath = [[NSBundle mainBundle] URLForResource:fileName withExtension:nil];
[_myVidPlayer.view setBackgroundColor:[UIColor whiteColor]];
[_myVidPlayer.view setFrame:CGRectMake(0, 64, 320, 320)];
[_myVidPlayer setContentURL:vidPath];
[_myVidPlayer setControlStyle:MPMovieControlStyleNone];
[_myVidPlayer setRepeatMode:MPMovieRepeatModeOne];
[_myVidPlayer prepareToPlay];
[self.view addSubview: _myVidPlayer.view];
[_myVidPlayer play];
}
Note:
Available in iOS 2.0 and later
Deprecated in iOS 9.0
"Use AVPlayerViewController in AVKit."
I think that the problem is that the controller will fade out and back in between the movies.
You can control the background view color and contents, but I'm not sure that you can eliminate the fade in/out.
Related
In one of my ViewControllers, I have an AVPlayerViewController. In my viewDidLoad I have
self.videoPlayerController = [AVPlayerViewController new];
Then, I play a video using the following method
- (void)playVideo:(NSURL*)videoURL {
[self presentViewController:self.videoPlayerController animated:YES completion:^(){
AVPlayer *player = [[AVPlayer alloc] initWithURL:videoURL];
[self.videoPlayerController setVideoGravity:AVLayerVideoGravityResizeAspect];
[self.videoPlayerController setPlayer:player];
[self.videoPlayerController.player play];
}];
}
When playing a video for the first time, the AVPlayerViewController appears, but the video is stuck on the first frame, and will not play. No audio plays either.
In iOS 10.0, the seekbar in the on screen controls show that the video is playing (it will progress to the end). In iOS 10.2 however, the seekbar is stuck at 0:00 as well.
I am experiencing this problem on iPads with iOS versions 10.0 and 10.2, however, it works in the XCode simulator (both 10.0 and 10.2).
My problem is similar in nature to this.
Also, the video plays correctly if you close the ViewController containing the AVPlayerViewController, reopen it, and attempt to play the video again.
I guess that the problem is in your video file. Try to download another video mp4 video sample. Add it to your project and try.
After making some simple changes to an app, the MPMoviePlayerController has begun stuttering when playing videos. The problem only happens about 10% of the time, and so far I haven't discovered any repeatable pattern. The stutter just happens at the beginning: the video starts, and then after about a second, it restarts. That's it.
Here's my setup code, as called from viewDidLoad:
self.moviePlayerController=[[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[self.moviePlayerController setControlStyle:MPMovieControlStyleNone];
[self.moviePlayerController setShouldAutoplay:NO];
[self.moviePlayerController prepareToPlay];
[self.moviePlayerController.view setAlpha:0];
[self.moviePlayerController.backgroundView setBackgroundColor:[UIColor whiteColor]];
And then when the user taps a big play button:
[self.moviePlayerController play];
I've combed through all the code many times, setup up many NSLogs and breakpoints, and don't appear any closer to solving the problem. Why has this started happening? Has anyone else had this issue with MPMoviePlayerController? Thanks for reading.
I ended up swapping out MPMoviePlayerController for AVPlayerViewController. Pretty straight-forward to do, and the stutter is now gone.
I'm finding it difficult to search for this, background video searches for iOS typically refer to multitasking and switching apps with video.
I want to know if it's possible to have a video playing full screen without movie player controls, while displaying another view on top of that with logo, buttons, text, etc...
So,
Background: 15 second video loop -
Foreground: login / signup buttons, logo, etc...
There are a number of options, but the simplest will be to use a MPMoviePlayerController, with its repeatMode property set to loop forever, and its controlStyle set to none. That will give you a view with a looped movie with no controls that you can use as a background by adding it to your view hierarchy.
OP here - I was able to display a video full screen using this code (in viewDidLoad):
(don't forget to substantiate player as a property with MPMoviePlayerController *player;)
player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"videoname" ofType:#"m4v"]]];
[player.view setFrame: self.view.bounds];
player.repeatMode = MPMovieRepeatModeOne;
player.controlStyle = MPMovieControlStyleNone;
[myView addSubview:player.view];
[player play];
And that worked fine - it displays a video fullscreen without video controls. What I had a problem with was displaying buttons and graphics above that. Even though my view hierarchy had the player.view seemingly behind my view with buttons, I couldn't get the buttons to appear.
I fixed that by adding in:
[player.view addSubview:overlayView];
Just set the layer you intend to display above the video to an IBOutlet and then use the video view to add that view (overlayView in this case) as a subview.
I actually prefer a way that is portrayed in this tutorial
basically the idea is to create a gif of your video, crop it to iPhone Screen size and load it into an un-interactive UIWebView. I believe this will get the job done if you don't need any audio, and it's basically done effortlessly.
I hope it helps you =)
I am trying to have MPMoviePlayerController reach a programmed endPlaybackTime, then reassign the initial, current, and end times and "resume" play
So first play, say, from the start to 4 seconds, stop/pause, then resume and play from 4 to 8, etc...
but after I reassign current, initial, and endPlaybackTimes and run [mplayer play], the video restarts from the originally times (start to 4s) and plays to the original end time, even though debug messages confirm the new times after the second play
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
_mplayer3 = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
_mplayer3.controlStyle = MPMovieControlStyleNone;
[_mplayer3.view setFrame: self.view.bounds];
[self.view insertSubview:_mplayer3.view belowSubview:_TopBrag];
_mplayer3.endPlaybackTime = 4.0;
[_mplayer3 setShouldAutoplay:NO];
[_mplayer3 prepareToPlay];
[_mplayer3 view].userInteractionEnabled=YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(parallaxDownDidFinish)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_mplayer3];
[_mplayer3 play];
on the end notification, there's a gesture recognizer is created that appears to work correctly. in the gesture recognizer:
_mplayer3.initialPlaybackTime=_mplayer3.endPlaybackTime;
_mplayer3.currentPlaybackTime=_mplayer3.endPlaybackTime;
_mplayer3.endPlaybackTime+=4.0;
if (_mplayer3.endPlaybackTime > _mplayer3.duration)
_mplayer3.endPlaybackTime = _mplayer3.duration;
[_mplayer3 play]
and the video plays from 0 to 4 instead of 4 to 8, even though NSLogs after the play suggest the times are what's desired
appreciate any help
MPMoviePlayerController does not adhere to the initialPlaybackTime if not used on a fresh instance. You will need to release and realloc/assign the player to get this working. All you need to do is to reuse the initial code shown in your question once the player aught to continue.
Update:
Since you seem to be keen on keeping the player view active and as you are not using the standard user interface, I would suggest you to use AVPlayer instead. It is much more flexible and for your job it seems to be the right choice then.
I'm not sure whats the problem, because it should work, the problem i think is that you are in IOS 6, IOS 6 doesn't let you make such a small change, but if in your swipe you change the initial playtime to 9 or higher it should work....
try it and then let me know ;)
I have an MPMoviePlayerController where I load a video from a URL. In iOS 3.2.2 the video started downloading when I added it to a view, but it didn't play until I hit the play button (which is what I want). However, since iOS 4.2.1 came out, it started behaving differently; the video starts downloading and plays automatically.
Here's how I load my MPMoviePlayerController:
MPMoviePlayerController *player = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL URLWithString:theVideo.fileUrl]];
player.view.frame = articleVideoFrame;
[mainView addSubview:player.view];
I even tried to perform a [player pause]; after the addSubview part, but it still plays automatically. Could you guys help me with this one?
Got it! I used player.shouldAutoplay = NO; and that did the trick. Documentation says it is by default YES, which explains it all. Probably in 3.2 default was NO, but has been switched in 4.2.
I have noticed some other behavior changes in video playback in 4.2.1...namely the video player does not become visible until it starts receiving the data for the movie...
In the previous versions it used to come up instantaneously with "Loading movie..." text on top.
Sometimes, the player gets stuck in when not in full screen mode, with no Done button available or not responding to touches on Pause and Zoom...
I am having other more subtle issues with the playback but I can't isolate the issue just yet...