Sunday, May 26, 2013

Simple example to play stream video from internet using MediaPlayer

Please notice that it's a very simple and incompleted example to play stream video of 3gp format from internet using MediaPlayer.

MediaPlayer play stream video from internet


Modify layout to have a SurfaceView for the MediaPlayer.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <SurfaceView 
        android:id="@+id/playersurface"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>


MainActivity.java
package com.example.androidmediaplayer;

import java.io.IOException;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.app.Activity;

public class MainActivity extends Activity implements SurfaceHolder.Callback, OnPreparedListener{
 
 MediaPlayer mediaPlayer;
 SurfaceHolder surfaceHolder;
 SurfaceView playerSurfaceView;
 String videoSrc = "rtsp://v6.cache1.c.youtube.com/CjYLENy73wIaLQkDsLHya4-Z9hMYDSANFEIJbXYtZ29vZ2xlSARSBXdhdGNoYKX4k4uBjbOiUQw=/0/0/0/video.3gp";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  playerSurfaceView = (SurfaceView)findViewById(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.setAudioStreamType(AudioManager.STREAM_MUSIC);
  } 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();
 }

}


Note: permission of "android.permission.INTERNET" is needed in AndroidManifest.xml.

download filesDownload the files.

Next:
- Release MediaPlayer in onDestroy() and onPause()


8 comments:

Anonymous said...

if I want to make some folder in my web storage, so I an choose what video to play, can you give an example code to implement that ?

Anonymous said...

Thanks much!

Unknown said...

Not Playing video, stopped at black screen

Anonymous said...

Doesn't work here either.

Anonymous said...

Here too, its not working..

Unknown said...
This comment has been removed by the author.
Unknown said...

this code only works on android 4.4 and above (which is useless since we can use videoview which is easier to implement) on android 4.2 and below nothing happens on a black screen :/

SKOLI said...

Nothing happens.. Stoppped at the black screen .. @Elio Tohme i,ve tested on above 4.4 device only .. nothing happened