HTML5 <video> element on Android

后端 未结 15 2140
逝去的感伤
逝去的感伤 2020-11-22 15:09

According to:

http://developer.android.com/sdk/android-2.0-highlights.html

Android 2.0 should support the HTML5 video element. I haven\'t been able to get th

相关标签:
15条回答
  • 2020-11-22 15:43

    This might not exactly answer your question, but we're using the 3GP or 3GP2 file format. Better even to use the rtsp protocol, but the Android browser will also recognize the 3GP file format.

    You can use something like

    self.location = URL_OF_YOUR_3GP_FILE
    

    to trigger the video player. The file will be streamed and after playback ends, handling is returned to the browser.

    For me this solves a lot of problems with current video tag implementation on android devices.

    0 讨论(0)
  • 2020-11-22 15:47

    I tried using the .mp4 format to play a video on Android devices but that did not go well. So after some trial and error, I converted the video into the .webm format and following code with no extra javascript or JQuery:

    <video id="video" class="video" muted loop autoplay>
        <source src="../media/some_video.webm" type="video/webm">
        Sorry, your browser doesn't support embedded videos.
    </video>
    

    It worked on an older Android device (at least a few years old as of 2020).

    0 讨论(0)
  • 2020-11-22 15:48

    Here I include how a friend of mine solved the problem of displaying videos in HTML in Nexus One:

    I never was able to make the video play inline. Actually many people on the internet mention explicitly that inline video play in HTML is supported since Honeycomb, and we were fighting with Froyo and Gingerbread... Also for smaller phones I think that playing full screen is very natural - otherwise not so much is visible. So the goal was to make the video open in full screen. However, the proposed solutions in this thread did not work for us - clicking on the element triggered nothing. Furthermore the video controls were shown, but no poster was displayed so the user experience was even weirder. So what he did was the following:

    Expose native code to the HTML to be callable via javascript:

    JavaScriptInterface jsInterface = new JavaScriptInterface(this);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.addJavascriptInterface(jsInterface, "JSInterface");
    

    The code itself, had a function that called native activity to play the video:

    public class JavaScriptInterface {
        private Activity activity;
    
        public JavaScriptInterface(Activity activiy) {
            this.activity = activiy;
        }
    
        public void startVideo(String videoAddress){
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(videoAddress), "video/3gpp"); // The Mime type can actually be determined from the file
            activity.startActivity(intent);
        }
    }
    

    Then in the HTML itself he kept on failing make the video tag work playing the video. Thus, finally he decided to overwrite the onclick event of the video, making it do the actual play. This almost worked for him - except for no poster was displayed. Here comes the most weird part - he kept on receiving ERROR/AndroidRuntime(7391): java.lang.RuntimeException: Null or empty value for header "Host" every time he set the poster attribute of the tag. Finally he found the issue, which was very weird - it turned out that he had kept the source subtag in the video tag, but never used it. And weird enough exactly this was causing the problem. Now see his definition of the video section:

    <video width="320" height="240" controls="controls" poster='poster.gif'  onclick="playVideo('file:///sdcard/test.3gp');" >
       Your browser does not support the video tag.
    </video>
    

    Of course you need to also add the definition of the javascript function in the head of the page:

    <script>
      function playVideo(video){
        window.JSInterface.startVideo(video);
      }
    </script>
    

    I realize this is not purely HTML solution, but is the best we were able to do for Nexus One type of phone. All credits for this solution go to Dimitar Zlatkov Dimitrov.

    0 讨论(0)
提交回复
热议问题