使用webview崩溃玩Youtube

时间:2015-02-12 16:22:07

标签: android webview youtube

我尝试使用WebView在我的应用中播放YouTube视频。 但是当我点击" Play"应用程序崩溃。

这是我的代码:

public class Youtube extends Activity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.a_video_item);

    WebView videoView = (WebView)findViewById(R.id.videoView);
    TextView tvTitle = (TextView) findViewById(R.id.VideoTitle);

    WebSettings videoViewSettings = videoView.getSettings();
    videoViewSettings.setJavaScriptEnabled(true);
    videoViewSettings.setPluginState(WebSettings.PluginState.ON);
    videoView.setWebChromeClient(new WebChromeClient());

    String youtubeID ="XSzE2LLFluE";

    tvTitle.setText(youtubeID);
    videoView.loadUrl("http://www.youtube.com/embed/" + youtubeID +"?autoplay=1&vq=small");
    }
}

这是我的XML:

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

<WebView
    android:id="@+id/videoView"
    android:layout_width="fill_parent"
    android:layout_height="200dp"
    android:layout_below="@+id/VideoTitle"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"/>

<TextView
    android:id="@+id/VideoTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="@string/imgdesc"
    android:layout_marginRight="10dp"/>

</RelativeLayout>

我在Android Studio模拟器上使用Nexus 5测试项目。

2 个答案:

答案 0 :(得分:0)

您必须添加两行代码:

webView.getSettings().setPluginsEnabled(true); webView.getSettings().setJavaScriptEnabled(true);

这将解决问题。试试并告诉。

答案 1 :(得分:0)

我发现了这个问题。 因为我在没有Youtube应用程序崩溃的情况下在模拟器上进行了测试。 在使用Youtube app的设备上,它的工作正常。

当我点击全屏&#39;现在它崩溃了按钮