Webview Xamarin.forms and view / play video in HTML5 tag and Android issues

I take a video using MediaPicker from xamarin.labs and then upload this video to my web server. Then after that I retrieve this video for display in WebView using the HTML5 tag.

This works great for iOS. however, the same code does not work on Android.

I am creating my own renderer in android to create a Webchromeclient , but it does not play video on android.

html5 sample with video (I am also trying to use the type parameter in the <video> :

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

and this is my part of the web view 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 someone tell me what is wrong?

Thanks in advance.

+5
source share
1 answer

Try replacing the HTML sample as shown 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 corrections for two things:

  • DOCTYPE
  • Add "http: //" at the beginning of the URL

In addition, you can refer to this blog post (creating HTML5 video on Android phones) for additional HTML tips and tricks for creating video work.

+1
source

Source: https://habr.com/ru/post/1214142/


All Articles