我正在为我的应用制作视频播放器模块。
这是我的.h文件:
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface SpanishViewController : UIViewController
- (IBAction)Video1Button:(id)sender;
@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
@end
这是由.m文件中的按钮执行的事件代码:
- (IBAction)Video1Button:(id)sender {
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"01" ofType:@"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}
视频以mp4编码,符合标准,并在iOS上运行。 结果是 - here
视频无法启动,按钮“完成”无法正常工作..我无法理解出了什么问题。 请帮帮我。
答案 0 :(得分:1)
是的,问题是, 而不是使用全局属性
@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
您已在 - (IBAction)Video1Button:(id)发件人中重新声明了MPMoviePlayerController;方法
因此,moviePlayer的生命以 Video1Button 方法的结尾结束。
正确的方式,
- (IBAction)Video1Button:(id)sender {
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"01" ofType:@"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
_moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
[self.view addSubview:_moviePlayerController.view];
_moviePlayerController.fullscreen = YES;
[_moviePlayerController play];
}
答案 1 :(得分:0)
像这样更改你的video1Button方法。
- (IBAction)Video1Button:(id)sender {
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"01" ofType:@"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
[self.view addSubview:self.moviePlayerController.view];
self.moviePlayerController.fullscreen = YES;
[self.moviePlayerController play];
}