Webview中的Android youtube网站正在干扰

问题描述 投票:0回答:2

我尝试在Android应用程序中使用WebView加载youtube网站。需要将整个youtube站点加载到WebView(不仅仅是一个特定的视频URL)。这意味着用户在youtube网站上选择视频然后播放。但视频的播放是干扰。在应用程序中使用youtube是不对的?

我正在使用此代码:webView.loadUrl(url);

android video youtube android-webview playing
2个回答
0
投票

请按照此代码,它将适合您

main activity.Java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {

private WebView mWebview ;
private String youtubeUrl = "https://www.youtube.com/";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mWebview = (WebView) findViewById(R.id.youtubeWebView);
    mWebview.loadUrl(youtubeUrl);
    mWebview.getSettings().setJavaScriptEnabled(true);

}
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<WebView
    android:id="@+id/youtubeWebView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

</android.support.constraint.ConstraintLayout>

并在您的清单文件中添加Internet权限

AndroidManifest.xml中

 <uses-permission android:name="android.permission.INTERNET" />

它将在下面的图片中看起来像这样。

enter image description here


0
投票

您可以将任何网址加载到网页视图中,并在您应用的清单文件中添加互联网权限,并使用如下代码 -

yourWebView.loadUrl(youtubeUrl);

这对我有用,我相信它会合作

© www.soinside.com 2019 - 2024. All rights reserved.