簡體   English   中英

AVPlayer無法在iOS中播放視頻文件

[英]AVPlayer not playing video file in iOS

我正在嘗試使用以下代碼播放存儲在設備上的視頻。 問題是我什么都沒看到,只有白色的屏幕。

在這種情況下, 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];

我正在使用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];

希望這可以幫助您解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM