在Swift中使用属性canPlayReverse时,为什么我的音乐没有反向播放?

时间:2016-04-18 18:41:33

标签: ios swift avplayer

我试图通过使用canPlayReverse属性反向播放我的音乐,但这不起作用。当我尝试反向播放时,它向前发展。以下是我的代码:谢谢!

class GameScene: SKScene, MPMediaPickerControllerDelegate, AVAudioPlayerDelegate {

private var player:AVPlayer! = AVPlayer()

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); M_PI
            if delta > 0 {
                print("rotateleft")
                if musicPlayer == player.currentItem?.canPlayReverse {
                    musicPlayer.rate = -1.0

                }

            } else {

                print("rotateright")
                musicPlayer.rate = 3.0
                musicPlayer.play()
            }

            previousAngle = angle2
   }

1 个答案:

答案 0 :(得分:1)

当您使用asset!时,您告诉编译器该变量将指向一个有效的对象,但您的代码并未将asset设置为任何非零值。< / p>

一般来说,避免&#39;!&#39;尽可能测试您的选项。