Code example for MediaPlayer: Playing audio and video

Methods: getCurrentPositiongetDurationisPlayingstart

0
        this.seekBar = seekBar;
        this.seekBar.setOnSeekBarChangeListener(mediaSeekListener);
    } 
 
    public void play() { 
        if (!mediaPlayer.isPlaying() || isPaused) {
            mediaPlayer.start();
 
            isPaused = false;
 
            long refreshDelay = onRefresh();
            queueNextRefresh(refreshDelay);
        } 
    } 
 
    public void pause() { 
        if (mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
 
            isPaused = true;
        } 
    } 
 
    public void stop() { 
        if (mediaPlayer.isPlaying() || isPaused) {
            try { 
                mediaPlayer.stop();
                mediaPlayer.prepare();
                mediaPlayer.seekTo(0);
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block 
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block 
                e.printStackTrace();
            } 
 
            isPaused = false;
        } 
    } 
 
    public void close() { 
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
            } 
            mediaPlayer.release();
        } 
        mediaPlayer = null;
 
        refreshHandler.removeMessages(REFRESH);
 
        isPaused = true;
    } 
 
    private OnSeekBarChangeListener mediaSeekListener = new OnSeekBarChangeListener() {
 
        public void onStopTrackingTouch(SeekBar seekBar) { 
            if (isPaused) { 
                play(); 
            } 
        } 
 
        public void onStartTrackingTouch(SeekBar seekBar) { 
            if (mediaPlayer.isPlaying()) { 
                pause(); 
            } 
        } 
 
        public void onProgressChanged(SeekBar seekBar, int progress, 
                boolean fromUser) { 
            if (fromUser) { 
                int progressMs = (progress * mediaPlayer.getDuration()) 
                        / seekBar.getMax(); 
                mediaPlayer.seekTo(progressMs); 
            } 
        } 
    }; 
 
    private OnCompletionListener playbackCompletionListener = new OnCompletionListener() {
        public void onCompletion(MediaPlayer mp) { 
            try { 
                mp.stop(); 
                mp.prepare(); 
            } catch (IllegalStateException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 
        } 
    }; 
 
    private class RefreshHandler extends Handler {
        @Override 
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case REFRESH:
                long delay = onRefresh();
                queueNextRefresh(delay);
                break; 
            default: 
                break; 
            } 
        } 
    } 
 
    private void queueNextRefresh(long delay) {
        if (mediaPlayer.isPlaying()) {
            Message msg = refreshHandler.obtainMessage(REFRESH);
            refreshHandler.removeMessages(REFRESH);
            refreshHandler.sendMessageDelayed(msg, delay);
        } 
    } 
 
    private long onRefresh() { 
        if (mediaPlayer != null && mediaPlayer.getDuration() > 0) {
            int progress = (mediaPlayer.getCurrentPosition() * seekBar.getMax())
                    / mediaPlayer.getDuration();
            seekBar.setProgress((int) progress);
        } 
        return DEFAULT_INCREMENT;
    } 
}