This question already has an answer here:
How do I fastforward using AVAudioplayer while hearing the music?
                                
                                    (1个答案)
                                
                        
                                3年前关闭。
            
                    
我通过设置AVAudioPlayer值来快速转发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
 }

最佳答案

您可以通过将currentTime设置为小于当前值的值来倒带。

我认为这不会以听得见的方式倒带。您不会魔术般地听到音乐倒退或类似的声音。 AVAudioPlayer不会这样做。但是,它将更改播放位置,以便当您再次开始播放(向前)时,您将比以前更早开始。

AVPlayer(与AVAudioPlayer相对)可以反向播放,但前提是特定的AVPlayerItem允许它播放。

关于ios - 有没有一种方法可以在Swift中使用rate属性来倒带音乐? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36638209/

10-11 14:37