我已在30秒内成功停止了视频。但是我无法解除MP MovieViewController
并且我想停止缓冲活动。我用过这段代码......
视频播放代码:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Pungi" ofType:@"mp4"]];
self.movie = [[MPMoviePlayerController alloc] initWithContentURL:url];
self.movie .controlStyle = MPMovieControlStyleEmbedded;
[ self.movie play];
[self.view addSubview:self.movie.view];
[self.movie setFullscreen:YES animated:YES];
self.movie.initialPlaybackTime = 0.5;
[NSTimer scheduledTimerWithTimeInterval:15.0
target:self
selector:@selector(stopVideo)
userInfo:nil
repeats:NO];
stopVideo:
[self.movie stop];
[self.movie.view removeFromSuperview];
[self.movie release];
答案 0 :(得分:5)
将您的MPMoviePlayerController
放在一个单独的类中并加载它:
MoviePlayerViewController.h
#import <MediaPlayer/MediaPlayer.h>
@interface MoviePlayerViewController : UIViewController
@end
MoviePlayerViewController.m
#import "MoviePlayerViewController.h"
MPMoviePlayerViewController *movieController;
@interface MoviePlayerViewController ()
@end
@implementation MoviePlayerViewController
- (void)willEnterFullscreen:(NSNotification*)notification {
NSLog(@"willEnterFullscreen");
}
- (void)enteredFullscreen:(NSNotification*)notification {
NSLog(@"enteredFullscreen");
}
- (void)willExitFullscreen:(NSNotification*)notification {
NSLog(@"willExitFullscreen");
}
- (void)exitedFullscreen:(NSNotification*)notification {
NSLog(@"exitedFullscreen");
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)playbackFinished:(NSNotification*)notification {
NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
switch ([reason intValue]) {
case MPMovieFinishReasonPlaybackEnded:
[self dismissModalViewControllerAnimated:YES];
break;
case MPMovieFinishReasonPlaybackError:
break;
case MPMovieFinishReasonUserExited:
[self dismissModalViewControllerAnimated:YES];
break;
default:
break;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willExitFullscreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enteredFullscreen:) name:MPMoviePlayerDidEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
NSString *videoName = @"Videoname";
NSString *filepath = [[NSBundle mainBundle] pathForResource:videoName @"movietype"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
[movieController.view setFrame:CGRectMake(0, -20, 320, 480)];
[self.view addSubview:movieController.view];
[movieController.moviePlayer play];
}
ViewController.m
MoviePlayerViewController *player = [[MoviePlayerViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:player animated:YES];
它的作用:
MoviePlayerViewController
是一个自定义类,可以使用视频加载MPMoviePlayerController
。在viewDidLoad
方法中(或您想要的任何地方),您可以加载MoviePlayerViewController
。
([self presentModalViewController:animated]
)....
这样做的好处是,您的主类没有使用moviecrap / definitions重载,并且当完成使用Notifications检查它是否已经停止时,您可以轻松地关闭MoviePlayerViewController
。
如果它已停止:
[self dismissModalViewControllerAnimated:YES];
希望这有帮助!
答案 1 :(得分:2)
如下面的代码所示,您应该暂停并在实际停止之前将initialPlaybackTime设置为-1。这是MPMoviePlayerController提供的棘手问题之一。
[_moviePlayerController pause];
if ([_moviePlayerController isKindOfClass:[MPMoviePlayerController class]]) {
((MPMoviePlayerController*)_moviePlayerController).initialPlaybackTime = -1;
}
[_moviePlayerController stop];
if ([_moviePlayerController isKindOfClass:[MPMoviePlayerController class]]) {
((MPMoviePlayerController*)_moviePlayerController).initialPlaybackTime = -1;
}
[_moviePlayerController.view removeFromSuperview];
答案 2 :(得分:1)
您需要在viewDidLoad中添加通知观察者:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackDidFinishNotification:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
然后添加方法:
- (void)moviePlayerPlaybackDidFinishNotification:(NSNotification*)notification
{
[self dismissMoviePlayerViewControllerAnimated];
}
答案 3 :(得分:0)
试试这个.......
self.movie.initialPlaybackTime = -1;
[self.movie stop];
[self.movie release];
答案 4 :(得分:0)
导入标题中的部分标题:
#import <MediaPlayer/MediaPlayer.h>
#import <MediaPlayer/MPMoviePlayerViewController.h>
你可能还需要平衡你的“presentMoviePlayer”电话与某处的解雇:
[self dismissMoviePlayerViewControllerAnimated];
如果您提前完成了资源,则可以使用NotificationManager监视MPMoviePlayerPlaybackDidFinishNotification,以便尽早发布。
以及
- (void)dealloc {
[movie release],
movie = nil;
[super dealloc];
}