Saturday, February 25, 2012

MediaPlayer: Playing Video and Audio files

With every activity, after onPause and onStop, if the activity is not destroyed, it will continue to run in the background, until it is killed so the system can free up the memory it is using. So until the activity is destroyed, the mediaplayer will continue to occupy space in the phones memory.

When done with the MediaPlayer, you should call release(), to free the resources. If not released, too many MediaPlayer instances will result in an exception." 
 Depending on your needs, you can put the mp.stop() in the onPause part, so if the activity goes to the background for whatever reason, it will only pause the playback, and it can continue it in onResume(). Just put mp.play() in onResume().
If you don't release the media player in the onDestroy part, it will continue to be in the memory, after the activity is stopped. The OS will handle this issue for you if it runs out of memory, but it's nicer, if You do it, so the OS will run out of memory later (much later in case of huge files).
private MediaPlayer mp;
 
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mp=new MediaPlayer();
    startAudioPlayer(context,uri);
//you could set up an onCompletionListener, so when the media playback is done, it will release the media player.
mp.setOnCompletionListener(new OnCompletionListener(){
    @Override
    public void onCompletion(MediaPlayer mp) {
        mp.release();
    }
 });

}
 
public void startAudioPlayer(Context context,Uri uri){
    mp= MediaPlayer.create(context, uri);
    mp.start();
}
 
@Override
protected void onPause() {
    super.onPause();
    if(mp.isPlaying()){ //you need to check if it is playing first, or you might get a null pointer exception!
        mp.stop();
    }
}
@Override
 public void onDestroy(){
 super.onDestroy();
    mp.release();
 }
   
Another way to play an audio file.
 
public void audioPlayer(String path, String fileName){

    //set up MediaPlayer    

    MediaPlayer mp = new MediaPlayer();



    try {

        mp.setDataSource(path+"/"+fileName);

    } catch (IllegalArgumentException 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();

    }


    try {

        mp.prepare();

    } catch (IllegalStateException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    } catch (IOException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    }

    mp.start();

} 

Incase of Video File, use this method to play the video file

public void videoPlayer(String path, String fileName, boolean autoplay){
//get current window information, 
    //and set format, set it up differently, 
    //if you need some special effects
getWindow().setFormat(PixelFormat.TRANSLUCENT);
//the VideoView will hold the video
VideoView videoHolder = new VideoView(this);
//MediaController is the ui control howering above the video (just like in the default youtube player).
videoHolder.setMediaController(new MediaController(this));
//assing a video file to the video holder
videoHolder.setVideoURI(Uri.parse(path+"/"+fileName));
//get focus, before playing the video.
videoHolder.requestFocus();
if(autoplay){
videoHolder.start();
    }
}

1 comment:

  1. Ridiculous story there. What occurred after? Take care!


    Feel free to visit my web blog; target coupons printable coupons

    ReplyDelete

Android Developers Blog

Ram's shared items