2016-04-15 77 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

相关问题