我是Android的菜鸟,尽管我读过很多教程,但我仍然无法弄清楚如何在我的网页浏览中播放YouTube视频。我可以毫无问题地浏览网站,但是当我点击视频时没有任何反应。我尝试将android:hardwareAccelerated =“true”添加到我的清单中但没有成功。任何帮助是极大的赞赏。以下是我到目前为止的情况:
huffingtonpost = (WebView)findViewById(R.id.webView1);
huffingtonpost.setWebViewClient(new WebViewClient());
huffingtonpost.getSettings().setJavaScriptEnabled(true);
huffingtonpost.getSettings().setPluginState(PluginState.ON);
huffingtonpost.getSettings().setUseWideViewPort(true);
huffingtonpost.getSettings().setLoadWithOverviewMode(true);
try{
huffingtonpost.loadUrl("http://www.youtube.com/KitcoNews");
}catch (Exception e){
e.printStackTrace();
}
我的清单
<application
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
答案 0 :(得分:0)
以下是代码示例:
myWebView = (WebView) findViewById( R.id.webview_compontent );
String playVideo= "<html><body>Youtube video .. <br> <iframe class=\"youtube-player\" type=\"text/html\" width=\"640\" height=\"385\" src=\"http://www.youtube.com/embed/bIPcobKMB94\" frameborder=\"0\"></body></html>"
myWebView.loadData(playVideo, "text/html", "utf-8");
如果从EditText或其他内容获取URL,您只需操作字符串playVideo
希望这会有所帮助。
答案 1 :(得分:0)
如何在Android网页视图中播放Youtube频道?
公共类MainActivity扩展了Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
String html="http://www.youtube.com/KitcoNews";
// Let's display the progress in the activity title bar, like the
// browser app does.
getWindow().requestFeature(Window.FEATURE_PROGRESS);
WebView webview = new WebView(this);
setContentView(webview);
webview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(""Done);
}
});
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
//Users will be notified in case there's an error (i.e. no internet connection)
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
//This will load the webpage that we want to see
webview.loadUrl(html);
}
}