Example usage for android.media MediaPlayer setNextMediaPlayer

List of usage examples for android.media MediaPlayer setNextMediaPlayer

Introduction

In this page you can find the example usage for android.media MediaPlayer setNextMediaPlayer.

Prototype

public native void setNextMediaPlayer(MediaPlayer next);

Source Link

Document

Set the MediaPlayer to start when this MediaPlayer finishes playback (i.e.

Usage

From source file:github.daneren2005.dsub.service.DownloadServiceImpl.java

private synchronized void setupNext(final DownloadFile downloadFile) {
    try {// w  ww.  ja  va  2 s  .  c om
        final File file = downloadFile.isCompleteFileAvailable() ? downloadFile.getCompleteFile()
                : downloadFile.getPartialFile();
        if (nextMediaPlayer != null) {
            nextMediaPlayer.setOnCompletionListener(null);
            nextMediaPlayer.release();
            nextMediaPlayer = null;
        }
        nextMediaPlayer = new MediaPlayer();
        nextMediaPlayer.setWakeMode(DownloadServiceImpl.this, PowerManager.PARTIAL_WAKE_LOCK);
        try {
            nextMediaPlayer.setAudioSessionId(mediaPlayer.getAudioSessionId());
        } catch (Throwable e) {
            nextMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        }
        nextMediaPlayer.setDataSource(file.getPath());
        setNextPlayerState(PREPARING);

        nextMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            public void onPrepared(MediaPlayer mp) {
                try {
                    setNextPlayerState(PREPARED);

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
                            && (playerState == PlayerState.STARTED || playerState == PlayerState.PAUSED)) {
                        mediaPlayer.setNextMediaPlayer(nextMediaPlayer);
                        nextSetup = true;
                    }
                } catch (Exception x) {
                    handleErrorNext(x);
                }
            }
        });

        nextMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            public boolean onError(MediaPlayer mediaPlayer, int what, int extra) {
                Log.w(TAG, "Error on playing next " + "(" + what + ", " + extra + "): " + downloadFile);
                return true;
            }
        });

        nextMediaPlayer.prepareAsync();
    } catch (Exception x) {
        handleErrorNext(x);
    }
}

From source file:org.moire.ultrasonic.service.DownloadServiceImpl.java

private synchronized void setupNext(final DownloadFile downloadFile) {
    try {/*from   w w  w.  j  a v  a  2s. c o  m*/
        final File file = downloadFile.isCompleteFileAvailable() ? downloadFile.getCompleteFile()
                : downloadFile.getPartialFile();

        if (nextMediaPlayer != null) {
            nextMediaPlayer.setOnCompletionListener(null);
            nextMediaPlayer.release();
            nextMediaPlayer = null;
        }

        nextMediaPlayer = new MediaPlayer();
        nextMediaPlayer.setWakeMode(DownloadServiceImpl.this, PowerManager.PARTIAL_WAKE_LOCK);

        try {
            nextMediaPlayer.setAudioSessionId(mediaPlayer.getAudioSessionId());
        } catch (Throwable e) {
            nextMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        }

        nextMediaPlayer.setDataSource(file.getPath());
        setNextPlayerState(PREPARING);

        nextMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            @SuppressLint("NewApi")
            public void onPrepared(MediaPlayer mp) {
                try {
                    setNextPlayerState(PREPARED);

                    if (Util.getGaplessPlaybackPreference(DownloadServiceImpl.this)
                            && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
                            && (playerState == PlayerState.STARTED || playerState == PlayerState.PAUSED)) {
                        mediaPlayer.setNextMediaPlayer(nextMediaPlayer);
                        nextSetup = true;
                    }
                } catch (Exception x) {
                    handleErrorNext(x);
                }
            }
        });

        nextMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mediaPlayer, int what, int extra) {
                Log.w(TAG, String.format("Error on playing next (%d, %d): %s", what, extra, downloadFile));
                return true;
            }
        });

        nextMediaPlayer.prepareAsync();
    } catch (Exception x) {
        handleErrorNext(x);
    }
}

From source file:github.daneren2005.dsub.service.DownloadService.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private synchronized void setupNext(final DownloadFile downloadFile) {
    try {/*w w  w  . j ava 2 s  .  c  o m*/
        final File file = downloadFile.isCompleteFileAvailable() ? downloadFile.getCompleteFile()
                : downloadFile.getPartialFile();
        resetNext();

        // Exit when using remote controllers
        if (remoteState != LOCAL) {
            return;
        }

        nextMediaPlayer = new MediaPlayer();
        nextMediaPlayer.setWakeMode(DownloadService.this, PowerManager.PARTIAL_WAKE_LOCK);
        try {
            nextMediaPlayer.setAudioSessionId(audioSessionId);
        } catch (Throwable e) {
            nextMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        }
        nextMediaPlayer.setDataSource(file.getPath());
        setNextPlayerState(PREPARING);

        nextMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            public void onPrepared(MediaPlayer mp) {
                // Changed to different while preparing so ignore
                if (nextMediaPlayer != mp) {
                    return;
                }

                try {
                    setNextPlayerState(PREPARED);

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
                            && (playerState == PlayerState.STARTED || playerState == PlayerState.PAUSED)) {
                        mediaPlayer.setNextMediaPlayer(nextMediaPlayer);
                        nextSetup = true;
                    }

                    applyReplayGain(nextMediaPlayer, downloadFile);
                } catch (Exception x) {
                    handleErrorNext(x);
                }
            }
        });

        nextMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            public boolean onError(MediaPlayer mediaPlayer, int what, int extra) {
                Log.w(TAG, "Error on playing next " + "(" + what + ", " + extra + "): " + downloadFile);
                return true;
            }
        });

        nextMediaPlayer.prepareAsync();
    } catch (Exception x) {
        handleErrorNext(x);
    }
}