AVPlayer 不播放视频文件

发布于 2024-11-10 13:49:11 字数 1222 浏览 2 评论 0原文

嗨,我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅暮の光 2024-11-17 13:49:11

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

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

Because you use local file, it's better to use [NSURL fileURLWithPath:url] for converting NSString to NSURL. So change the code to:

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:url]];
吲‖鸣 2024-11-17 13:49:11

我不知道逐帧缓慢的情况,但你正在做一些你不需要做的事情。在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文