我正在通過設置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