6

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

2 回答 2

11

因为您使用本地文件,所以最好使用[NSURL fileURLWithPath:url]将 NSString 转换为 NSURL。所以将代码更改为:

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:url]];
于 2013-03-10T12:59:46.443 回答
4

我不知道逐帧缓慢,但你正在做一些你不需要做的事情。在 iPhone 文档中,Apple 的人完全搞砸了内存管理,使它变得比它需要的复杂得多,所以我会告诉你让它发挥作用的关键因素:

首先,保留玩家,而不是图层。如果您不知道为什么,请阅读一本书或 Views Manual。(里面充满了谐音风格的错别字,但比买一本厚书要容易)。

2,您没有投射图层并使用 setPlayer:,确切的行在 Apple 的文档中。您必须使用类型转换转换 self.layer。

于 2011-06-03T14:56:43.090 回答