首页 文章

Javascript HTML5 play()无法在ios9 Safari中运行?

提问于
浏览
6

我有一个基本的HTML5视频播放器没有像这样的控件...

<video id="videoPlayer" preload="auto" webkit-playsinline>
<source id="videoSourceMP4" src="" type="video/mp4" />
<source id="videoSourceOGG" src="" type="video/ogg" />
</video>

顶部有一个按钮,按下后会加载所需的视频:

document.getElementById("videoSourceMP4").src = "videos/video.mp4";
document.getElementById("videoSourceOGG").src = "videos/video.ogg";
document.getElementById("videoPlayer").load();

我现在也开始检查视频是否可以播放并且应该开始播放:

document.getElementById("videoPlayer").oncanplaythrough = function(){
document.getElementById("videoPlayer").play();
}

这在ios8中有效,但是自从ios9没有任何反应 . 视频卡在第一帧上,无法播放 . 即使我在play()上添加另一个按钮,也没有任何反应 . 我必须启用控件并使用提供的播放按钮播放它 . 如果我暂停视频,然后按下我自己的播放按钮,它将起作用 . 我希望能够使用我自己设计的控件 .

有谁知道为什么现在这样做?

1 回答

  • 0

    以下代码的工作原理如下:

    • iOS 9.2.1 - 按下按钮时视频全屏播放

    • 在主屏幕上,视频以内嵌方式播放

    • iOS 9.3 beta 1 - 按下按钮时视频会内联播放

    • 在主屏幕上,视频以内嵌方式播放

    视频文件来自http://techslides.com/sample-webm-ogg-and-mp4-video-files-for-html5

    <html>
    <head>
        <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
        <meta name="apple-mobile-web-app-capable" content="yes" />
        <style>
        video{
            width:400px;
            border: 2px dashed black;
        }
        button{
            font-size:2em;
            padding:1em;
        }
        </style>
    </head>
    <body onload='init()'>
        <video id="videoPlayer" preload="auto" webkit-playsinline>
            <source id="videoSourceMP4" src="" type="video/mp4" />
            <source id="videoSourceOGG" src="" type="video/ogg" />
        </video>
        
    <button type='button' onclick='go()'>Play</button> <script> function init(){ document.getElementById("videoPlayer").oncanplaythrough = function(){ document.getElementById("videoPlayer").play(); } } function go(){ document.getElementById("videoSourceMP4").src = "small.mp4"; document.getElementById("videoSourceOGG").src = "small.ogg"; document.getElementById("videoPlayer").load(); } </script> </body>

相关问题