2

如果我将音频添加到合成中,我一直在努力解决 avplayer 不显示视频资产的问题。

这是现在的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

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

    AVURLAsset *videoAsset = [[AVURLAsset alloc]initWithURL:[NSURL URLWithString:@"http://xxx.xxx.xxx/xxx.mp4"] options:nil];
    AVURLAsset *audioAsset = [[AVURLAsset alloc]initWithURL:[NSURL URLWithString:@"http://xxx.xxx.xxx/xxx.mp3"] options:nil];

    AVMutableCompositionTrack *cmpTrackVideo = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    AVMutableCompositionTrack *cmpTrackAudio = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

    AVAssetTrack *clipVideo = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
    AVAssetTrack *clipAudio = [[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];

    [cmpTrackVideo insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:clipVideo atTime:kCMTimeZero error:nil];
    [cmpTrackAudio insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration) ofTrack:clipAudio atTime:kCMTimeZero error:nil];

    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:composition];
    self.player = [[AVPlayer alloc] initWithPlayerItem:playerItem];

    AVPlayerLayer * playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
    [playerLayer setFrame:self.view.bounds];
    [self.view.layer addSublayer:playerLayer];

    [self.player play];
}

我也尝试过使用playerItem.videoComposition等等avmutablevideocompositioninstruction......但没有运气。

有没有人知道可能导致这种情况的原因?

我会对任何输入、提示、示例等感到满意。

最好的问候,尼诺

4

0 回答 0