我正在为我的应用制作视频播放器模块。
这是我的 .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
视频不启动,“完成”按钮不起作用..我不明白哪里出了问题。 请帮助我。
最佳答案
是的,问题是, 而不是使用全局属性
@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
您已在您的 - (IBAction)Video1Button:(id)sender; 中重新声明了一个 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];
}
关于iOS 视频无法使用 MPMoviePlayer 播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16312558/