原本在ios5上的寫法如下:(ios6將無法使用)
- (void)openMP4:(NSString *)s_url
{
NSURL *fileURL = [NSURL URLWithString:s_url];
[player.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
[player.moviePlayer setFullscreen:NO];
player = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
//弹出播放器窗口
[self presentMoviePlayerViewControllerAnimated:player ];
[player.moviePlayer setMovieSourceType:MPMovieSourceTypeFile];
[player.moviePlayer prepareToPlay];
[self.view addSubview:player.view];
//播放
[player.moviePlayer play];
//增加一個觀察者
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
}
- (void) moviePlayBackDidFinish:(NSNotification*) aNotification {
//移除觀察者
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
//播放器消失
[player dismissMoviePlayerViewControllerAnimated];
player=NULL;
}
相容ios5、ios6的寫法如下(來源):
{
// Initialize the movie player view controller with a video URL string
NSURL *fileURL = [NSURL URLWithString:s_url];
MPMoviePlayerViewController *playerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
// Remove the movie player view controller from the "playback did finish" notification observers
[[NSNotificationCenter defaultCenter] removeObserver:playerVC
name:MPMoviePlayerPlaybackDidFinishNotificationobject: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];
}
- (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:MPMoviePlayerPlaybackDidFinishNotificationobject:moviePlayer];
// Dismiss the view controller
[self dismissModalViewControllerAnimated:YES];
}}
沒有留言:
張貼留言