Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.forms webview and view/play video in HTML5 video tag and android issue

I am capturing video using MediaPicker from xamarin.labs and then upload that video on my web server. Then after, I retrieve that video to show in WebView with HTML5 video tag.

This works fine for iOS. however the same code doesn't work on Android.

I create custom renderer in android to create Webchromeclient but it does not play video on android.

the sample html5 with video is (I also try with type parameter in <video> tag:

<Doctype! HTML>
<html>
<body><video src="www.myserver.com/video1.mp4" controls height="150" width="150"/>
</body>
</html/>

and this is my web view part in my PCL project:

public class MyWebView: WebView
{
}

this is my page:

public class VideoPage: ContentPage
{
   public VidoePage()
   {
      var webView = new MyWebView();
      webView.Source = new HtmlSource {Html = abovementionedHtml};
      var layout = new StackLayout()
     {
        Childern = {webWiew}
     };
     this.Content= layout;
}

Android renderer:

[assembly: ExportRenderer(typeof(MyWebView), typeof(MyWebViewRenderer))] 
namespace VideoSample.Droid
{

using Xamarin.Forms.Platform.Android;

public class MyWebViewRenderer : WebRenderer 
{
     protected override void OnElementChanged(ElementChangedEventArgs  e)
    {
        base.OnElementChanged(e);

        if (this.Control == null)
        {
            var webView = new global::Android.Webkit.WebView(this.Context);
            webView.SetWebChromeClient(new WebChromeClient());
            webView.Settings.JavaScriptEnabled = true;
            webView.Settings.SetPluginState(WebSettings.PluginState.On);
            this.SetNativeControl(webView);
        }
    }    
}
}

So can anyone tell me what is wrong ?

Thanks in advance.

like image 742
SoftSan Avatar asked Feb 25 '15 08:02

SoftSan


1 Answers

Try to replace the sample HTML as below with some fixes. Not sure if this is the root cause.

<!DOCTYPE html>
<html>
<body>
    <video src="http://www.myserver.com/video1.mp4" controls height="150" width="150">
</body>
</html>

I made the adjustments for two things:

  • The DOCTYPE
  • Adding "http://" at the beginning of the URL

Also, you may refer to this blog post (Making HTML5 Video work on Android phones) for further HTML tips and tricks on making video work.

like image 54
Alex Lau Avatar answered Nov 08 '22 14:11

Alex Lau