0
votes

I am building an iOS app that uses the youtube-ios-player-helper library to play YouTube videos. I'm trying to set the playback speed, but nothing happens:

class PlayerViewController: UIViewController {

    private let player = YTPlayerView()

    override func viewDidLoad() {
        super.viewDidLoad()

        player.delegate = self
        view.addSubview(player)
        setupPlayerConstraints()

        player.load(withVideoId: "123456")
        // Tried here, but nothing change: 
        print("Available playback rates: \(String(describing: player.availablePlaybackRates()))")
        player.setPlaybackRate(2.0)
    }

}

extension PlayerViewController: YTPlayerViewDelegate {

    func playerViewDidBecomeReady(_ playerView: YTPlayerView) {
        player.playVideo()
    }

    func playerView(_ playerView: YTPlayerView, didChangeTo state: YTPlayerState) {
        switch (state) {
        case.playing:
            // Tried here, but again, nothing change:
            print("Available playback rates: \(String(describing: player.availablePlaybackRates()))")
            player.setPlaybackRate(2.0)
        default:
            break;
        }
    }

}

As shown by the code above, I've tried setting playback speed after loading the video and also when player changes it's state to playing. In none of then the speed was changed.

Also, player.availablePlaybackRates returns nil in both cases (that's strange because when I watch the same video using the YouTube app, I can change the playback speed).

I know that setting the playback speed is a suggestion to the player, but on the official YouTube app, changing the speed works for the same video that I'm trying to watch on my app.

Is there anything that I'm missing here?

1
Don't think this is possible yet. This SO post to confirms it.noogui
Oh gosh, guess you're right... And it doesn't seem they're going to fix it. Last commit was almost 2 years agoMarcos Tanaka

1 Answers

0
votes

I had the same problem. But I solved it like this and it works well. in swift 4

  1. private -> public

    private func stringFromEvaluingJavaScript(jsToExecute: String) -> String{
        Guard let result = self.webView.stringByEvaluingJavaScript (from jsToExecute) else{
        return ""
        }
    return result
    }
    
  2. directly Execute

    playerView.stringFromEvaluingJavaScript(jsToExecute): "player.setPlaybackRate(1.5);")