如何在Android WebView中播放YouTube视频?

4

我是一个 RSS 阅读器应用程序的开发者,我从 URL 中获取 RSS 数据,这些数据可能包含指向 YouTube 视频的链接。

以下是指向 YouTube 视频的链接示例:

div class="video-shortcode"><iframe title="YouTube video player" width="600" 
height="350" src="http://www.youtube.com/embed/HXrcUyCVA6c"
frameborder="0" allowfullscreen></iframe></div>

当我运行我的应用程序时,没有视频,全部是黑色的,我无法播放它。

我该如何在WebView内播放视频?

编辑: 这是结果:

enter image description here

这是我的代码:

 // set webview properties
    WebSettings ws = desc.getSettings();
    ws.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
    ws.getPluginState();
    ws.setPluginState(PluginState.ON);
    ws.setJavaScriptEnabled(true);
    ws.setUserAgent(0);
    ws.setJavaScriptCanOpenWindowsAutomatically(true);

    desc.setWebChromeClient(new WebChromeClient() {
    });
    desc.loadDataWithBaseURL("http://www.balkanandroid.com/", feed
    .getItem(pos).getContent(), "text/html", "UTF-8", null);

我在AndroidManifest.xml中有android:hardwareAccelerated="true"。

可能是重复的问题 https://dev59.com/43TYa4cB1Zd3GeqPwZMg - EvZ
我尝试添加setWebChromeClient和添加android:hardwareAccelerated="true",但仍然无法正常工作。 - Zookey
1
我也遇到了这个问题,我从这里解决了我的问题:https://dev59.com/dmcs5IYBdhLWcg3wmlSk - user2545733
我无法看到带有播放按钮的视频的第一帧画面,只是黑屏! - Zookey
编辑了我的问题并添加了更多细节。 - Zookey
显示剩余4条评论
3个回答

9

也许对现在没有用处,但对未来的人可能会有帮助

但是请删除这一行:

ws.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

并确保启用了javascript

ws.setJavaScriptEnabled(true);

当我刚开始学习时,这两行代码给我带来了很多麻烦。


5
在Android清单文件中,设置android:hardwareAccelerated="true":
<application android:icon="@drawable/icon" android:label="@string/app_name" android:hardwareAccelerated="true">
<activity android:name=".Activity"
    android:label="@string/app_name" android:configChanges="orientation|keyboardHidden">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

在你的主活动中进行设置:

WebSettings w = webView.getSettings();      
w.setPluginState(PluginState.ON);

希望这对你有用....!!!!

我添加了android:hardwareAccelerated="true",并且我的PluginState为ON。 - Zookey
@Zookey 可以参考一下这个链接:http://stackoverflow.com/questions/8177206/how-to-play-video-in-android-webview - user2545733
@Zookey 你可以使用VideoView。 - user2545733

2

请使用此代码。它使用web-view播放一个YouTube视频。它的表现良好。

   package com.example.webbiewdemoapplication;

   import android.app.Activity;
   import android.content.Intent;
   import android.os.Bundle;
   import android.view.View;
   import android.webkit.WebView;
   import android.widget.Button;

 public class WebviewMainActivityFirst extends Activity {

 WebView myWebview;
 Button bw1;
 @Override
 public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webview_main);

    myWebview = (WebView) findViewById(R.id.webview1);

    bw1 = (Button) findViewById(R.id.click_button1);
    bw1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

    Intent intent1 = new  Intent().setClass(
                    WebviewMainActivityFirst.this, WebViewActivitySecond.class);
            startActivity(intent1);
        }
    });




  }
        }

 // second activity

 package com.example.webbiewdemoapplication;

 import android.annotation.SuppressLint;
 import android.app.Activity;
 import android.os.Bundle;
 import android.webkit.WebView;


 public class WebViewActivitySecond extends Activity {

private WebView webView;
@SuppressLint("SetJavaScriptEnabled")
public void onCreate(Bundle saveState) {

    super.onCreate(saveState);
    setContentView(R.layout.activity_main_second);

    webView = (WebView) findViewById(R.id.webview1);

    webView.getSettings().setJavaScriptEnabled(true);

    webView.loadUrl("https://www.youtube.com/watch?v=68AqHwgk2s8");

//  String customHtml = "<html><body><h1>Hello, WebView</h1></body></html>";
//  webView.loadData(customHtml, "text/html", "UTF-8");



}

 }

// xml part

 activity_webview_main.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" >

<Button
    android:id="@+id/click_button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="106dp"
    android:text="Go To WebView" />

</RelativeLayout>

activity_main_second.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >


 <WebView
    android:id="@+id/webview1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />


 </LinearLayout>

编辑了我的问题并添加了更多细节。 - Zookey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接