1

我之前没有在 iOS 中播放过视频文件,所以我不确定我是否只是遗漏了一些东西。播放器打开,但卡在加载动画中,我无法点击完成按钮。难道是我的视频文件不受支持?我为ipad编码。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIButton *video_btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    video_btn.frame = CGRectMake(0, 0, 100, 50);
    [video_btn setTitle:@"Moonrise" forState:UIControlStateNormal];
    [video_btn addTarget:self action:@selector(playMovie:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:video_btn];



}

-(IBAction)playMovie:(id)sender
{

    NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"moonrise" ofType:@"mp4"];
    NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
    MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
    [self.view addSubview:moviePlayerController.view];
    moviePlayerController.fullscreen = YES;
    [moviePlayerController play];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

我得到这个作为输出:

2012-09-25 12:47:56.206 App[4895:c07] [MPAVController] Autoplay: Disabling autoplay for pause
2012-09-25 12:47:56.207 App[4895:c07] [MPAVController] Autoplay: Disabling autoplay
2012-09-25 12:47:56.221 App[4895:c07] [MPAVController] Autoplay: Disabling autoplay for pause
2012-09-25 12:47:56.222 App[4895:c07] [MPAVController] Autoplay: Disabling autoplay
2012-09-25 12:47:56.226 App[4895:c07] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0)
2012-09-25 12:47:56.255 App[4895:c07] [MPAVController] Autoplay: Enabling autoplay
4

1 回答 1

0

尝试将您的 MPMoviePlayerController 设置为属性。

@property (strong, nonatomic) MPMoviePlayerController *moviePlayerController;

要自动关闭,您必须在 MPMoviePlayerController 中设置通知。像这样:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(videoFinishedCallback:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:moviePlayerController];

这将在视频结束时调用函数 - videoFinishedCallback。然后,您实现回调:

- (void) videoFinishedCallback:(NSNotification*)aNotification
{
    //remove notification
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:object:moviePlayerController];

    // here you set your code to remove the video
    // for example, you can remove from superview and set value to nil
}
于 2012-09-25T17:40:57.363 回答