在Android Webview上显示html5视频。视频在播放时变黑

时间:2017-07-24 10:03:06

标签: android video android-webview html5-video

在Android Webview上显示视频。播放和声音时视频变黑,但视频屏幕变黑。我正在研究android 15+版本。玩这个需要什么?..一些设备将正常工作..

Xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.gowsample.MainActivity" >
<WebView
    android:id="@+id/webView1"
   android:layout_width="match_parent"
     android:layout_height="wrap_content"

   />

<TextView
    android:id="@+id/sam"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/webView1"
    android:layout_below="@+id/webView1"
    android:layout_marginTop="36dp"
    android:text="@string/hello_world" />

MainActivity Code:

package com.example.gowsample;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;


public class MainActivity extends Activity {
 private WebView mWebview ;

@SuppressLint({ "NewApi", "SetJavaScriptEnabled" }) @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mWebview  = (WebView)findViewById(R.id.webView1);
    mWebview.getSettings().setMediaPlaybackRequiresUserGesture(false);
    mWebview.getSettings().setJavaScriptEnabled(true);




   String myvar = "";

   String head = "<!DOCTYPE html><html><head><meta name=\"viewport\" content=\"width=device-width, user-scalable=yes\" /></head>";

   String summary = "<style>table{ height:100%;}td.height{height:100%;}</style><table width=100% height=100%> <tr><td class=\"height\" style=\"text-align: center; vertical-align: middle;\"><video id='my-video' controls autoplay style=\"width: 350px; height: 250px;vertical-align: middle;\"><source src='http://techslides.com/demos/sample-videos/small.mp4' type='video/mp4' /></video></td></tr></table>";

  String html = head + "<body>"+summary+"</body></html>";
  mWebview.loadData(html, "text/html", null);  
  }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

3 个答案:

答案 0 :(得分:6)

尝试以下代码为我工作..

package com.example.gowsample;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebChromeClient;
import android.webkit.WebView;


public class MainActivity extends Activity {
private WebView mWebview ;

@SuppressLint({ "NewApi", "SetJavaScriptEnabled" }) @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mWebview  = (WebView)findViewById(R.id.webView1);

    mWebview.getSettings().setJavaScriptEnabled(true);
    mWebview.setWebChromeClient(new WebChromeClient());
    mWebview.getSettings().setMediaPlaybackRequiresUserGesture(false);



   String myvar = "";

   String head = "<!DOCTYPE html><html><head><meta name=\"viewport\" content=\"width=device-width, user-scalable=yes\" /></head>";

   String summary = "<style>table{ height:100%;}td.height{height:100%;}</style><table width=100% height=100%> <tr><td class=\"height\" style=\"text-align: center; vertical-align: middle;\"><video id='my-video' controls autoplay style=\"width: 300px; height: 250px;vertical-align: middle;\"><source src='http://techslides.com/demos/sample-videos/small.mp4' type='video/mp4' /></video></td></tr></table><script>var myvideo = document.getElementsByTagName('video')[0]; myvideo.play()</script>";

   String html = head + "<body style='background-color:#000000;'>"+summary+"</body></html>";
   mWebview.loadData(html, "text/html", null);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

答案 1 :(得分:0)

请发布代码以帮助您。无论如何在你的Android清单中使用它。

android:hardwareAccelerated="true"

答案 2 :(得分:0)

我设法使用hardwareAccelerated =“ true”从youtube播放了视频。 这是我将其添加到AndroidManifest.xml文件中的方式。

AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="YOUR.PACKAGE.NAME">

    <application
        android:label="@string/app_name"
        android:hardwareAccelerated="true">
        //Note the enclosing ">"
        ...

    </application>

</manifest>