AVPlayer没有播放视频

时间:2012-07-06 09:55:37

标签: objective-c ios

- (void)viewDidLoad{

    [super viewDidLoad];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"VidName" ofType:@"mov"];
    NSURL *url = [NSURL URLWithString:path];

    AVPlayer *av = [[AVPlayer alloc] initWithURL:url];
    AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:av];
    [layer setFrame:self.view.frame];
    [self.view.layer addSublayer:layer];
    [av play];

    NSLog(@"error: %@", av.error);
}

记录的错误是NULL。

1 个答案:

答案 0 :(得分:10)

而不是

NSURL *url = [NSURL URLWithString:path];

使用以下行。

NSURL *url=[[NSURL alloc]initFileURLWithPath:path];