2016-04-15 73 views
0

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

您是否嘗試將2.0添加到musicPlayer.currentTime而不是減法?也許它會從剩下的時間開始倒數。 –

+0

是的,我嘗試添加,它仍然快速向前。 – coding22

回答

0

您可以通過將currentTime設置爲小於其當前值的值來倒帶。

我不認爲這會倒帶一些聽得到的方式。你不會奇蹟般地聽到音樂倒退或類似的東西。 AVAudioPlayer不這樣做。但是,它會改變比賽位置,這樣當你再次開始比賽時(比賽前),你會比你更早開始比賽。

AVPlayer(而不是AVAudioPlayer)可以反向播放,但只有在特定AVPlayerItem允許的情況下才能播放。

+1

我告訴你的結果基本上與你在這裏提出的問題在這裏相同:http://stackoverflow.com/questions/36587401/how-do-i-fastforward-using-avaudioplayer-while-hearing音樂而不是多次詢問同一個問題,試着聽聽你給出的答案。 – matt

+0

這個問題是關於倒帶,而不是快進。這個問題不是http://stackoverflow.com/questions/36587401/how-do-i-fastforward-using-avaudioplayer-while-hearing-the-music的重複。 OP顯然知道如何快進(按照包含的「touchesMoved」方法) –

+0

@AleksanderAzizi它是重複的,因爲他已經被告知他不能用AVAudioPlayer向後播放。他不斷問相同的問題,因爲他不接受這個事實。 – matt

相關問題