我正在通过设置rate
值快速转发我的AVAudioPlayer
。但我无法弄清楚如何倒带。有没有办法在Swift中使用rate属性重放音乐?
我正在制作一个DJ应用程序,其中音乐播放rate
在旋转转盘时发生变化,导致音乐快进和倒带(同时仍然听到声音)。
这是我目前转盘纺纱的方法。我怎样才能倒回AVAudioPlayer
?
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
let node = self.nodeAtPoint(location)
//this is for the left turntable
if node.name == "lefttt" {
//lets user rotate left turntable when there is one finger on turntable.
let dy = leftTurntable.position.y - location.y
let dx = leftTurntable.position.x - location.x
let angle2 = atan2(dy, dx)
leftTurntable.zRotation = angle2
let delta = (angle2 - previousAngle)
if delta > 0 {
// Code to rewind music goes here....
print("rotateleft")
} else {
//This code works perfectly and fast forwards the music when spinning turntable to right
print("rotateright")
musicPlayer.rate = 2.0
musicPlayer.play()
}
previousAngle = angle2
}
您是否尝试将2.0添加到musicPlayer.currentTime而不是减法?也许它会从剩下的时间开始倒数。 –
是的,我尝试添加,它仍然快速向前。 – coding22