0

I'm trying to play YouTube video in Video-view Android. It is giving can't play this video. What I have tried is like below..

MainActivity.java

    public class MainActivity extends AppCompatActivity {

    // Widgets
    private VideoView vv_youtube;

    // Variables
    private String base_url = "https://www.youtube.com/watch?v=K8lppDEOkcM";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        vv_youtube = (VideoView) findViewById(R.id.vv_youtube);

        Uri video = Uri.parse(base_url);
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(vv_youtube);
        vv_youtube.requestFocus();
        vv_youtube.setMediaController(mediaController);
        vv_youtube.setVideoURI(video);

        vv_youtube.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer arg0) {
                vv_youtube.start();
            }
        });

        vv_youtube.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.e("Error is: ", String.valueOf(extra));
                return false;
            }
        });

        vv_youtube.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                Log.e("Complete is: ", mp.toString());
            }
        });
    }
}

LogCat

11-19 02:21:45.741 21929-21961/com.example.inbridge08.videoviewyoutube I/OpenGLRenderer: Initialized EGL, version 1.4 11-19 02:21:45.772 21929-21961/com.example.inbridge08.videoviewyoutube D/OpenGLRenderer: Enabling debug mode 0 11-19 02:21:45.799 21929-21961/com.example.inbridge08.videoviewyoutube W/EGL_emulation: eglSurfaceAttrib not implemented 11-19 02:21:45.799 21929-21961/com.example.inbridge08.videoviewyoutube W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe2b19220, error=EGL_SUCCESS 11-19 02:21:45.898 21929-21929/com.example.inbridge08.videoviewyoutube D/MediaPlayer: Couldn't open file on client side, trying server side 11-19 02:21:46.421 21929-21947/com.example.inbridge08.videoviewyoutube E/MediaPlayer: error (1, -2147483648) 11-19 02:21:46.421 21929-21929/com.example.inbridge08.videoviewyoutube E/MediaPlayer: Error (1,-2147483648) 11-19 02:21:46.421 21929-21929/com.example.inbridge08.videoviewyoutube D/VideoView: Error: 1,-2147483648 11-19 02:21:46.421 21929-21929/com.example.inbridge08.videoviewyoutube E/Error is:: -2147483648

[ 11-19 02:21:46.513 82: 82 D/] Socket deconnection

I searched on google for two days and I reached on decision that I have to use YouTube Android Player API to play YouTube videos in Video-view. If I replace URL with other(Not YouTube video URL) It is working fine.

I tried on both Emulator and device. Not working on both.

Thanks in Advance!

Maulik Dodia
  • 1,629
  • 2
  • 21
  • 48

1 Answers1

0

Try using the MediaPlayer class for using playing videos in VideoView. Here's a snippet taken from Using MediaPlayer guide to play videos from URIs:

Playing from a remote URL via HTTP streaming looks like this:

String url = "http://........"; // your URL here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();

Note: If you're passing a URL to stream an online media file, the file must be capable of progressive download.

You may also check this SO thread for additional reference.

Community
  • 1
  • 1
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56