How to use MediaPlayer in ANDROID? A simple example.

By | February 25, 2011

MediaPlayer class can be used to play Audio in ANDROID.
See how to do this.

MediaPlayer mp = new MediaPlayer();
mp = MediaPlayer.create(MyClassName.this,songId);
		try {
			mp.prepare();

		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (Exception e) {
			Toast.makeText(Switch.this,e.getMessage() , Toast.LENGTH_SHORT).show();			}
		mp.start();

        //Called when the song completes.....
            mp.setOnCompletionListener(new OnCompletionListener() {
  			public void onCompletion(MediaPlayer mp) {
  			}
  		});

Leave a Reply

Your email address will not be published. Required fields are marked *