gpt4 book ai didi

ios - AVAudioEngine AudioFile 时间管理

转载 作者:行者123 更新时间:2023-11-29 02:12:18 25 4
gpt4 key购买 nike

我目前正在尝试更改正在使用 slider 播放的歌曲的播放时间。

我的问题是获取准确的 AVAudioTime 以根据 slider 安排播放

代码(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()
}

slider 的最大值分配给歌曲的长度,所以这不是问题。

歌曲从头开始播放

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 是它开始的时间。我仍在弄清楚它是如何工作的。应该是 future 的某个时间,

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

关于ios - AVAudioEngine AudioFile 时间管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29152659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com