Saturday, March 24, 2012

How to use MediaPlayer in ANDROID? A simple example.


How to use MediaPlayer in ANDROID? A simple example.


MediaPlayer class can be used to play Audio in ANDROID.
See how to do this.
?
Drag and copy the code
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
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) {
            }
        });

No comments: