Code example for MediaPlayer

Methods: setAudioStreamTypesetOnErrorListenersetOnInfoListenersetVolume

1
        return mediaPlayer.getDuration();
    } 
 
    private void initMediaPlayer() { 
 
        mediaPlayer = new MediaPlayer();
 
        mediaPlayer.setAudioStreamType(STREAM_TYPE);
        mediaPlayer.setVolume(1f, 1f);
 
        mediaPlayer.setOnErrorListener(new OnErrorListener() {
            @Override 
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.e(TAG, "MediaPlayer.onErrorListener (What: " + what
                        + ", Extra: " + extra + ")");
                // mp.reset(); 
                if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
                    mp.release();
                    initMediaPlayer(); 
                } 
                return true; 
            } 
        }); 
 
        mediaPlayer.setOnInfoListener(new OnInfoListener() {
            @Override 
            public boolean onInfo(MediaPlayer mp, int what, int extra) {
                Log.e(TAG, "MediaPlayer.onInfoListener (What: " + what
                        + ", Extra: " + extra + ")");
                return false;