Reputation: 1323
I'm streaming video to the iPhone and don't like the way MPMoviePlayerController handles things at the beginning. I noticed that when you click on the row to select a particular movie the app just sits there until it is loaded sufficiently to play it. So I popped in a UIImageView with a loading image and a nice spinner to keep the user informed.
I think they should have the option to cancel the play back of the movie if they get tired of waiting. So I've got a cancel button with a background image.
UIButton *cancel;
UIImage *cancelImage = [UIImage imageNamed:@"cancel.png"];
cancel = [UIButton buttonWithType:UIButtonTypeCustom];
cancel.frame = CGRectMake(0,0, cancelImage.size.width, cancelImage.size.height);
cancel.center = CGPointMake(35, 20);
[cancel setImage:cancelImage forState:UIControlStateNormal];
[cancel addTarget:self action:@selector(cancelMovie) forControlEvents:UIControlEventTouchUpInside];
[moviePreviewView addSubview:cancel];
But I'm not sure what to put in the cancelMovie method. I've tried duplicating what I have in the moviePlayBackDidFinish call back method but it crashes rather spectacularly.
I also tried:
-(void)cancelMovie {
[theMovie.view removeFromSuperview];
[theMovie release];
}
But that doesn't do anything good either. I was thinking I could call the moviePlayBackDidFinish directly but I don't think you can call a notification method directly.
Your thoughts?
Upvotes: 1
Views: 189
Reputation: 29767
-(void)cancelMovie
{
if (_mediaPlayer){
[_mediaPlayer stop];
_mediaPlayer.initialPlaybackTime = -1.0;
_mediaPlayer.contentURL = nil;
[_mediaPlayer prepareToPlay];
}
}
Upvotes: 0
Reputation: 1448
@Michael solution works. There could be improvement that there is no need to play cancel video. Instead, you can directly call the MPMoviePlayerPlaybackDidFinishNotification directly.
[self moviePlayBackDidFinish:nil];
First i implemented @Michael's idea, but eventually, i succeeded without playing next video.
Upvotes: 0
Reputation: 1323
I hate answering my own question but since this question only had 7 views it couldn't be that important I suppose. Perhaps I shouldn't post them in the middle of the night... no one sees them when you do.
Anyway, I didn't find a good solution to this problem... just a hack.
In the cancel method I told the moviePlayer to load a different, one second movie that is a the same photo I used in the previewImage. When that 'movie' finishes it then proceeds to call the MPMoviePlayerPlaybackDidFinishNotification and cleans up and returns the user back where they were.
-(void)cancelMovie
{
cancelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"cancel" ofType:@"m4v"]];
[theMovie setContentURL:cancelURL];
[theMovie setControlStyle:MPMovieControlStyleFullscreen];
[theMovie setFullscreen:YES];
// for some reason ... this is neccessary
[theMovie prepareToPlay];
// Register that the load state changed (movie is ready)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:theMovie];
// Register for the PlayBackDidFinish (movie is finished)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
}
Not good... but it works.
Upvotes: 2