4

我正在制作 RSS 阅读器应用程序,我从 URL 获取 RSS 数据,并且该 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 中播放视频?

编辑: 这是结果:

在此处输入图像描述

这是我的代码:

 // 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"。

4

3 回答 3

9

可能没有任何用处,但它可能对未来的人很方便

但删除这一行:

ws.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

并确保启用了 javascript

ws.setJavaScriptEnabled(true);

这两条线在我刚开始的时候给我带来了很多麻烦。

于 2013-12-06T04:28:16.460 回答
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);

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

于 2013-07-22T09:50:46.733 回答
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>
于 2013-07-22T09:37:41.140 回答