iOS 开发之播放本地视频没效果

这个问题出在于加载资源的方式上了,下面放四段代码:

- (void)playVideo0{
    NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"Video" ofType:@"mp4"];
    NSURL *videoURL = [NSURL URLWithString:videoPath];
    AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:videoURL];
    AVPlayer *myPlayer = [AVPlayer playerWithPlayerItem:playerItem];
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:myPlayer];
    playerLayer.frame = self.view.bounds;
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.view.layer addSublayer:playerLayer];
    [myPlayer play];
}
- (void)playVideo1{
    NSURL * videoUrl = [[NSBundle mainBundle] URLForResource:@"Video" withExtension:@"mp4"];
    AVPlayer * player = [AVPlayer playerWithURL:videoUrl];
    AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:player];
    layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    layer.frame = self.view.bounds;
    [self.view.layer addSublayer:layer];
    [player play];
}

- (void)playVideo2{
    NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"Video" ofType:@"mp4"];
    NSURL *videoURL = [NSURL fileURLWithPath:videoPath];
    AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:videoURL];
    AVPlayer *myPlayer = [AVPlayer playerWithPlayerItem:playerItem];
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:myPlayer];
    playerLayer.frame = self.view.bounds;
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.view.layer addSublayer:playerLayer];
    [myPlayer play];
}
- (void)playVideo3{
    NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"Video" ofType:@"mp4"];
    NSURL *videoURL = [NSURL fileURLWithPath:videoPath isDirectory:YES];
    AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:videoURL];
    AVPlayer *myPlayer = [AVPlayer playerWithPlayerItem:playerItem];
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:myPlayer];
    playerLayer.frame = self.view.bounds;
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.view.layer addSublayer:playerLayer];
    [myPlayer play];
}

经过测试发现playVideo0是看不到视频播放的,其他三种形式都可以,如果大家有遇到过,请注意哟~

你可能感兴趣的:(iOS 开发之播放本地视频没效果)