Mar 17, 2011

A simple example using VideoView to play 3gp from YouTube

A simple example using VideoView to play 3gp from YouTube

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello"
   />
<LinearLayout
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content">
<VideoView
   android:id="@+id/myvideoview"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>


package com.AnVideoView;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class AnVideoView extends Activity {

String SrcPath = "rtsp://v5.cache1.c.youtube.com/CjYLENy73wIaLQnhycnrJQ8qmRMYESARFEIJbXYtZ29vZ2xlSARSBXdhdGNoYPj_hYjnq6uUTQw=/0/0/0/video.3gp";

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
       myVideoView.setVideoURI(Uri.parse(SrcPath));
       myVideoView.setMediaController(new MediaController(this));
       myVideoView.requestFocus();
       myVideoView.start();
   }
}


Related Post:
- Using VideoView to play mp4 from sdcard
- Dual VideoView to play 3gp from YouTube
- How to get 3gp link of Youtube video

49 comments:

  1. This example cannot be run on emulator! You need a true device to run it.

    ReplyDelete
    Replies
    1. Bcoz Emulator,does not support online streaming facilities.its a pureally virtual machine.so try in and Android mobile or tablets

      Delete
  2. Thanks Eric for the example!

    Do you now how to show the MediaController when starting the Activity so that the user can see how long is the video and where the control buttons are?

    I have tried many things but did not managed to do that so far.

    Thanks,
    BJ

    ReplyDelete
  3. BJ,

    Sorry, I can't catch your question - The example have already show the play time and controls.

    ReplyDelete
  4. Hi i m getting an msg that "sorry this video cannot be played".please help ASAP

    ReplyDelete
    Replies
    1. Hey,but you trying to open that video in Emulator,or android phone?

      Delete
  5. PRABHJOT SINGH,

    Do you run on emulator? or un-supported format?

    ReplyDelete
  6. same problem, I've run it on device

    ReplyDelete
  7. Hello guys! I copy this code into my empty project, but it doesn't work (( It said me that video loagind is impossible. Help me please. I try it on my device.

    ReplyDelete
  8. Must I edit a AndroidManifest file?

    ReplyDelete
    Replies
    1. Yes. go to AndroidManifest. click on Permission Tab.
      Choose Add. Select the Name as android.permission.INTERNET

      it allows your app to connect to the internet.

      Delete
  9. if you load from internet, for sure you have to edit AndroidManifest.

    ReplyDelete
  10. it does not works...it says "sorry this video cannot be Played"

    ReplyDelete
  11. Hello guys, Please let anybody know's about how to play the video using buffer? I have received buffers from network.

    ReplyDelete
  12. please change the path of ur video in plcae of srcpath and add the internet permission line in manifest file thanku

    ReplyDelete
  13. hi i just started using android and i need to drag a video
    means that i can show a video with a big resolution on my android emulator using the keyboards up, left, down and right
    please can anyone help me
    thanks very much

    ReplyDelete
  14. can you please tell me how can i play two video on screen?give me the full code with xml.please :(

    ReplyDelete
  15. Hello ,
    The reason you guys are getting error "sorry this video cannot be Played" is because you guys are playing the video through the WiFi avilable in the device. Just turn off the WiFi and turn on 3g , Video will be played .

    rtsp links are supported in 3G

    ReplyDelete
    Replies
    1. its not required to turn off WiFi... i tested this code on wifi enabled device (tho i use only Wifi Data on my tab)...its wrking fine...its also supported in 2G (tested)...

      Delete
  16. enkhbayar,

    I'm not sure. Can you provide any link for example?

    ReplyDelete
  17. How did you get that youtube link?

    ReplyDelete
    Replies
    1. Search in YouTube mobile site: http://m.youtube.com/

      Delete
  18. pourquoi la lecture vidéo n'est pas d'une bonne qualité et est ce que la capture vidéo fonctionne sur l'émulateur car j'ai testé plusieurs tuto mai il ne fonctionnent pas et la taille du fichier est toujour égale à 0

    ReplyDelete
  19. I get an error saying Video cannot be played ? How do I fix this error ? What changes need to be made to the android manifest ? I already have internet permissions enabled for my application.

    ReplyDelete
    Replies
    1. Some case is it cannot be downloaded, or the format is not supported.

      Delete
  20. If you have any query realted android or video file not supported,or video cant play.so mail me your query asap.i can solve ..

    ReplyDelete
  21. What to do if teh url is not in the 3gp format,for example if the url is like http://www.youtube.com/watch?v=jvcNOz1NHKM

    ReplyDelete
    Replies
    1. Suggest to play 3gp version in this case.

      You can search your 3gp version in http://m.youtube.com/

      The url of your 3gp version is :
      rtsp://v7.cache6.c.youtube.com/CjYLENy73wIaLQmjHE09Ow33jhMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYKjR78WV1ZH5Tgw=/0/0/0/video.3gp

      Delete
    2. How can i get the 3gp version of a link after i search for something? so that the player should be dinamically

      Delete
    3. If you know this, please help, my email: rosu_alin@ymail.com

      Delete
  22. hey,i have done my project successfully show on the emulator but video is not running on that and error like:
    sorry this video can not be played

    ReplyDelete
  23. Hello,

    Is it possible to run the youtube video using Android Native Media Player? Also would like to add that can we buffer/cache the video in background and user can run it afterwards?

    ReplyDelete
  24. Hello please help me. i just want to play youtube video as your code reference . i can play your video 3gp link its good. but however i put my url in string path it always says "sorry this video can't be play". this message for all video url. i have learn how to get video url :"right click on video url and copy it." but then not success . i run this in real device . what is the problem ?

    ReplyDelete
  25. for those who got the message "sorry this video can't be play":
    Edit android manifest.xml:

    ReplyDelete
  26. can you use with youtube api bro :)

    ReplyDelete
  27. how to show loading/buffering of video play from URL before loading...........

    ReplyDelete
  28. Hi I want to create app for Android Smart TV Box(Minix) and i want to play RTMP or HLS RTSP(mp4) streaming video, please help me How can I do that,.

    I tried with ur code but am getting this error "Sorry this video cant be played" how can i resolve it,.please help me,.

    Thank you.

    ReplyDelete
    Replies
    1. yes its a mp4 but rtmp stream,.

      Delete
    2. I think Android build-in player not support rtmp stream!

      Delete
    3. Ya it is, but am using vitamio player to play my stream its perfectly working in simulator but on device (Android Box- minix) its not working also gives this error "This video can't be played" and this is my url (rtmp://fml.8888.planetstream.net/208888/newry).

      Thank you.

      Delete
  29. I to run the app I get an error message "Sorry, you can not play this video"

    ReplyDelete
  30. Bonjour, j’ai besoin de retourner à la page d’accueil de mon application à la fin de la lecture de la vidéo!
    j’éspère que vous pouvez m’aider, merci d’avance

    ReplyDelete
  31. I have added same code in my application, and also added Internet, wifi and network permissions in manifest.xml file. . I have parsed rtsp url. After running App in emulator I checked logcat, it gives "Couldn't open file on client side, trying server side". But I thought this is because I tried running on emulator then tested on android device, but the problem is same, "Sorry, you can not play this video". Can anyone please help me.?

    ReplyDelete

Infolinks In Text Ads