Example usage for android.media MediaPlayer reset

List of usage examples for android.media MediaPlayer reset

Introduction

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

Prototype

public void reset() 

Source Link

Document

Resets the MediaPlayer to its uninitialized state.

Usage

From source file:net.nightwhistler.pageturner.fragment.ReadingFragment.java

public void onStreamingCompleted(final String wavFile) {

    LOG.debug("TTS streaming completed for " + wavFile);

    if (!ttsIsRunning()) {
        this.textToSpeech.stop();
        return;/*ww  w . j  a va 2s .  c  om*/
    }

    if (!ttsItemPrep.containsKey(wavFile)) {
        LOG.error(
                "Got onStreamingCompleted for " + wavFile + " but there is no corresponding TTSPlaybackItem!");
        return;
    }

    final TTSPlaybackItem item = ttsItemPrep.remove(wavFile);

    try {

        MediaPlayer mediaPlayer = item.getMediaPlayer();
        mediaPlayer.reset();
        mediaPlayer.setDataSource(wavFile);
        mediaPlayer.prepare();

        this.ttsPlaybackItemQueue.add(item);

    } catch (Exception e) {
        LOG.error("Could not play", e);
        showTTSFailed(e.getLocalizedMessage());
        return;
    }

    this.uiHandler.post(this::closeWaitDialog);

    //If the queue is size 1, it only contains the player we just added,
    //meaning this is a first playback start.
    if (ttsPlaybackItemQueue.size() == 1) {
        startPlayback();
    }
}

From source file:net.nightwhistler.pageturner.activity.ReadingFragment.java

@Override
public void onUtteranceCompleted(final String wavFile) {

    LOG.debug("TTS streaming completed for " + wavFile);

    if (!ttsIsRunning()) {
        this.textToSpeech.stop();
        return;/* w ww.j  a  v  a2s  . c  o  m*/
    }

    if (!ttsItemPrep.containsKey(wavFile)) {
        LOG.error(
                "Got onUtteranceCompleted for " + wavFile + " but there is no corresponding TTSPlaybackItem!");
        return;
    }

    final TTSPlaybackItem item = ttsItemPrep.remove(wavFile);

    try {

        MediaPlayer mediaPlayer = item.getMediaPlayer();
        mediaPlayer.reset();
        mediaPlayer.setDataSource(wavFile);
        mediaPlayer.prepare();

        this.ttsPlaybackItemQueue.add(item);

    } catch (Exception e) {
        LOG.error("Could not play", e);
        showTTSFailed(e.getLocalizedMessage());
        return;
    }

    this.uiHandler.post(new Runnable() {

        @Override
        public void run() {
            closeWaitDialog();
        }
    });

    //If the queue is size 1, it only contains the player we just added,
    //meaning this is a first playback start.
    if (ttsPlaybackItemQueue.size() == 1) {
        startPlayback();
    }
}

From source file:com.aujur.ebookreader.activity.ReadingFragment.java

@Override
public void onUtteranceCompleted(final String wavFile) {

    LOG.debug("TTS streaming completed for " + wavFile);

    if (!ttsIsRunning()) {
        this.textToSpeech.stop();
        return;/*from  ww w.j a va2  s  .  co m*/
    }

    if (!ttsItemPrep.containsKey(wavFile)) {
        LOG.error(
                "Got onUtteranceCompleted for " + wavFile + " but there is no corresponding TTSPlaybackItem!");
        return;
    }

    final TTSPlaybackItem item = ttsItemPrep.remove(wavFile);

    try {

        MediaPlayer mediaPlayer = item.getMediaPlayer();
        mediaPlayer.reset();
        mediaPlayer.setDataSource(wavFile);
        mediaPlayer.prepare();

        this.ttsPlaybackItemQueue.add(item);

    } catch (Exception e) {
        LOG.error("Could not play", e);
        showTTSFailed(e.getLocalizedMessage());
        return;
    }

    this.uiHandler.post(new Runnable() {

        @Override
        public void run() {
            closeWaitDialog();
        }
    });

    // If the queue is size 1, it only contains the player we just added,
    // meaning this is a first playback start.
    if (ttsPlaybackItemQueue.size() == 1) {
        startPlayback();
    }
}

From source file:com.sentaroh.android.SMBSync.SMBSyncMain.java

private void playBackDefaultNotification() {
    //      Thread th=new Thread(){
    //         @Override
    //         public void run() {
    //            Uri uri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    //            if (uri!=null) {
    ////               Ringtone rt=RingtoneManager.getRingtone(mContext, uri);
    ////               rt.play();
    ////               SystemClock.sleep(1000);
    ////               rt.stop();
    //               MediaPlayer player = MediaPlayer.create(mContext, uri);
    //               if (player!=null) {
    //                  int dur=player.getDuration();
    //                  player.start();
    //                  SystemClock.sleep(dur+10);
    //                  player.stop();
    //                  player.reset();
    //                  player.release();
    //               }
    //            }
    //         }//from  w  w  w .  j  a  v  a2 s.  co  m
    //      };
    //      th.start();
    Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    if (uri != null) {
        MediaPlayer player = MediaPlayer.create(mContext, uri);
        if (player != null) {
            int dur = player.getDuration();
            player.start();
            SystemClock.sleep(dur + 10);
            player.stop();
            player.reset();
            player.release();
        }
    }
}

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

private synchronized void doPlay(final DownloadFile downloadFile, final int position, final boolean start) {
    try {/*from   w w  w .  j av a2  s.  c  om*/
        downloadFile.setPlaying(true);
        final File file = downloadFile.isCompleteFileAvailable() ? downloadFile.getCompleteFile()
                : downloadFile.getPartialFile();
        boolean isPartial = file.equals(downloadFile.getPartialFile());
        downloadFile.updateModificationDate();

        subtractPosition = 0;
        mediaPlayer.setOnCompletionListener(null);
        mediaPlayer.setOnPreparedListener(null);
        mediaPlayer.setOnErrorListener(null);
        mediaPlayer.reset();
        setPlayerState(IDLE);
        try {
            mediaPlayer.setAudioSessionId(audioSessionId);
        } catch (Throwable e) {
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        }
        String dataSource = file.getAbsolutePath();
        if (isPartial && !Util.isOffline(this)) {
            if (proxy == null) {
                proxy = new BufferProxy(this);
                proxy.start();
            }
            proxy.setBufferFile(downloadFile);
            dataSource = proxy.getPrivateAddress(dataSource);
            Log.i(TAG, "Data Source: " + dataSource);
        } else if (proxy != null) {
            proxy.stop();
            proxy = null;
        }
        mediaPlayer.setDataSource(dataSource);
        setPlayerState(PREPARING);

        mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                Log.i(TAG, "Buffered " + percent + "%");
                if (percent == 100) {
                    mediaPlayer.setOnBufferingUpdateListener(null);
                }
            }
        });

        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            public void onPrepared(MediaPlayer mediaPlayer) {
                try {
                    setPlayerState(PREPARED);

                    synchronized (DownloadService.this) {
                        if (position != 0) {
                            Log.i(TAG, "Restarting player from position " + position);
                            mediaPlayer.seekTo(position);
                        }
                        cachedPosition = position;

                        applyReplayGain(mediaPlayer, downloadFile);

                        if (start || autoPlayStart) {
                            mediaPlayer.start();
                            setPlayerState(STARTED);

                            // Disable autoPlayStart after done
                            autoPlayStart = false;
                        } else {
                            setPlayerState(PAUSED);
                            onSongProgress();
                        }
                    }

                    // Only call when starting, setPlayerState(PAUSED) already calls this
                    if (start) {
                        lifecycleSupport.serializeDownloadQueue();
                    }
                } catch (Exception x) {
                    handleError(x);
                }
            }
        });

        setupHandlers(downloadFile, isPartial, start);

        mediaPlayer.prepareAsync();
    } catch (Exception x) {
        handleError(x);
    }
}

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

private synchronized void doPlay(final DownloadFile downloadFile, final int position, final boolean start) {
    try {/*from   w  w  w  .j a  v  a  2s . c  om*/
        subtractPosition = 0;
        mediaPlayer.setOnCompletionListener(null);
        mediaPlayer.setOnPreparedListener(null);
        mediaPlayer.setOnErrorListener(null);
        mediaPlayer.reset();
        setPlayerState(IDLE);
        try {
            mediaPlayer.setAudioSessionId(audioSessionId);
        } catch (Throwable e) {
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        }

        String dataSource;
        boolean isPartial = false;
        if (downloadFile.isStream()) {
            dataSource = downloadFile.getStream();
            Log.i(TAG, "Data Source: " + dataSource);
        } else {
            downloadFile.setPlaying(true);
            final File file = downloadFile.isCompleteFileAvailable() ? downloadFile.getCompleteFile()
                    : downloadFile.getPartialFile();
            isPartial = file.equals(downloadFile.getPartialFile());
            downloadFile.updateModificationDate();

            dataSource = file.getAbsolutePath();
            if (isPartial && !Util.isOffline(this)) {
                if (proxy == null) {
                    proxy = new BufferProxy(this);
                    proxy.start();
                }
                proxy.setBufferFile(downloadFile);
                dataSource = proxy.getPrivateAddress(dataSource);
                Log.i(TAG, "Data Source: " + dataSource);
            } else if (proxy != null) {
                proxy.stop();
                proxy = null;
            }
        }

        mediaPlayer.setDataSource(dataSource);
        setPlayerState(PREPARING);

        mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                Log.i(TAG, "Buffered " + percent + "%");
                if (percent == 100) {
                    mediaPlayer.setOnBufferingUpdateListener(null);
                }
            }
        });

        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            public void onPrepared(MediaPlayer mediaPlayer) {
                try {
                    setPlayerState(PREPARED);

                    synchronized (DownloadService.this) {
                        if (position != 0) {
                            Log.i(TAG, "Restarting player from position " + position);
                            mediaPlayer.seekTo(position);
                        }
                        cachedPosition = position;

                        applyReplayGain(mediaPlayer, downloadFile);

                        if (start || autoPlayStart) {
                            mediaPlayer.start();
                            applyPlaybackParamsMain();
                            setPlayerState(STARTED);

                            // Disable autoPlayStart after done
                            autoPlayStart = false;
                        } else {
                            setPlayerState(PAUSED);
                            onSongProgress();
                        }

                        updateRemotePlaylist();
                    }

                    // Only call when starting, setPlayerState(PAUSED) already calls this
                    if (start) {
                        lifecycleSupport.serializeDownloadQueue();
                    }
                } catch (Exception x) {
                    handleError(x);
                }
            }
        });

        setupHandlers(downloadFile, isPartial, start);

        mediaPlayer.prepareAsync();
    } catch (Exception x) {
        handleError(x);
    }
}