AVPlayer 不播放视频文件
嗨,我正在使用 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]];
HI I'm using AVPlayer to play the video slowly frame by frame. I used this coding for that. I could not able to play the video. Please notice my problem.
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]];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为您使用本地文件,所以最好使用
[NSURL fileURLWithPath:url]
将 NSString 转换为 NSURL。因此将代码更改为:Because you use local file, it's better to use
[NSURL fileURLWithPath:url]
for converting NSString to NSURL. So change the code to:我不知道逐帧缓慢的情况,但你正在做一些你不需要做的事情。在 iPhone 文档中,Apple 人员完全搞乱了内存管理,使其变得比需要的更加复杂,所以我将告诉您使其发挥作用的关键事项:
首先,保留播放器,而不是图层。如果您不知道原因,请阅读书籍或视图手册。 (里面充满了同音字式的错别字,但比买一本厚书容易多了)。
第二,你没有投射图层并使用setPlayer:,确切的行在Apple的文档中。您必须使用类型转换来转换 self.layer 。
I don't know about frame by frame slowness, but you're doing a few things you don't need to do. In the iPhone documentation, the Apple guys completely messed with memory management, making it way more complex than it needs to be, so I'll tell you the key things that make it play:
First, retain the player, not the layer. If you don't know why, read a book or the Views Manual. (Which is filled with homonym-style typos, but it's easier than buying a thick book).
2nd, you did not cast the layer and use setPlayer:, the exact line is inside Apple's documentation. You have to convert self.layer using a typecast.