简体   繁体   English

HTML5视频无法在Android上播放

[英]HTML5 video doesnt play with android

I have got the following html code: 我有以下html代码:

<span id="video-teaser">
    <span>Click here for a <strong>FREE</strong> hot video</span>
    <video src="http://example.com/videos/nakednews.mp4" controls="" poster="http://example.com//img/iphone-video-btn.png" id="videoTeaser" height="1"></video>
</span>

I tried to come up with two solutions.. use javascript addlistener..and trigger the video when it is pressed. 我试图提出两种解决方案..使用javascript addlistener ..并在按下视频时触发视频。

     <script type="text/javascript">
            var video = document.getElementById('videoTeaser');
            var elementToTrigger=document.getElementById('video-teaser');
                    elementToTrigger.addEventListener('click',function(){
                    video.play();
                    },false);
      </script>

Both of the solutions dont work.. the video itself works when I click on the video-teaser span, on the pc version of the site.. 这两种解决方案都无法使用。.当我单击网站的PC版本上的视频预告片时,视频本身可以工作。

What could be the solution to this.. 什么可能是解决方案。

尝试在http://code.google.com/p/html5webview/source/checkout的 svn链接中给出的代码可以正常工作

This is a known issue for Android < 4.0 这是Android <4.0的已知问题

This is a workadroud : 这是一个工作方法:

In your webview you have to set your own ChromeClient 在您的网络视图中,您必须设置自己的ChromeClient
and override this function : 并覆盖此功能:

        _customViewCallback = callback;
        if (view instanceof FrameLayout){  
             FrameLayout frame = (FrameLayout) view;  
             if (frame.getFocusedChild() instanceof VideoView){  
                 final VideoView video = (VideoView) frame.getFocusedChild(); 
                 _customView = video;
                 frame.removeView(video);  
                 Activity a = (Activity)_context;
                 a.setContentView(video);  
                 video.setOnCompletionListener(new OnCompletionListener() {  

                    @Override  
                    public void onCompletion(MediaPlayer mp) { 
                        mp.stop();
                        video.stopPlayback();
                        onHideCustomView();
                    } 
                });  
                 video.setOnErrorListener(new OnErrorListener() {  

                    @Override  
                    public boolean onError(MediaPlayer mp, int what, int extra) {  
                        return false;  
                    }  
                });  
                 if (!video.isPlaying())
                     video.start();  
             }  
         }  

And override OnHideCustomView to add your treatment and add(so that your video can open another time) : 并重写OnHideCustomView来添加您的处理并添加(以便您的视频可以再次打开):

_customViewCallback.onCustomViewHidden();

These links might be helpful to you : 这些链接可能对您有帮助:

WebView and HTML5 <video> WebView和HTML5 <视频>

How to play a video in a webview with android? 如何使用Android在WebView中播放视频?

WebView NOT opening android default video player? WebView无法打开android默认视频播放器吗?

Code inside the script tag will work after loading content of span tag Put the script tag below the span tag as below 加载span标签的内容后,script标签内的代码将起作用。将script标签放在span标签下面,如下所示

<span id="video-teaser">
    <span>Click here for a <strong>FREE</strong> hot video</span>
    <video src="http://example.com/videos/nakednews.mp4" controls="" poster="http://example.com//img/iphone-video-btn.png" id="videoTeaser" height="1"></video>
</span>

 <script type="text/javascript">
            var video = document.getElementById('videoTeaser');
            var elementToTrigger=document.getElementById('video-teaser');
                    elementToTrigger.addEventListener('click',function(){
                    video.play();
                    },false);
      </script>
</body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM