簡體   English   中英

AVPlayer 不播放視頻文件

[英]AVPlayer not playing Video file

嗨,我正在使用 AVPlayer 逐幀緩慢播放視頻。 我為此使用了這個編碼。 我無法播放視頻。 請注意我的問題。

UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 440)];
newView.backgroundColor = [UIColor yellowColor];

NSString *videoName = [fileNameArray objectAtIndex:indexPath.section];
NSString *url = [Utilities documentsPath:[NSString stringWithFormat:@"OSC/%@/%@.mov",videoName,videoName]];

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:url]];
//AVPlayer *avPlayer = [[AVPlayer playerWithURL:[NSURL URLWithString:url]] retain];
AVPlayer *avPlayer = [[AVPlayer playerWithPlayerItem:playerItem] retain];
AVPlayerLayer *avPlayerLayer = [[AVPlayerLayer playerLayerWithPlayer:avPlayer] retain];

avPlayerLayer.frame = self.view.frame;
[newView.layer addSublayer:avPlayerLayer];
[self.view addSubview:newView];
[avPlayer play];

avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; 

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playerItemDidReachEnd:)
                                             name:AVPlayerItemDidPlayToEndTimeNotification
                                           object:[avPlayer currentItem]];

因為您使用本地文件,所以最好使用[NSURL fileURLWithPath:url]將 NSString 轉換為 NSURL。 所以將代碼更改為:

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:url]];

我不知道逐幀緩慢,但你正在做一些你不需要做的事情。 在 iPhone 文檔中,Apple 家伙完全搞砸了 memory 管理,使它變得比它需要的復雜得多,所以我會告訴你讓它發揮作用的關鍵因素:

首先,保留玩家,而不是圖層。 如果您不知道為什么,請閱讀一本書或 Views Manual。 (里面充滿了諧音風格的錯別字,但比買一本厚書要容易)。

2,您沒有投射圖層並使用 setPlayer:,確切的行在 Apple 的文檔中。 您必須使用類型轉換轉換 self.layer。

暫無
暫無

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

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