AVAudioEngine音频文件时间管理



我正试图更改用滑块播放的歌曲的播放时间。

我的问题是获得准确的AVAudioTime来安排基于滑块的播放

代码(swift)看起来像:

@IBAction func ChangeAudioTime(sender: AnyObject) {
 //get current sampleRate of song already playing

 var nodetime: AVAudioTime  = self.playerNode.lastRenderTime

 var playerTime: AVAudioTime = self.playerNode.playerTimeForNodeTime(nodetime)

 playerNode.stop()
 var time: NSTimeInterval = NSTimeInterval(Slider.value)
 var hostTime = AVAudioTime.hostTimeForSeconds(time)
 var sampleTime = AVAudioFramePosition(hosttime/UInt64(playerTime.sampleRate))
 var ExampleTime: AVAudioTime = AVAudioTime(hostTime:hostTime, sampleTime: sampleTime, atRate: playerTime.sampleRate)

 playerNode.scheduleFile(audioFile, atTime:ExampleTime, completionHandler:nil)
      playerNode.play()
}

滑块的最大值被指定为歌曲的长度,所以这不是问题所在。

这首歌从开始播放

控制台中ExampleTime的输出为:

<AVAudioTime 0x1702b4160: 147.874222 s 80475 fr (/44100 Hz)>

我做错了什么?

我认为这就是您所需要的:

-(void)playAtTime:(AVAudioTime*)aTime {
startingFrame = _currentTime * self.file.processingFormat.sampleRate;
AVAudioFrameCount frameCount = (AVAudioFrameCount)(self.file.length - startingFrame);
    [_player scheduleSegment:self.file
               startingFrame:startingFrame
                  frameCount:frameCount
                      atTime:aTime
           completionHandler:^{
                NSLog(@"done playing");//actually done scheduling.
            }];
    [_player play];

}

scheduleFile:atTime实际上播放整个文件,atTime是它启动的时候。我自己还在想办法。这应该是未来的一段时间,

在本例中,_currentTime(以秒为单位)是您想要在歌曲中开始的位置。

最新更新