1

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

这是我的 .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 上运行的视频。结果是 -这里

视频无法启动,“完成”按钮不起作用..我不明白出了什么问题。请帮我。

4

2 回答 2

1

是的,问题是,而不是使用全局属性

@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];
}
于 2013-05-01T04:08:51.273 回答
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];
}
于 2013-05-01T04:21:44.803 回答