我最近正在尝试更改正在使用滑块播放的歌曲的播放时间。
我的问题是如何根据滑块获得准确的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)>
我做错了什么?
发布于 2015-07-24 15:21:48
我想这就是你需要的:
-(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 (以秒为单位)是歌曲中您想要开始的位置。
https://stackoverflow.com/questions/29152659
复制相似问题