How to play video Using Url On Android?

I am trying to play videos that are streaming from the Internet using Android MediaPlayer. When I use normal video URLs, it works fine but when I use YouTube URLs, it doesn't work. What is a solution to this?

  • This is my code  and when i tried to used rtsp it throws video server died error and error(100,0) and error (1, -2147483648).                                          MediaPlayer mediaPlayer; SurfaceHolder surfaceHolder; SurfaceView playerSurfaceView; //String videoSrc = "http://rtsp//r3---sn-a5m7zu7s.c.youtube.com/CiILENy73wIaGQnZtkPljfg7OhMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"; String videoSrc ="http://oneshot.qualcomm.com/webAR/content/strawberryfields_H264_AAC.mp4"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); playerSurfaceView = (SurfaceView) findViewById(http://r.id/.playersurface); surfaceHolder = playerSurfaceView.getHolder(); surfaceHolder.addCallback(this); } @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } @Override public void surfaceCreated(SurfaceHolder arg0) { try { mediaPlayer = new MediaPlayer(); mediaPlayer.setDisplay(surfaceHolder); mediaPlayer.setDataSource(videoSrc); mediaPlayer.prepare(); mediaPlayer.setOnPreparedListener(this); mediaPlayer.setOnBufferingUpdateListener(this); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setOnErrorListener(this); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void surfaceDestroyed(SurfaceHolder arg0) { // TODO Auto-generated method stub } @Override public void onPrepared(MediaPlayer mp) { mediaPlayer.start(); } @Override public void onBufferingUpdate(MediaPlayer mp, int percent) { mp = this.mediaPlayer; percent = 90; } @Override public boolean onError(MediaPlayer arg0, int arg1, int arg2) { // TODO Auto-generated method stub return true; } }

  • Answer:

    My guess would be that YouTube is iffy about letting anybody stream any video without permission. See, the videos on YouTube are not public domain, there are restrictions - by country, by age, by platform (some videos for example, will not play on mobiles). And to enforce these rules, YouTube may not allow any random user to stream videos by URL. I'd advise you to use the YouTube Android API instead, that should work better. https://developers.google.com/youtube/android/player/

Kaushal Hooda at Quora Visit the source

Was this solution helpful to you?

Other answers

@https://developers.google.com/youtube/android/player/ can be used to play YouTube url's . Though a point to be noted here is that YouTube won't allow videos to be played inWebView's anymore i.e. "Embedding of YouTube video in WebView is Prohibited" But you can still do it using i-frame.

Sudhanshu Bhatt

Thanks for A2A . I see that everyone has given you proper answers. It is better if you take this to some technical forum rather than discussing over Quora. You can always use YouTube API in your code as mentioned by other users.

Ankul Tanna

Hi, Others have already answered this. Only thing I csn add here is try a different youtube url to test

Namit Kothari

Your class would work properly for all URLs other than YouTube. This is due to some YouTube policy crap. The only way to handle this is to is the YouTube api. Its pretty simple. Here's the api https://developers.google.com/yo... Unzip this and import the jar file. The functions are provided in developers.google. com And thanks for the A2A!!!

Vyas Giridharan

Kindly use stackoverflow for this ,Quora is neither the platform nor the right way. Here's the answer for the same. http://stackoverflow.com/questions/8148535/media-player-using-youtube

Anvith Bhat

Youtube has its own class and package. If you want YouTube's urls to be worked, include the youtube's keywords.

Madhurima Roy

It must be some Youtube policy. Try Stack Overflow. Good Luck

Vibhanshu Chaudhary

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.