gpt4 book ai didi

ios - 具有独立视频和音频 url 的 AVAsset (iOS)

转载 作者:行者123 更新时间:2023-11-29 00:38:19 24 4
gpt4 key购买 nike

是否可以使用两个 url 创建一个 AVAsset 对象,一个用于音频轨道,另一个用于视频轨道?

我已经用 AVMutableComposition 尝试过,但它似乎先加载整个内容并在开始视频+音频播放之前将其缓冲到某个地方。在AVComposition的文档中,它说可以组合基于文件的 Assets ,但我需要一种方法来组合基于url的 Assets 。

或者是否可以为 AVComposition 设置一个选项,以便在加载整个内容之前开始播放?

编辑

我是这样尝试的:

NSDictionary *urlAssetOptions = @{AVURLAssetPreferPreciseDurationAndTimingKey: [NSNumber numberWithBool:NO]};

AVMutableComposition *composition = [AVMutableComposition composition];


NSURL *audioUrl = [NSURL URLWithString:@"http://..."];
AVURLAsset *audioAsset = [AVURLAsset URLAssetWithURL:audioUrl options:urlAssetOptions];

AVMutableCompositionTrack *audioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration) ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil];


NSURL *videoUrl = [NSURL URLWithString:@"http://..."];
AVURLAsset *videoAsset = [AVURLAsset URLAssetWithURL:videoUrl options:urlAssetOptions];

AVMutableCompositionTrack *videoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];


AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:composition];

最佳答案

您使用的解决方案不需要加载整个内容来开始创建可变合成,也不需要开始播放您创建的合成。但是,它需要加载一部分媒体文件以确定每个文件的持续时间和轨道。

下面是工作代码,它使用 google 找到的 mp3 和 mp4 文件的 url 来创建可变组合并将其传递给 AVPlayerViewController。如果运行代码,您会发现它很快就开始播放,但如果您跳过视频时间线,您会发现加载请求时间的数据需要很长时间。

NSURL *audioURL = [NSURL URLWithString:@"http://www.mfiles.co.uk/mp3-downloads/Toccata-and-Fugue-Dm.mp3"];
AVAsset *audioAsset = [AVAsset assetWithURL:audioURL];

NSURL *videoURL = [NSURL URLWithString:@"http://thv1.uloz.to/6/c/4/6c4b50308843dd29c9176cc2c4961155.360.mp4?fileId=20389770"];
AVAsset *videoAsset = [AVAsset assetWithURL:videoURL];

CMTime duration;
if (CMTimeGetSeconds(audioAsset.duration) < CMTimeGetSeconds(videoAsset.duration)) {
duration = audioAsset.duration;
} else {
duration = videoAsset.duration;
}

NSError *error;

AVMutableComposition* mixAsset = [[AVMutableComposition alloc] init];

AVMutableCompositionTrack* audioTrack = [mixAsset addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, duration) ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error: &error];

AVMutableCompositionTrack* videoTrack = [mixAsset addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, duration) ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error: &error];

AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:mixAsset];

AVPlayerViewController* playerController = [AVPlayerViewController new];
playerController.player = [AVPlayer playerWithPlayerItem:playerItem];

[self presentViewController:playerController animated:YES completion:nil];

关于ios - 具有独立视频和音频 url 的 AVAsset (iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40113274/

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