gpt4 book ai didi

ios - 从应用内播放视频?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:47:29 25 4
gpt4 key购买 nike

我正在尝试使用 AVPlayer 从我的应用程序播放本地视频。我到处都看过,但找不到任何有用的教程/演示/文档。这是我正在尝试的,但它没有播放。知道为什么吗?该 URL 有效,因为我使用同一个 URL 成功地使用 MPMoviePlayer 播放视频。

       Video *currentVideo = [videoArray objectAtIndex:0];

NSString *filepath = currentVideo.videoURL;
NSURL *fileURL = [NSURL fileURLWithPath:filepath];

AVURLAsset *asset = [AVURLAsset assetWithURL: fileURL];
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset: asset];
self.player = [[AVPlayer alloc] initWithPlayerItem: item];


AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.player];
self.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;


layer.frame = CGRectMake(0, 0, 1024, 768);
[self.view.layer addSublayer: layer];


[self.player play];

最佳答案

我认为问题在于您假设在执行此行后 AVURLAsset *asset = [AVURLAsset assetWithURL: fileURL]; Assets 已准备好使用。这不是 AV Foundation Programming Guide 中指出的情况。 :

    You create an asset from a URL using AVURLAsset. Creating the asset, however, 
does not necessarily mean that it’s ready for use. To be used, an asset must
have loaded its tracks.

创建 Assets 后尝试加载它的轨道:

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:fileURL options:nil];
NSString *tracksKey = @"tracks";

[asset loadValuesAsynchronouslyForKeys:@[tracksKey] completionHandler:
^{
NSError *error;
AVKeyValueStatus status = [asset statusOfValueForKey:tracksKey error:&error];

if (status == AVKeyValueStatusLoaded) {

// At this point you know the asset is ready
self.playerItem = [AVPlayerItem playerItemWithAsset:asset];

...
}
}];

请引用this link查看完整示例。

希望这对您有所帮助!

关于ios - 从应用内播放视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19326728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com