Playing youtube video in a videoview

2019-07-20 18:43发布

问题:

In my application I have a videoview and I want to play youtube videos in that.For that I tried lots of methods like

VideoView mVideoView = new VideoView(this);
setContentView(mVideoView);
mVideoView.setVideoURI(Uri.parse("rtsp://v3.cache7.c.youtube.com/CiILENy73wIaGQlOCTh0GvUeYRMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"));
mVideoView.start();

For this I am getting

08-13 12:33:07.443: D/MediaPlayer(618): Couldn't open file on client side, trying server side
08-13 12:33:09.003: E/MediaPlayer(618): error (1, -2147483648)
08-13 12:33:09.003: E/MediaPlayer(618): Error (1,-2147483648)

Then I tried

VideoView mVideoView = new VideoView(this);
setContentView(mVideoView);
mVideoView.setVideoURI(Uri.parse("vnd.youtube:GDD0Brw-udk"));
mVideoView.start();

Here also I am getting the same error.

And

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=GDD0Brw-udk&feature=youtube_gdata_player")));

This leaves my application, and starts the mobile youtube where I have no control.

Another method I used is

Intent lVideoIntent = new Intent(null, Uri.parse("http://www.youtube.com/get_video_info?&video_id=GDD0Brw-udk")
            , this, IntroVideoActivity.class);
startActivity(lVideoIntent); 

This is also not working for me..

What should I do..Anyone help me please..Thanx in advance

回答1:

If you want you can put it into a webview . This is how i got it to work :

                    webview = (WebView) v.findViewById(R.id.videoview);  
                    String videoPoP = "http://www.youtube.com/v/A6kCkkLo6Rw?";
        webview.setHorizontalScrollBarEnabled(false);
        webview.setVerticalScrollBarEnabled(false);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setPluginState(PluginState.ON);


            String widthAndHeight = "width=\"" + widthdp + "\"  height=\"" + heightdp + "\"";           

         temp = "<object "
                + widthAndHeight
                + ">"
                + "<body style='margin:0;padding:0;'>"
                + "<param name='allowFullScreen' value='false'>"
                + "</param><param name='allowscriptaccess' value='always'>"
                + "</param><embed src='"
                + videoPoP
                + "'"
                + " type='application/x-shockwave-flash'  allowscriptaccess='always' allowfullscreen='true'"
                + widthAndHeight + "></embed></object>";

        webview.loadData(temp, "text/html", "utf-8");

Pass the parameters to width and height ( static or dynamic upto you ) inside the widthdp and heightdp variable and put it in a WebViewClient and it should work fine



回答2:

your code:

VideoView mVideoView = new VideoView(this);
setContentView(mVideoView);
mVideoView.setVideoURI(Uri.parse("rtsp://v3.cache7.c.youtube.com/CiILENy73wIaGQlOCTh0GvUeYRMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"));
mVideoView.start();

is fine and will work as for me just check your url.