iOS 视频无法使用 MPMoviePlayer 播放

标签 ios objective-c video mpmovieplayercontroller

我正在为我的应用制作视频播放器模块。

这是我的 .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/

相关文章:

ios - opengl es VBO 用法 - 无几何渲染

ios - 如何使用 UIBezierPath 创建水滴形状

ios - Swift4,选择单元格时的背景颜色

video - Youtube iFrame API 参数

python - 有没有办法在kivy中录制视频?

ios - 如何将 CAGradient 应用于图片的下方?

ios - 如何在结构定义中定义CGPoints数组?

iphone - UINavigationController.view.frame 即使在横向上也返回纵向 CGRect。如何解决这个问题?

objective-c - 为什么 NSApplescriptexecuteAndReturnError : seem to leak memory?

Java - 使用 Mac 和 Windows x86 和 x64 进行网络摄像头拍摄/视频