Play function does not work in iOS browser

Asked

Viewed 271 times

0

These functions below are from the Youtube API for viewing videos. It’s working on all browsers. But when I open the site on iPhone and click on PLAY to start the video, it doesn’t perform the action.

Placed click, touchstart and tap. None of the three work.

function onYouTubePlayerAPIReady(){
    player = new YT.Player('video-estoria-animada', {
        videoId: document.getElementById('video-id').value,
        playerVars: { 
            modestbranding: 1,
            version: 3,
            controls: 2,
            wmode: 'opaque',
            theme: 'light',
            rel: 1,
            showinfo: 1,
            html5: 1,
            listType: 'playlist',
            list: "PLA8fGhdnBVrGSjEYeCPozWTXGhRX2rdJ7",
        },
        events: {
            'onReady' : onPlayerReady,
            'onStateChange': onPlayerStateChange
        }
    });

    $(document).ready(function(){
        var clickDeviceEvent = 'ontouchstart' in window ? 'touchstart' : 'mousedown';

        //Estória animada play
        $('#video.estoria-animada > div.play-video').on(clickDeviceEvent, function(e){
            e.stopPropagation(); 
            e.preventDefault();
            player.playVideo();
        });

        $('#video-shadow').on(clickDeviceEvent, function(){
            player.pauseVideo();
        });

        //faz a mudança do tipo de video
        $('#change-legenda > div').on(clickDeviceEvent, function(){
            $('#change-legenda > div').removeClass('active');
            $(this).addClass('active');

            // Pega o tempo atual do video para setar ao trocar
            var currTime = player.getCurrentTime();

            if($(this).is('.sem-legenda')){
                player.loadVideoById($('#video-id').val(), currTime);
                player.unMute();
            }else{
                player.loadVideoById($('#video-id-muted').val(), currTime);
                player.mute();
            }
        });
    });
}

function onPlayerReady(event){
    player.addEventListener('onStateChange', onPlayerStateChange);
}

function onPlayerStateChange(event){
    var evento = event.data;
    if(evento == YT.PlayerState.PLAYING || evento == 1){
        $('#video.estoria-animada > div.play-video').addClass('hide');
        $('#video-shadow').delay(100).fadeIn(500);
    }
    else if(evento == YT.PlayerState.PAUSED || evento == 2){
        $('#video-shadow').delay(100).fadeOut(500);
    }
}

Just to mention the updates I’ve been making in this code.

I did a lot of research with the term: click Event Ios not Working. And I read most of the results. Many of them suggested putting the following property in the element of click.

cursor:pointer;

That would be enough to fix the bug iOS. What I’m searching for is nothing more than a bug. Now whether or not it is, I don’t know for sure.

I updated the code above also with what is currently. Explaining what happens:

On top of the video is a div with a button PLAY, that by clicking on it the div get off the video and start playback.

Even on my cell phone android 4.2 works that video. Less on iOS iPhone 4 and 4s.

  • Diego if you do this $('#video.estoria-animada > div.play-video').on('tap touchstart click', function() {
 alert("Play"); player.playVideo();
 }); Alert appears on iOS?

  • I’ll debug tomorrow and give you the feedback, man!

  • @Guilhermenascimento, sim aparece o Alert no iOS!

  • I noticed that on some iPhones work. Others do not.

  • Diego iPhone is a thing right rs, need to know which iOS does not work, I will test on mine, is iOS8.3.

  • Exactly... I said iPhone, but I’m talking about iOS. http://www.contosdatiasil.com.br - Go in animated stories.

  • 1

    It’s a security error see: www-widgetapi.js:24 Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://www.youtube.com') does not match the recipient window's origin and the API did not load recaptcha__pt_br.js:193 https://apis.google.com/js/api.js net::ERR_CONNECTION_CLOSED, This can even work eventually in some browsers, but I believe it will be a little difficult to work with this error at all, it seems to me that eventually some browsers ignore the errors and proceed, while others do not. I’ll test more as I have time.

  • Where can you see these errors ? In the iOS browser itself ? Appears in the Chrome console.

  • I saw on the Chrome desktop console, on Ios only having the Mac, I think it might be necessary to Xcode.

  • I have the Xcode and iOS emulator. I already tested it once, the problem is that it runs the video. uahuh

  • 1

    Ta well connect the cable of your iphone that does not work the player, open Safari and go to the Developer or Developer option. In the menu should appear your Iphone, like something like "iPhone Diego", select it and open the safari console (I think opens alone). On your iPhone with player problem try to play a video from the site, should appear a number of errors.

  • Safari doesn’t recognize iPhone Guilherme. I can’t see the error.

  • On your iPhone you go to Settings > safari > Advanced > Enable "Web Inspector". Reconnect your iPhone on your mac.

  • Appears unable to post message www.youtube.com.... Same as in the error you showed above.

  • 1

    seems to be an error of origin (security lock), maybe with iframe, I will try to find the problem later.

  • I edited my pole.

Show 11 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.