android media player - play file http rtsp streams

MediaPlayer class (android.media.MediaPlayer ) can be used to control playback of audio/video files and streams.  

Code:

String url = "http://........"; // your media URL here
//String url = "rtsp://........"; 
//String url = "file:///sdcard/intro.3gp"; //local file

MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url); //Sets the data source (file-path or http/rtsp URL) to use

To play audio file, you can simply call mediaPlayer.start();

mediaPlayer.prepare()
mediaPlayer.start();

For the Audio Stream (http, rtsp)

After setting the datasource and the display surface, you need to either call prepare() or prepareAsync().
For streams, you should call prepareAsync(), which returns immediately, rather than blocking until enough data has been buffered.

A MediaPlayer object must first enter the Prepared state before playback can be started. MediaPlayer.OnPreparedListener defines Interfaces  for a callback to be invoked when the media source is ready for playback.


mediaPlayer.prepareAsync();
//You can show progress dialog here untill it prepared to play

mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            //Called when the media file is ready for playback.
            mp.start();
        }
    });
    mediaPlayer.setOnErrorListener(new OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            return false;
        }
    });


No comments :

Post a Comment

Your Comment and Question will help to make this blog better...