【问题标题】:AVAsset with separate video and audio urls (iOS)AVAsset 具有单独的视频和音频 url (iOS)
【发布时间】:2017-02-28 00:35:05
【问题描述】:

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

我已经用AVMutableComposition 进行了尝试,但它似乎首先加载了整个内容并将其缓冲在某个地方,然后才能开始视频+音频播放。在AVComposition的文档中,它说基于文件的资产可以组合,但我需要一种方法来组合基于url的资产。

或者是否可以为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];

【问题讨论】:

  • 最简单的方法..创建两个 AVAsset。\
  • @Gagan_iOS 那么我该如何使用这两个 AVAsset 创建 AVPlayerItem 对象?

标签: ios objective-c avplayeritem avurlasset avcomposition


【解决方案1】:

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

下面是工作代码,它使用 url 到 google 找到的 mp3 和 mp4 文件来创建可变组合并将其传递给 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];

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-19
  • 2015-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多