【问题标题】:Is there a way to rewind music using AVAudioPlayer?有没有办法使用 AVAudioPlayer 倒带音乐?
【发布时间】:2016-04-14 03:07:14
【问题描述】:

我正在使用 AVAudioPlayer 播放音乐,需要帮助来倒带音乐。我使用rate = 2.0 让它快进,但现在我不确定如何使用 rate 属性或其他东西来倒带音乐。有人可以帮我解决这个问题。谢谢!

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 {

                //rewind code
                print("rotateleft")
            }
            else {
                print("rotateright")

                musicPlayer.rate = 2
                musicPlayer.play()


            }
            previousAngle = angle2
       }

【问题讨论】:

    标签: ios xcode swift swift2 avaudioplayer


    【解决方案1】:

    AVAudioplayer 不提供任何直接倒带音乐的方法。但是您可以通过滑块/按钮或任何其他方式手动设置“currentTime”属性

    来自苹果文档: currentTime

    通过设置此属性,您可以寻找声音中的特定点 文件或实现音频快进和快退功能。


    或者,如果您想使用 rate 属性,您可以使用AVPlayer。它还支持以速率倒带: 您可以在此处找到有关 AVPlayer 倒带的更多信息:https://stackoverflow.com/a/10761446/4557505

    【讨论】:

    • 是的,但我必须将很多代码更改为 AVPlayer 才能使其正常工作。我尝试使用当前时间并倒带,但倒带时听不到任何声音。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 2019-05-26
    • 2023-03-03
    相关资源
    最近更新 更多