本地视频无法在iOS 6 iPad应用中播放

时间:2012-09-25 16:52:05

标签: cocoa-touch ipad ios6

之前我没有在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

1 个答案:

答案 0 :(得分: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
}