AVPlayer不在iOS中播放视频文件

时间:2015-07-26 02:10:11

标签: ios avfoundation avplayer avplayerlayer

我正在尝试使用以下代码播放存储在我的设备上的视频。问题是我在屏幕上看不到任何东西,只是一个白色的屏幕。

在这种情况下,filePath的值为file:///var/mobile/Containers/Data/Application/0E87EF09-B87C-443F-9BFB-E9A68AC4A162/Documents/TMRecordedVideos/2015-07-25T18-57-19Z.mov

NSString *filePath = [self.video fileURL];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath isDirectory:NO];

AVAsset *videoAsset = [AVAsset assetWithURL:fileUrl];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:videoAsset];

self.videoPlayer = [AVPlayer playerWithPlayerItem:playerItem];

self.videoPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.videoPlayer];
[self.videoPlayerLayer setFrame:self.view.frame];

[self.view.layer addSublayer:self.videoPlayerLayer];
[self.videoPlayer play];

1 个答案:

答案 0 :(得分:0)

我正在使用 AVPlayerLayer 播放视频。我使用下面的代码,它工作正常。 URL(在我的情况下为 outputFileURL )与您的filePath类似。

AVPlayer *avPlayerq = [AVPlayer playerWithURL:outputFileURL];
avPlayerq.actionAtItemEnd = AVPlayerActionAtItemEndNone;
AVPlayerLayer *videoLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayerq];
videoLayer.frame= self.view.bounds;
videoLayer.videoGravity=AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:videoLayer];
[avPlayerq play];

希望这可以帮助您解决问题。