*I'm creating an iPhone app where we can watch videos...
I,ve been making my own controls so I want to implement the next, back buttons, I can show theme so for the UI everything is ok the problem is to restart the MoviePlayer with another content...
Any idea???
I could use the same configuration when the playback did finish, I mean... If the video ends playing to start playing another one...
I've tried to set contentUrl but it trows an exception:
2012-04-17 11:37:41.198 NexTest2[8218:11f03] *** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan 11.5]'
viewController.m
- (void)viewDidLoad{
[super viewDidLoad];
fileURL = [NSURL URLWithString:urlString];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
moviePlayerController.controlStyle = MPMovieControlStyleNone;
[moviePlayerController setShouldAutoplay:YES];
[self addObservers];
/* Inset the movie frame in the parent view frame. */
CGRect viewInsetRect = CGRectInset ([self.view bounds],0.0, 0.0 );
[[moviePlayerController view] setFrame: viewInsetRect ];
[self.view addSubview:moviePlayerController.view];
[self resizeControlViews];
}
then in resizeControlViews I have this, cause I'm using these two views like controls:
-(void)resizeControlViews{
CGRect barFrame = self.controlBarView.frame;
barFrame.origin.x = round((moviePlayerController.view.frame.size.width - barFrame.size.width) / 2.0);
barFrame.origin.y = round((moviePlayerController.view.frame.size.height - barFrame.size.height)/10.0);
self.controlBarView.frame = barFrame;
[moviePlayerController.view addSubview:controlBarView];
CGRect playFrame = self.controlPlaybackView.frame;
playFrame.origin.x = round((moviePlayerController.view.frame.size.width - playFrame.size.width) / 2.0);
playFrame.origin.y = round((moviePlayerController.view.frame.size.height - playFrame.size.height)/1.1);
self.controlPlaybackView.frame = playFrame;
[moviePlayerController.view addSubview:controlPlaybackView];
}
Here everything is working fine, in one of these views there is a slider who controls the seeking, and in the other one are the play, next and back buttons.
I think maybe the problem is whit the UI components of these views...
I've added these observers to control the playback:
//Add the observers to the player
-(void)addObservers{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackStarted:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(setDurationLabel:) name:MPMovieDurationAvailableNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyWindowChanged:) name:UIWindowDidBecomeKeyNotification object:nil];
}
so to handle when the player is playing I use the MPMoviePlayerPlaybackStateDidChangeNotification with this selector:
//called when the playback state changes of state
- (void)playbackStarted:(NSNotification*)notification {
MPMoviePlayerController *player = notification.object;
if (player.playbackState == MPMoviePlaybackStatePlaying) {
[self timerRunning];
}
}
and if the player is "playing" I monitor it with this method:
-(void)timerRunning{
self.currentTime.text = [self floatToStringTime:(moviePlayerController.currentPlaybackTime)];
self.timeBar.value = moviePlayerController.currentPlaybackTime / moviePlayerController.duration;
if (moviePlayerController.playbackState == MPMoviePlaybackStatePlaying) {
[self performSelector:#selector(timerRunning) withObject:nil afterDelay:0.5];
}
}
so Maybe here is the issue... but with a single video it works perfectly...
I've four where the mistake is... is while i'm monitoring in the line to set the value to the slider, but I really don't know why, I think it is cause when another video or stoping video it is still executed so the player has not duration or current playback what trows this exception... how could I to solve it... here is the mistake... maybe moving this code to another part...
self.timeBar.value = moviePlayerController.currentPlaybackTime / moviePlayerController.duration;
Check your UI related code. You most likely are not properly checking the player status before getting and using content related properties from it.
Well finally it works fine... the setCurrentContentUrl works fine, the problem was my code so I solved it... I just validate that duration was different to 0...
Now I have a player with full custom controls :) cool :)
-(void)timerRunning{
if (moviePlayerController.playbackState == MPMoviePlaybackStatePlaying) {
self.currentTime.text = [self floatToStringTime:(moviePlayerController.currentPlaybackTime)];
if (moviePlayerController.duration != 0) {
self.timeBar.value = moviePlayerController.currentPlaybackTime / moviePlayerController.duration;
}
[self performSelector:#selector(timerRunning) withObject:nil afterDelay:0.5];
}
}
Thank you anyways
Related
NOTE: See the updates at the bottom.
I have an application to play videos one by one from a list. So, to test this functionality, I created a simple application with only one view controller. I referenced this blog before implementing this view controller. The view controller is named TNViewController and its implementation is as follows:
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface TNViewController : UIViewController {
#private
NSMutableArray *_videoArray;
int _currentVideo;
MPMoviePlayerController *_moviePlayer;
NSURL *_movieUrl;
}
#end
Its implementation is:
#import "TNViewController.h"
#implementation TNViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
[self.view setFrame:CGRectMake(0, 0, 480, 320)];
[self initVideos];
[self initPlayer];
}
- (void) initVideos {
_videoArray = [[NSMutableArray alloc] init];
NSString *path = [[NSBundle mainBundle] pathForResource:#"sintel_trailer" ofType:#"mp4" inDirectory:nil];
[_videoArray addObject:path];
path = [[NSBundle mainBundle] pathForResource:#"elephants_dream_trailer" ofType:#"mp4" inDirectory:nil];
[_videoArray addObject:path];
path = [[NSBundle mainBundle] pathForResource:#"big_buck_bunny_trailer" ofType:#"mp4" inDirectory:nil];
[_videoArray addObject:path];
_currentVideo = -1;
}
- (NSString*) nextVideo {
_currentVideo++;
if (_currentVideo >= _videoArray.count) {
_currentVideo = 0;
}
return [_videoArray objectAtIndex:_currentVideo];
}
- (void) initPlayer {
_moviePlayer = [[MPMoviePlayerController alloc]init];
[self readyPlayer];
[self.view addSubview:_moviePlayer.view];
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayer];
}
- (void) readyPlayer {
_movieUrl = [NSURL fileURLWithPath:[self nextVideo]];
[_movieUrl retain];
_moviePlayer.contentURL = _movieUrl;
// For 3.2 devices and above
if ([_moviePlayer respondsToSelector:#selector(loadState)]) {
// Set movie player layout
[_moviePlayer setControlStyle:MPMovieControlStyleNone];
[_moviePlayer setFullscreen:YES];
// May help to reduce latency
[_moviePlayer prepareToPlay];
// Register that the load state changed (movie is ready)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
} else {
// Register to receive a notification when the movie is in memory and ready to play.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePreloadDidFinish:)
name:MPMoviePlayerContentPreloadDidFinishNotification
object:nil];
}
}
/*---------------------------------------------------------------------------
* For 3.1.x devices
*--------------------------------------------------------------------------*/
- (void) moviePreloadDidFinish:(NSNotification*)notification {
// Remove observer
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerContentPreloadDidFinishNotification
object:nil];
// Play the movie
[_moviePlayer play];
}
/*---------------------------------------------------------------------------
* For 3.2 and 4.x devices
*--------------------------------------------------------------------------*/
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification {
NSLog(#"moviePlayerLoadStateChanged");
// Unless state is unknown, start playback
if ([_moviePlayer loadState] != MPMovieLoadStateUnknown) {
// Remove observer
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
// Set frame of movie player
[[_moviePlayer view] setFrame:CGRectMake(0, 0, 480, 320)];
// Play the movie
[_moviePlayer play];
}
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
NSLog(#"playback finished...");
NSLog(#"End Playback Time: %f", _moviePlayer.endPlaybackTime);
int reason = [[[notification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
if (reason == MPMovieFinishReasonPlaybackEnded) {
NSLog(#"Reason: movie finished playing");
}else if (reason == MPMovieFinishReasonUserExited) {
NSLog(#"Reason: user hit done button");
}else if (reason == MPMovieFinishReasonPlaybackError) {
NSLog(#"Reason: error");
}
[self playNextVideo];
}
- (void) playNextVideo {
NSString *filePath = [self nextVideo];
[_movieUrl release];
_movieUrl = [NSURL fileURLWithPath:filePath];
[_movieUrl retain];
_moviePlayer.contentURL = _movieUrl;
[_moviePlayer play];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (void) dealloc {
[_moviePlayer release];
[_movieUrl release];
[_videoArray release];
[super dealloc];
}
#end
Now, my problem is that the notification MPMoviePlayerPlaybackDidFinishNotification is called twice. As you can see from the above code, I have registered for it only once in the viewDidLoad(in initPlayer called from viewDidLoad). Here is the log output:
2012-07-02 12:29:17.661 DemoApp[1191:ef03] moviePlayerLoadStateChanged
2012-07-02 12:30:11.470 DemoApp[1191:ef03] playback finished...
2012-07-02 12:30:11.471 DemoApp[1191:ef03] End Playback Time: -1.000000
2012-07-02 12:30:11.472 DemoApp[1191:ef03] Reason: movie finished playing
2012-07-02 12:30:11.474 DemoApp[1191:ef03] playback finished...
2012-07-02 12:30:11.475 DemoApp[1191:ef03] End Playback Time: -1.000000
2012-07-02 12:30:11.476 DemoApp[1191:ef03] Reason: movie finished playing
2012-07-02 12:31:03.821 DemoApp[1191:ef03] playback finished...
2012-07-02 12:31:03.822 DemoApp[1191:ef03] End Playback Time: -1.000000
2012-07-02 12:31:03.824 DemoApp[1191:ef03] Reason: movie finished playing
2012-07-02 12:31:03.826 DemoApp[1191:ef03] playback finished...
2012-07-02 12:31:03.827 DemoApp[1191:ef03] End Playback Time: -1.000000
2012-07-02 12:31:03.827 DemoApp[1191:ef03] Reason: movie finished playing
As you can see, the playback finished is called twice. This causes one video to be skipped from the queue. (In fact, in original project where the problem occures, nextVideo caches a video in advance from the server, and returns the path to the cached video, if it exists in the cache. Otherwise, it returns nil.). Here, first the sintel_trailer.mp4 is played. After it finishes playback, instead of elephants_dream_trailer.mp4, it plays big_buck_bunny_trailer.mp4. That is, it cycles plays the videos skipping on in between. So, what is causing the MPMoviePlayerPlaybackDidFinishNotification to invoke twice? I am working on this for two days, still no luck. Any idea?
UPDATE 1:
Currently I am using a switch in the callback moviePlayBackDidFinish: like below and is working:
if (!_playNextVideo) {
_playNextVideo = YES;
return;
}
_playNextVideo = NO;
// code to play video....
But still I would like to know what causes the callback being called twice. I feel the current solution of switch like a hack, and like to remove it.
UPDATE 2:
Until now, I have been trying this with iPhone 4.3 simulator. But, when I tried the same program with iPhone 5.0 simulator and iPhone 5.1 simulator, it works without any problem. That is, only one callback is being sent after movie finished playing. And that renders my little hack(on update 1) useless (it solves the problem in 4.3 but creates problem in 5.0 and 5.1). I am using Xcode 4.3.2 running on MacOSX Lion - 10.7.4. Do you have any idea on how to solve this problem? Why two callbacks on 4.3?
UPDATE 3:
I pinpoint to the line causes problem. It is in playNextVideo method. The line causes problem is _moviePlayer.contentURL = _movieUrl;. Changing it in the first callback causes, the MPMoviePlayerPlaybackDidFinishNotification to be sent again. But, it happens only in iPhone 4.3 simulator. Any idea?
UPDATE 4:
Still, I haven't got any idea on this weird behavior. So, I am now using a time trick like the one in UPDATE 1 as follows on moviePlayBackDidFinish:
NSTimeInterval currentCallback = [NSDate timeIntervalSinceReferenceDate];
NSTimeInterval difference = currentCallback - _lastCallback;
_lastCallback = currentCallback;
if (difference < 5.0) {
return;
}
// code to play video....
I had the same problem. and solved it this way:
I created a new method to skip a video
- (void) skipVideo {
[_moviePlayer stop];
}
Stopping the player in skipVideo will cause a MPMovieFinishReasonPlaybackEnded notification (in simulator and on device). When setting contentUrl of player now, no other MPMovieFinishReasonPlaybackEnded notification is caused, so moviePlayBackDidFinish is called only once;
Before playing next video (in playNextVideo) you have to call
[_moviePlayer prepareToPlay];
That works fine for me!
You can create new player for next track:
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: _movieUrl];
if (player)
{
[self setMoviePlayer:player];
}
[self.moviePlayer play];
Instead of
self.moviePlayer.contentURL = _movieUrl;
Notification MPMoviePlayerPlaybackDidFinishNotification will called once.
I have encountered a problem with the MPMoviePlayerController in 3.1.2.
If I cancel the player while it is still loading, the player closes. However, the video starts playing a few moments later in the background. The only ways to stop it are to play another video or close the app. This seems to work fine in 3.2+.
Here's what I'm doing:
- (void)loadMoviePlayer
{
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
if ([NSClassFromString(#"MPMoviePlayerController") instancesRespondToSelector:#selector(view)])
{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
// running iOS 3.2 or better
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:#"http://www.mysite.com/myvideo.m3u8"]];
[moviePlayer.view setBackgroundColor:[UIColor blackColor]];
[moviePlayer.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
// [moviePlayer.moviePlayer setControlStyle:MPMovieControlStyleNone];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
[moviePlayer.moviePlayer prepareToPlay];
[moviePlayer.moviePlayer play];
#endif
}
else
{
MPMoviePlayerController *mMPPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:#"http://www.mysite.com/myvideo.m3u8"]];
mMPPlayer.scalingMode=MPMovieScalingModeFill;
mMPPlayer.backgroundColor=[UIColor blackColor];
[mMPPlayer play];
}
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
[[UIApplication sharedApplication] setStatusBarHidden:NO];
// Remove observer
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[self dismissModalViewControllerAnimated:YES];
}
I added moviePlayBackDidFinish this morning. It gets called when I hit cancel, but dismissModalViewControllerAnimated doesn't seem to do anything. I also tried removeFromSuperView, but my player will not respond.
So, how can I make sure the player does not play after hitting "cancel"?
Thanks in advance.
You may have come across an old bug in MPMoviePlayerController. Back in the days, we actually had to play an almost empty (black, silence) M4V after playing proper content to be sure the player does not attempt to continue playback in the background when stopping at certain stages. That bug manifests in audible sound but no picture of the aborted/stopped video.
There are however a few more things worth trying when stopping (assuming your instance of MPMoviePlayerController is called moviePlayer);
set the current playback position to the complete movie duration moviePlayer.currentPlaybackTime = moviePlayer.duration;
send another stop within your notification handler [moviePlayer stop];
In my case, I found that setting the following line would eventually stop the movie player from playing:
moviePlayer.contentURL = nil;
(with moviePlayer your instance of MPMoviePlayerController).
I am trying to get an embedded video playback properly implemented within a tab-bar navigation. In my specific case, the video shall be displayed in a non-fullscreen manner on a UIView that is hosted by a UIViewController, managed by a UITabBarController.
For simplifying the example, lets say I have two tabs within my tabbar. First one shows some random stuff, second one shows the viewcontroller that hosts the embedded video.
Once the user selects the second tab, the video is loading and playing properly.
For initializing the player, I am using the following code from within my UIView derived class, triggered by the initializer (initWithFrame):
- (void)initPlayback
{
self.movieViewController = [[MPMoviePlayerViewController alloc] init];
movieViewController_.wantsFullScreenLayout = NO;
movieViewController_.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
[self addSubview:self.movieViewController.view];
}
For starting the playback, I am using the following code, triggered by the viewWillAppear method of my UIViewController derived class:
- (void)playVideo
{
[movieViewController_.moviePlayer setContentURL:fileURL_];
}
If then, the user selects the first tab (while the video is still playing), I am making sure that the video is stopped as it would continue playing if that was not done:
- (void)stopVideo
{
[movieViewController_.moviePlayer stop];
}
Once the user selects the second tab again, the view stays blank, nothing is loaded or played even though the playVideo-method is invoked.
What am I missing, why is the video playback failing when reselecting the second tab?
==========new attempt=============
This time I stopped relying on shouldAutoplay (as suggested) but that did not make a difference.
Adapted and added code for this;
- (void)MPMoviePlayerLoadStateDidChange:(NSNotification *)notification
{
if (movieViewController_.moviePlayer.loadState == MPMovieLoadStatePlayable &&
movieViewController_.moviePlayer.playbackState != MPMoviePlaybackStatePlaying)
{
[movieViewController_.moviePlayer play];
}
}
- (void)deregisterFromNotifications
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
}
- (void)registerForNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(MPMoviePlayerLoadStateDidChange:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
}
- (void)initPlayback
{
NSLog(#"playback init...");
self.movieViewController = [[MPMoviePlayerViewController alloc] init];
movieViewController_.wantsFullScreenLayout = NO;
movieViewController_.moviePlayer.shouldAutoplay = NO;
movieViewController_.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
movieViewController_.moviePlayer.currentPlaybackTime = 0.0f;
[self addSubview:movieViewController_.view];
}
- (void)playVideo
{
NSLog(#"playback starting...");
[self registerForNotifications];
[movieViewController_.moviePlayer setContentURL:fileURL_];
}
- (void)stopVideo
{
NSLog(#"playback stopping...");
[movieViewController_.moviePlayer stop];
[self deregisterFromNotifications];
}
In playVideo I think it should be
[movieViewController_.moviePlayer setContentURL:fileURL_];
[movieViewController_.moviePlayer play];
I assume that it works the first time because autoplay defaults to YES
MPMoviePlayerViewController is a subclass of UIViewController. If the second tab is dedicated to displaying video why not just use an instance of it as the root view controller for the second tab?
Adding [movieViewController_.moviePlayer prepareToPlay] to my playVideo method does the trick (when working with remote streams).
I have following code that I use to play a selected portion of a movie clip. The movie plays okay. But the scrubber bar does not reflect the correct start stop time. Also, the forward and rewind buttons take the clip beyond the segment that was specified. Why is this and can we fix it?
TIA.
MPMoviePlayerViewController *mp =
[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:movieFile] ];
[mp.moviePlayer setInitialPlaybackTime:21];
[mp.moviePlayer setEndPlaybackTime:48];
[self presentMoviePlayerViewControllerAnimated:mp];
[self shouldAutorotateToInterfaceOrientation:YES];
//NSLog(#"Movie Player Controller View = %#", mp.moviePlayer.view);
//NSLog(#"Movie Player Controller Parent View = %#", mp.parentViewController);
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[mp moviePlayer]];
[mp.moviePlayer play];
The code below is more or less taken from the example MPMoviePlayerController sample code. In an app I wrote last year, it used to play videos fullscreen without an issue. Since iOS 4.0, there's just audio in the background. It's like the movie player doesn't have a view or the view is behind my app. I can still interact with my app, even 'start' a new video (audio only).
It's like the movie player now needs a view, but I don't see any way of supplying this in the API or the sample code (which does seem to be a version or two behind.
I load my videos from a URL and if I type these into Safari, they play just fine.
Here's the relevant code fragments, for what it's worth:
- (void)playMovieUrl:(NSURL*)url
delegate:(id)delegate
callbackSelector:(SEL)selector
{
#try {
movieFinishedCallbackDelegate = delegate;
movieFinishedCallbackSelector = selector;
movieURL = url;
MPMoviePlayerController* theMovie=[[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(myMovieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
[theMovie play];
}
#catch (NSException * e) {
return;
}
}
// When the movie is done,release the controller.
-(void)myMovieFinishedCallback:(NSNotification*)aNotification
{
MPMoviePlayerController* theMovie=[aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
[theMovie release];
[movieURL release];
[movieFinishedCallbackDelegate performSelector:movieFinishedCallbackSelector];
}
You probably need to present theMovie:
[self presentMoviePlayerViewControllerAnimated:theMovie];
And change to:
MPMoviePlayerViewController
in ios 3.2 letter use MPMoviePlayerViewController. it behave like a modelViewcontroller