在 Android 网络视图上显示 html5 视频。播放时视频变黑

Showing html5 Video on Android Webview. Video getting black while playing

正在 Android 网络视图上显示视频。播放时视频变黑,有声音但视频屏幕变黑。我正在研究 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" />

主要活动代码:

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);
}
}

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

android:hardwareAccelerated="true"

试试下面的代码,它对我有用..

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);
}
}

我使用 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>