Play a youtube url in android

帅比萌擦擦* 提交于 2019-12-02 07:51:13

问题


I have a fragment and a videoview is present inside that fragment.I have a you tube url for video. I tried videoview as well as webview. But it is not working. How i should play that video inside that layout?


回答1:


Just use normal a webview and everything will be fine.

Try this: (Tested and works)

new Thread(new Runnable() {
    @Override
    public void run() {
        final WebView myWebView = (WebView) findViewById(R.id.webView1);
        myWebView.setWebViewClient(new WebViewClient());
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.setWebChromeClient(new WebChromeClient());

        try {
            Log.e("Status", "tried url");
            myWebView.loadUrl("http://youtube.com/xxxxxxxxx");  

            handler.post(new Runnable() {
                @Override
                public void run() {
                    Log.e("Status", "In Run():");
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
            Log.e("Status", "exception");
        }       
    }       
}).start();



回答2:


You should use the YouTube Android Player API

It provides you with an efficient YouTube Player view (or even better - a Fragment) for embedding in your app that you can control programatically.



来源:https://stackoverflow.com/questions/20193615/play-a-youtube-url-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!