当合成中存在音频时,Avplayer不播放视频

时间:2012-08-25 22:10:16

标签: audio video avplayer avmutablecomposition avvideocomposition

如果我在合成中添加音频,我一直在努力解决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.videoCompositionavmutablevideocompositioninstruction等方式......没有运气。

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

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

祝你好运, 内诺

0 个答案:

没有答案