【问题标题】:How to play YouTube video inside Android WebView?如何在 Android WebView 中播放 YouTube 视频?
【发布时间】:2013-07-20 22:22:42
【问题描述】:

我正在制作 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"。

【问题讨论】:

标签: android youtube android-webview android-youtube-api


【解决方案1】:

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

但删除这一行:

ws.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

并确保启用了 javascript

ws.setJavaScriptEnabled(true);

这两行在我刚开始的时候给我带来了很大的麻烦。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    【解决方案3】:

    使用此代码。它使用 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>
    

    【讨论】:

    • 用更多细节编辑了我的问题。
    猜你喜欢
    • 1970-01-01
    • 2012-12-14
    • 2013-12-18
    • 1970-01-01
    • 2011-10-22
    • 2017-12-23
    • 2012-09-24
    • 2013-07-13
    • 2014-02-10
    相关资源
    最近更新 更多