2013-07-22 74 views
4

我正在制作RSS阅读器应用程序,并且我从URL获取RSS数据,并且RSS数据可以包含YouTube视频的链接。如何在Android WebView中播放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); 

我的android:在我的AndroidManifest.xml中的硬件加速= “真”。

+0

可能的重复http://stackoverflow.com/questions/17706580/youtube-video-not-playing-in-webview – EvZ

+0

我尝试添加setWebChromeClient并添加android:hardwareAccelerated =“true”,但它stil没有工作。 – Zookey

+0

检查所有代码示例。你错过了什么。 – EvZ

回答

9

大概没有任何用处的,但它可能为未来的人们

很方便,但删除此行:

ws.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

,并确保启用javascript

ws.setJavaScriptEnabled(true);

当我刚出场时,这两条线给我带来了很多麻烦。

+0

它不工作 – Erum

2

使用此验证码。它使用网络视图播放一个你管视频。它运行良好。

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> 
+0

编辑我的问题与更多的细节。 – Zookey

+0

现在不工作。视频已显示,但您无法播放它... – KiKo

4

在Android清单,设定机器人:硬件加速= “真”:

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

希望这会为你工作.. .. !!!!

+0

我添加了android:hardwareAccelerated =“true”,并且我有PluginState.ON – Zookey

+0

@Zookey请参阅参考资料http://stackoverflow.com/questions/8177206/how-to-play-video-in-android-webview – 2013-07-22 09:54:10

+0

@Zookey你可以使用videoview – 2013-07-22 10:15:52

相关问题