6

我正在尝试使用 AVMutableComposition 在精确的时间播放一系列声音文件。

当视图加载时,我创建的目的是在 1 秒内均匀地播放 4 个声音。声音的长短无关紧要,我只想在 0、0.25、0.5 和 0.75 秒时触发它们:

AVMutableComposition *composition = [[AVMutableComposition alloc] init];
NSDictionary *options = @{AVURLAssetPreferPreciseDurationAndTimingKey : @YES};

for (NSInteger i = 0; i < 4; i++)
{
  AVMutableCompositionTrack* track = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
  NSURL *url = [[NSBundle mainBundle] URLForResource:[NSString stringWithFormat:@"sound_file_%i", i] withExtension:@"caf"];
  AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:options];
  AVAssetTrack *assetTrack = [asset tracksWithMediaType:AVMediaTypeAudio].firstObject;
  CMTimeRange timeRange = [assetTrack timeRange];
  Float64 t = i * 0.25;
  NSError *error;
  BOOL success = [track insertTimeRange:timeRange ofTrack:assetTrack atTime:CMTimeMakeWithSeconds(t, 1) error:&error];
  if (!success)
  {
    NSLog(@"unsuccesful creation of composition");
  }
  if (error)
  {
    NSLog(@"composition creation error: %@", error);
  }
}

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

合成成功创建,没有错误。后来,当我想播放序列时,我这样做:

[self.avPlayer seekToTime:CMTimeMakeWithSeconds(0, 1)];
[self.avPlayer play];

出于某种原因,这些声音根本不是均匀分布的——而是几乎同时播放所有声音。我尝试了同样的事情,间隔超过 4 秒,替换了这样的时间计算:

Float64 t = i * 1.0;

这玩得很完美。任何低于 1 秒的时间间隔似乎都会产生意想不到的结果。我错过了什么?AVCompositions 不应该用于 1 秒以下的时间间隔吗?还是我误解了时间间隔?

4

3 回答 3

2

CMTimeMakeWithSeconds(t, 1)在整秒的“切片”中,因为你的时间刻度设置为 1。无论分数t是多少,atTime: 将始终以 0 结束。这就是为什么当你将它增加到 1 秒时它起作用的原因(t=i*1 )。

您需要将时间刻度设置为 4 以获得所需的 0.25 秒切片。由于 CMTime 现在以 0.25 秒为单位,因此您不需要 i * 0.25 计算。直接使用i即可;atTime:CMTimeMake(i, 4)

如果您将来可能需要更精确,您现在应该考虑到它,这样您以后就不必调整您的代码。Apple 建议使用 600 的时间刻度,因为它是常见视频帧速率(24、25 和 30 FPS)的倍数,但它也适用于纯音频。因此,对于您的情况,您将使用 24 个切片来获得 0.25 秒的值;Float64 t = i * 24;atTime:CMTimeMake(t, 600)

至于您几乎同时播放所有 4 种声音的问题,请注意这个未回答的 SO 问题,它只在第一次播放时发生。即使进行了上述更改,您仍可能会遇到此问题。

于 2014-08-27T17:09:52.130 回答
1

除非每首曲目的长度正好为 0.25 秒,否则这是您的问题:

Float64 t = i * 0.25;
NSError *error;
BOOL success = [track insertTimeRange:timeRange ofTrack:assetTrack atTime:CMTimeMakeWithSeconds(t, 1) error:&error];

您需要跟踪到目前为止添加的累积时间范围,并在那时插入下一首曲目:

CMTime currentTime = kCMTimeZero;

for (NSInteger i = 0; i < 4; i++) {

   /* Code to create track for insertion */

    CMTimeRange trackTimeRange = [assetTrack timeRange];

    BOOL success = [track insertTimeRange:trackTimeRange
                                  ofTrack:assetTrack 
                                    atTime:currentTime
                                      error:&error];

    /* Error checking code */

    //Update time range for insertion
    currentTime = CMTimeAdd(currentTime,trackTimeRange.duration);
}
于 2014-08-25T21:06:50.063 回答
0

我改变了一点你的代码,抱歉我没有时间测试它。

   AVMutableComposition *composition = [AVMutableComposition composition];
    NSDictionary *options = @{AVURLAssetPreferPreciseDurationAndTimingKey : @YES};

    CMTime totalDuration = kCMTimeZero;

    for (NSInteger i = 0; i < 4; i++)
    {
        AVMutableCompositionTrack* track = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];


        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"Record_%i", i] ofType:@"caf"]];
        AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:options];
        AVAssetTrack *assetTrack = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
        CMTimeRange timeRange = [assetTrack timeRange];
        NSError *error;

        BOOL success = [track insertTimeRange:timeRange ofTrack:assetTrack atTime:CMTIME_COMPARE_INLINE(totalDuration, >, kCMTimeZero)? CMTimeAdd(totalDuration, CMTimeMake(1, 4)): totalDuration error:&error];

        if (!success)
        {
            NSLog(@"unsuccesful creation of composition");
        }
        if (error)
        {
            NSLog(@"composition creation error: %@", error);
        }
        totalDuration = CMTimeAdd(CMTimeAdd(totalDuration,CMTimeMake(1, 4)), asset.duration);
    }

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

PS 使用kCMTimeZero而不是CMTimeMakeWithSeconds(0, 1).

于 2014-08-21T07:23:55.110 回答