首页 文章

AVComposition中的曲目在暂停时会丢失时间

提问于
浏览
1

我创建了一个AVMutableComposition,它包含一系列在特定时间开始的音轨 . 从那里开始,按照Apple的建议,我在用AVPlayer播放之前把它变成AVComposition .

播放这个AVPlayer项目一切正常,但是如果我暂停它然后继续,合成中的所有曲目似乎相对于彼此滑回约0.2秒(即,它们聚在一起) . 击中暂停并继续几次使效果复合并且重叠更加显着(基本上如果我足够击中,我将最终同时播放所有8个音轨) .

if (self.player.rate > 0.0) {
    //if player is playing, pause
    [self.player pause];

} else {

    if (self.player) {
      [self.player play];
       return;

        }

     */CODE CREATING COMPOSITION - missed out big chunk of code relating to finding the track and retrieving its position and scale/*


        NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
                                                            forKey:AVURLAssetPreferPreciseDurationAndTimingKey];

        AVURLAsset *sourceAsset = [[AVURLAsset alloc] initWithURL:url options:options];

        //calculate times
        NSNumber *time = [soundArray1 objectAtIndex:1]; //this is the time scale - e.g. 96 or 120 etc.

        double timenow = [time doubleValue];

        double insertTime = (240*y);

                    AVMutableCompositionTrack *track =
        [composition addMutableTrackWithMediaType:AVMediaTypeAudio
                                 preferredTrackID:kCMPersistentTrackID_Invalid];

        //insert the audio track from the asset into the track added to the mutable composition
        AVAssetTrack *myTrack = [[sourceAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
        CMTimeRange myTrackRange = myTrack.timeRange;
        NSError *error = nil;
        [track insertTimeRange:myTrackRange
                                      ofTrack:myTrack
                                       atTime:CMTimeMake(insertTime, timenow) 
                                        error:&error];

        [sourceAsset release];







    }

} 
    AVComposition *immutableSnapshotOfMyComposition = [composition copy];

        AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:immutableSnapshotOfMyComposition];
        self.player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
        NSLog(@"here");

        [self.player play];

谢谢

2 回答

  • 0

    好吧,这感觉有点hacky,但如果有人卡住了肯定会有效 . 如果有人有更好的答案,请告诉我!

    基本上,当我点击暂停时我只保存轨道的player.currentTime,当我点击播放时重新制作音轨,只是从我暂停它的那一刻开始 . 没有明显的延迟,但我仍然会更快乐而不浪费这些额外的处理 .

    确保在暂停后正确释放您的播放器项目,否则您最终会得到一大堆AVPlayers!

  • 0

    我的解决方案有点不那么hacky但仍然很hacky .

    解决方案来自于我注意到如果你在播放器上寻找,暂停引入的音频和视频之间的延迟消失了 .

    因此:只需在暂停之前保存 player.currentTime ,然后再播放 player seekToTime . 它在iOS 6上运行良好,还没有在其他版本上测试过 .

相关问题