18

I want to play video url inside my application webview but when i am run the application it showing only white screen . i had read some post on this and i have used that code but video is not playing in webview it launch device player but my requirement not this .

please help me if some one already done this . my code is here :-

    WebView webView = (WebView) findViewById(R.id.myweb);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setPluginState(PluginState.ON);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setSupportMultipleWindows(true);
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setAllowFileAccess(true);
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {
            setProgressBarIndeterminateVisibility(false);
            super.onPageFinished(view, url);
        }
    });
    webView.loadUrl("http://download.wavetlan.com/SVV/Media/HTTP/H264/Talkinghead_Media/H264_test1_Talkinghead_mp4_480x360.mp4");
    webView.setVisibility(View.VISIBLE);

Testing URL is working in browser .

Alok Tiwari
  • 607
  • 2
  • 7
  • 21

11 Answers11

20
WebView webview = (WebView) findViewById(R.id.webView1);
    webview.setWebViewClient(new WebViewClient());
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webview.getSettings().setPluginState(WebSettings.PluginState.ON);
    webview.getSettings().setMediaPlaybackRequiresUserGesture(false);
    webview.setWebChromeClient(new WebChromeClient());
    webview.loadUrl("https://www.youtube.com");

also set in manifist file

 <application
    android:allowBackup="true"
    android:hardwareAccelerated="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
Max
  • 519
  • 1
  • 7
  • 14
wadali
  • 2,221
  • 1
  • 20
  • 38
  • This is not playing video directly in webview, rather its opening browser which is not the solution to the problem. – Muhammad Umair Shafique Apr 07 '17 at 06:14
  • 1
    It is playing video which comes from URL and it's opening in webview . Can you explain why it's not a solution . If you have some better idea please share @Muhammad – wadali Apr 12 '17 at 16:43
  • 1
    `pluginState` - deprecated, plugins are not supported in Kitkat or later – hmac Dec 19 '19 at 15:59
2

I know this is an old issue, I had the same problem some days back and surfing across internet I found this complete source that helped me a lot, and now i want to share with others, maybe can help others like me..

https://github.com/cprcrack/VideoEnabledWebView

PRATEEK BHARDWAJ
  • 2,364
  • 2
  • 21
  • 35
Ruben Flores
  • 331
  • 1
  • 3
  • 13
2

If tried others anwsers no work,maybe you can try this:

 if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.LOLLIPOP){
        webview.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
    }
Kevin Wu
  • 71
  • 1
  • 3
1

Add webview.setWebChromeClient(new WebChromeClient()); and to enable plugins for your video add:

  if (Build.VERSION.SDK_INT < 8) {
     webview.getSettings().setPluginsEnabled(true);
  }  
  else {
     webview.getSettings().setPluginState(PluginState.ON);
  }

Hope this helps.

Gurfuffle
  • 784
  • 12
  • 32
Siddharth_Vyas
  • 9,972
  • 10
  • 39
  • 69
1

You need to load an HTML document that embeds the video inside of it, instead of the video directly. To play inline video in WebView using the <video> tag you must also ensure the hardware acceleration is enabled in your application (see http://developer.android.com/guide/topics/graphics/hardware-accel.html).

ksasq
  • 4,424
  • 2
  • 18
  • 10
1

This may be an old question but this works:

    //Here Im loading the html file from disk
    String html_file = LoadData(file.getAbsolutePath());

    WebView myWebView = (WebView) view.findViewById(R.id.webView);
    myWebView.reload();
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.loadData(html_file, "text/html; charset="utf-8", "UTF-8");

Use base64 encoded video data as your video source:

//html_file
<video controls>
   <source type="video/mp4" src="data:video/webm;base64,MY_BASE64_ENCODED VIDEO_HERE">
</video>
Jupiter
  • 615
  • 1
  • 6
  • 15
1

For me this was the solution

settings.setDomStorageEnabled(true);
0

No you can't play the video by link in this case. To do this you should create an RTSP connection then you can play the video through an RTSP url in Webview. You can retrieve an RTSP connection from a server like Youtube.

Gurfuffle
  • 784
  • 12
  • 32
Saunik Singh
  • 996
  • 1
  • 9
  • 19
0

Try this.

{
    WebSettings settings = webView.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setDomStorageEnabled(true);
    settings.setMinimumFontSize(10);
    settings.setLoadWithOverviewMode(true);
    settings.setUseWideViewPort(true);
    settings.setBuiltInZoomControls(true);
    settings.setDisplayZoomControls(false);
    webView.setVerticalScrollBarEnabled(false);         
    webView.loadDataWithBaseURL(null, vUrl, "text/html", "UTF-8", null);
}
noufalcep
  • 3,446
  • 15
  • 33
  • 51
0

I also got stuck with this issue. I got correct response from server, but couldn`t play video. After long time I found a solution here. Maybe, in future this link will be invalid. So, here is my correct code

Uri video = Uri.parse("Your link should be in this place "); 
mVideoView.setVideoURI(video); 

mVideoView.setZOrderOnTop(true); //Very important line, add it to Your code
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
    @Override 
    public void onPrepared(MediaPlayer mediaPlayer) {
   // here write another part of code, which provides starting the video
}}
Roman Soviak
  • 791
  • 2
  • 9
  • 30
0

If you use Kotlin, this code can be useful:

@SuppressLint("SetJavaScriptEnabled")
    val url = "https://..."
    val webView = findViewById<WebView>(R.id.webView)

    webView.webViewClient = WebViewClient()
    webView.settings.javaScriptCanOpenWindowsAutomatically = true
    webView.settings.javaScriptEnabled = true
    webView.settings.setSupportZoom(true)
    webView.settings.mediaPlaybackRequiresUserGesture = false
   
    webView.loadUrl(url)
Mori
  • 2,653
  • 18
  • 24