Dismissing MPMoviePlayerViewController the right way
2013-01-17 12:27
309 查看
If you have used the MPMoviePlayerViewController to play videos in iOS, chances are you have gotten a little frustrated by its rigidness. A couple
of issues I personally encountered were:
When presented modally, the view controller did not respect the modal transition style I had chosen for it.
When the video finished playing, the view controller dismissed itself automatically. I wanted it to remain visible until the user pressed the Done
button.
A couple of hours of browsing for the solution to no avail, I decided to try out sort of a hack, which turned out to work great.
The trick is to remove the MPMoviePlayerViewController instance from the MPMoviePlayerPlaybackDidFinishNotification notification observers, and instead
handle it yourself. Seems like this view controller listens to this notification and auto dismisses itself when it’s fired.
In my application, I present a thumbnail of the video. When touched, the video is played.
Java代码
- (void)playVideo:(NSString*)aVideoUrl
{
// Initialize the movie player view controller with a video URL string
MPMoviePlayerViewController *playerVC = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:aVideoUrl]] autorelease];
// Remove the movie player view controller from the "playback did finish" notification observers
[[NSNotificationCenter defaultCenter] removeObserver:playerVC
name:MPMoviePlayerPlaybackDidFinishNotification
object:playerVC.moviePlayer];
// Register this class as an observer instead
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:playerVC.moviePlayer];
// Set the modal transition style of your choice
playerVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
// Present the movie player view controller
[self presentModalViewController:playerVC animated:YES];
// Start playback
[playerVC.moviePlayer prepareToPlay];
[playerVC.moviePlayer play];
}
In the code above, an instance of MPMoviePlayerViewController is created, and then removed from the observers of the MPMoviePlayerDidFinishNotification
immediately; in its place, the current class is added as an observer, with a selector named movieFinishedCallback: (described below).
In addition, the modalTransitionStyle property of the view controller is set. The documentation suggests you to use the presentMoviePlayerViewControllerAnimated
method to present it, in order to use the standard movie player transition. But if you want to use different transition, you must use the regular presentModalViewController method.
Back to the observer notification. Now that the view controller is no longer an observer, it won’t dismiss itself, ever, even if the Done button is
pressed. So you must implement the code to dismiss it.
Java代码
- (void)movieFinishedCallback:(NSNotification*)aNotification
{
// Obtain the reason why the movie playback finished
NSNumber *finishReason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
// Dismiss the view controller ONLY when the reason is not "playback ended"
if ([finishReason intValue] != MPMovieFinishReasonPlaybackEnded)
{
MPMoviePlayerController *moviePlayer = [aNotification object];
// Remove this class from the observers
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
// Dismiss the view controller
[self dismissModalViewControllerAnimated:YES];
}
}
In my case, my original problem was that I wanted the video to stay visible until the user deliberately pressed the Done button. In the code above,
the notification’s “playback finished reason” is examined: if the video playback ended normally, then the notification is ignored; if the video playback ended for another reason (i.e. an error or the user exited), then the view controller is dismissed.
As an added bonus, by dismissing the view controller using the dismissModalViewControllerAnimated: method, the modal transition style set earlier
is respected.
The code is kinda messy, but I hope you get the idea. Perhaps it would be desirable to encapsulate this code by extending the MPMoviePlayerViewController
class.
转载:/article/4464531.html
of issues I personally encountered were:
When presented modally, the view controller did not respect the modal transition style I had chosen for it.
When the video finished playing, the view controller dismissed itself automatically. I wanted it to remain visible until the user pressed the Done
button.
A couple of hours of browsing for the solution to no avail, I decided to try out sort of a hack, which turned out to work great.
The trick is to remove the MPMoviePlayerViewController instance from the MPMoviePlayerPlaybackDidFinishNotification notification observers, and instead
handle it yourself. Seems like this view controller listens to this notification and auto dismisses itself when it’s fired.
In my application, I present a thumbnail of the video. When touched, the video is played.
Java代码
- (void)playVideo:(NSString*)aVideoUrl
{
// Initialize the movie player view controller with a video URL string
MPMoviePlayerViewController *playerVC = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:aVideoUrl]] autorelease];
// Remove the movie player view controller from the "playback did finish" notification observers
[[NSNotificationCenter defaultCenter] removeObserver:playerVC
name:MPMoviePlayerPlaybackDidFinishNotification
object:playerVC.moviePlayer];
// Register this class as an observer instead
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:playerVC.moviePlayer];
// Set the modal transition style of your choice
playerVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
// Present the movie player view controller
[self presentModalViewController:playerVC animated:YES];
// Start playback
[playerVC.moviePlayer prepareToPlay];
[playerVC.moviePlayer play];
}
In the code above, an instance of MPMoviePlayerViewController is created, and then removed from the observers of the MPMoviePlayerDidFinishNotification
immediately; in its place, the current class is added as an observer, with a selector named movieFinishedCallback: (described below).
In addition, the modalTransitionStyle property of the view controller is set. The documentation suggests you to use the presentMoviePlayerViewControllerAnimated
method to present it, in order to use the standard movie player transition. But if you want to use different transition, you must use the regular presentModalViewController method.
Back to the observer notification. Now that the view controller is no longer an observer, it won’t dismiss itself, ever, even if the Done button is
pressed. So you must implement the code to dismiss it.
Java代码
- (void)movieFinishedCallback:(NSNotification*)aNotification
{
// Obtain the reason why the movie playback finished
NSNumber *finishReason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
// Dismiss the view controller ONLY when the reason is not "playback ended"
if ([finishReason intValue] != MPMovieFinishReasonPlaybackEnded)
{
MPMoviePlayerController *moviePlayer = [aNotification object];
// Remove this class from the observers
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
// Dismiss the view controller
[self dismissModalViewControllerAnimated:YES];
}
}
In my case, my original problem was that I wanted the video to stay visible until the user deliberately pressed the Done button. In the code above,
the notification’s “playback finished reason” is examined: if the video playback ended normally, then the notification is ignored; if the video playback ended for another reason (i.e. an error or the user exited), then the view controller is dismissed.
As an added bonus, by dismissing the view controller using the dismissModalViewControllerAnimated: method, the modal transition style set earlier
is respected.
The code is kinda messy, but I hope you get the idea. Perhaps it would be desirable to encapsulate this code by extending the MPMoviePlayerViewController
class.
转载:/article/4464531.html
相关文章推荐
- MPMoviePlayerController刚进入ViewController就横屏满屏幕播放
- MPMoviePlayerViewControllerIOS6下实现横屏播放
- iOS 视频播放 MPMoviePlayerViewController
- MPMoviePlayerViewController在iOS6与iOS7中的不同用法
- MPMoviePlayerViewController和MPMoviePlayerController
- MPMoviePlayerViewController和MPMoviePlayerController
- IOS MPMoviePlayerViewController视频播放(MediaPlayer)
- MPMoviePlayerViewController和MPMoviePlayerController 这个太容易忘了
- iOS开发--利用MPMoviePlayerViewController播放视频简单实现
- 媒体播放器(AVAudioPlayer,MPMoviePlayerViewController)
- 视频播放--MPMoviePlayerViewController
- 视频播放器MPMoviePlayerController,MPMoviePlayerViewController
- MPMoviePlayerController和MPMoviePlayerViewController
- 使用MPMoviePlayerViewController播放视频
- MPMoviePlayerViewController实现横屏播放
- iOS6下慎用MPMoviePlayerViewController!
- 播放视频音频文件使用 MPMoviePlayerViewController
- MPMoviePlayerViewController
- MPMoviePlayerViewController电影播放器
- MPMoviePlayerViewController的使用