gpt4 book ai didi

ios - AVURLAsset tracksWithMediaType 在模拟器中不为 AVMediaTypeAudio 返回音频轨道

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:56:35 24 4
gpt4 key购买 nike

我正在做以下事情

    AVURLAsset              *audioAsset  = [[AVURLAsset alloc] initWithURL:audioUrl options:nil];
NSArray<AVAssetTrack *> *audioTracks = [audioAsset tracksWithMediaType:AVMediaTypeAudio];

在真实设备上运行良好。

问题只发生在模拟器中。我在 bundle 中静态添加了 mp3,因此 audioAsset 已正确启动。但是数组 audioTracks 在模拟器上是空的(即使 audioUrlaudioAsset 变量中的路径正确且存在。

有什么建议吗?

最佳答案

我在真实设备上也遇到过同样的问题。该问题是由于初始化 Assets 后尚未准备好而引起的。

请看documentation :

You can initialize a player item with an existing asset, or you can initialize a player item directly from a URL so that you can play a resource at a particular location (AVPlayerItem will then create and configure an asset for the resource). As with AVAsset, though, simply initializing a player item doesn’t necessarily mean it’s ready for immediate playback. You can observe (using key-value observing) an item’s status property to determine if and when it’s ready to play.

要解决此问题,您可以尝试执行以下操作:

AVURLAsset *audioAsset = [[AVURLAsset alloc] initWithURL:audioUrl options:nil];
NSString *tracksKey = @"tracks";
[audioAsset loadValuesAsynchronouslyForKeys:@[tracksKey] completionHandler:
^{
NSError *error;
AVKeyValueStatus status = [asset statusOfValueForKey:tracksKey error:&error];
if (status == AVKeyValueStatusLoaded) {
// The asset is ready at this point
NSArray<AVAssetTrack *> *audioTracks = [audioAsset tracksWithMediaType:AVMediaTypeAudio];
}
}];

另外值得注意的是,AVKeyValueStatus 状态可以是 AVKeyValueStatusFailed,以防 audioAsset 不可播放:

BOOL result = [audioAsset isPlayable];

关于ios - AVURLAsset tracksWithMediaType 在模拟器中不为 AVMediaTypeAudio 返回音频轨道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38169589/

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