lundi 18 juin 2018

Iframe API getVideoLoadedFraction

I am wondering why the if statement is not working. Inside Iframe Documentation there is something called player.getVideoLoadedFraction() and it gives a value between 0 and 1.

0 if the video is still buffering and 1 when the video has just started playing, I'm using UIWebView to load the video and it works fine I can even play and pause the video but when i simply check for when player.getVideoLoadedFraction() is 1 so i can be sure that the video has started and get information like duration and that then it never works.

func webViewDidFinishLoad(_ webView: UIWebView) {

        //check if we are still loading
        if VideoWebView.isLoading {
            return
        }

        print("done loading")

        // if it is false then we are not loading anymore and completly loaded the content

        guard let videoLoaded = VideoWebView.stringByEvaluatingJavaScript(from: "ytplayer.getVideoLoadedFraction()")else { return }

        if videoLoaded == "1" {

            print(VideoWebView.stringByEvaluatingJavaScript(from: "ytplayer.getCurrentTime()"))

        }


    }

}

In my case i use ytplayer.getVideoLoadedFraction() if you are wondering, as you can see i check this stuff when the Webview did finish load it's data but still the if statement won' be called.

Just for testing purpose I tested when I change the value of a UISlider that i have, then the IF statement gets called that code looks something like this.

lazy var videoSlider: UISlider = {
        let slider = UISlider()
        slider.minimumTrackTintColor = .red
        slider.maximumTrackTintColor = .white
        slider.translatesAutoresizingMaskIntoConstraints = false
        slider.setThumbImage(#imageLiteral(resourceName: "thumb"), for: .normal)

        slider.addTarget(self, action: #selector(handleSliderChange), for: .valueChanged)

        return slider
    }()

Here is the function

@objc func handleSliderChange() {

        guard let videoLoaded = VideoWebView.stringByEvaluatingJavaScript(from: "ytplayer.getVideoLoadedFraction()")else { return }

        if videoLoaded == "1" {

            print(VideoWebView.stringByEvaluatingJavaScript(from: "ytplayer.getCurrentTime()"))

        }

}

How do I solve this problem so when the Webview loads and ytplayer.getVideoLoadedFraction() is 1, I can for example print(VideoWebView.stringByEvaluatingJavaScript(from: "ytplayer.getCurrentTime()"))

Aucun commentaire:

Enregistrer un commentaire