Example usage for android.media MediaPlayer pause

List of usage examples for android.media MediaPlayer pause

Introduction

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

Prototype

public void pause() throws IllegalStateException 

Source Link

Document

Pauses playback.

Usage

From source file:org.artoolkit.ar.samples.ARSimple.ARSimple.java

private static void pause(MediaPlayer media, CountDownTimer timer) {
    if (media != null) {
        if (media.isPlaying()) {
            /*/*from  w  w  w. ja  va 2 s. c o m*/
            if (timer != null) {
               timer.cancel();
               timer = null;
            }
            */
            media.pause();
        } else {
            Log.w("MediaPlayer : ", " l'instance n'est pas en mode lecture");
        }
    } else {
        Log.w("MediaPlayer : ", " l'instance n'a pas t initialise");
    }
}

From source file:com.orange.essentials.otb.ui.OtbTermsFragment.java

@Override
public void onPause() {
    super.onPause();
    for (final MediaPlayer player : mPlayers) {
        if (player.isPlaying())
            player.pause();
    }/*from  www .  ja  va 2 s  . c o m*/
}

From source file:org.cyanogenmod.theme.chooser.AudiblePreviewFragment.java

private void stopMediaPlayers() {
    if (mContent == null)
        return;/*from   w w w .ja  v  a  2 s.c  o  m*/
    final int numChildern = mContent.getChildCount();
    for (int i = 0; i < numChildern; i++) {
        ImageView iv = (ImageView) mContent.getChildAt(i).findViewById(R.id.btn_play_pause);
        if (iv != null) {
            iv.setImageResource(android.R.drawable.ic_media_play);
            MediaPlayer mp = (MediaPlayer) iv.getTag();
            if (mp != null && mp.isPlaying()) {
                mp.pause();
                mp.seekTo(0);
            }
        }
    }
}

From source file:org.dharmaseed.android.PlayTalkActivity.java

public void playTalkButtonClicked(View view) {
    Log.d(LOG_TAG, "button pressed");
    MediaPlayer mediaPlayer = talkPlayerFragment.getMediaPlayer();
    if (mediaPlayer.isPlaying()) {
        mediaPlayer.pause();
        setPPButton("ic_media_play");
    } else if (talkPlayerFragment.getMediaPrepared()) {
        mediaPlayer.start();//  www  . j a  v  a  2s. co  m
        setPPButton("ic_media_pause");
    } else {
        try {
            if (talk.isDownloaded()) {
                Log.d(LOG_TAG, "Playing from " + talk.getPath());
                mediaPlayer.setDataSource(talk.getPath());
            } else {
                mediaPlayer.setDataSource(talk.getAudioUrl());
            }
            mediaPlayer.prepareAsync();
        } catch (Exception e) {
            Log.e(LOG_TAG, e.toString());
        }
    }
}

From source file:net.wespot.pim.view.InqImageDetailFragment.java

public void playPauseButton() {
    if (status == PAUSE) {
        playPauseButton/*from  ww w.  ja v a  2  s  .c  om*/
                .setImageDrawable(getActivity().getResources().getDrawable(android.R.drawable.ic_media_pause));

        status = PLAYING;
        mediaPlayer.start();

        finalTime = mediaPlayer.getDuration();
        startTime = mediaPlayer.getCurrentPosition();
        if (oneTimeOnly == 0) {
            seekbar.setMax((int) finalTime);
            oneTimeOnly = 1;
        }
        seekbar.setProgress((int) startTime);
        playHandler.postDelayed(UpdateSongTime, 100);
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                status = PAUSE;
                playPauseButton.setImageDrawable(
                        getActivity().getResources().getDrawable(android.R.drawable.ic_media_play));
            }
        });
    } else {
        playPauseButton
                .setImageDrawable(getActivity().getResources().getDrawable(android.R.drawable.ic_media_play));
        status = PAUSE;
        mediaPlayer.pause();
    }
}

From source file:com.android.onemedia.playback.LocalRenderer.java

/**
 * Pause the player if possible or set it to not play when ready. If the
 * player is in a state where it will never be ready returns false.
 *
 * @return true if the content was paused or will wait to play when ready
 *         later/* w  w w  .  ja v  a 2s  .  com*/
 */
@Override
public boolean onPause() {
    MediaPlayer player = mPlayer;
    // If the user paused us make sure we won't start playing again until
    // asked to
    mPlayOnReady = false;
    if (player != null && (mState & CAN_PAUSE) != 0) {
        player.pause();
        setState(STATE_PAUSED);
    } else if (!isPaused()) {
        return false;
    }
    return true;
}

From source file:com.massivcode.androidmusicplayer.services.MusicService.java

@Override
public void onCompletion(MediaPlayer mp) {

    //   ?? ???  ? 

    mp.pause();
    mp.reset();/*from w w  w  . j av  a2 s  .com*/

    try {
        // ? ? ?? ?? .
        boolean isShuffle = DataBackupUtil.getInstance(getApplicationContext()).loadIsShuffle();
        //  ? ?.
        boolean isRepeat = DataBackupUtil.getInstance(getApplicationContext()).loadIsRepeat();
        Log.d(TAG, " ->  : " + isShuffle + "  : " + isRepeat);

        if (mCurrentPlaylist != null) {
            setNextMusicInfo(mp, isShuffle, isRepeat);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (mCurrentMusicInfo != null) {
            showNotification();
            sendMusicEvent();
        }
    }

}

From source file:com.android.onemedia.playback.LocalRenderer.java

/**
 * Seek to a given position in the media. If the seek succeeded or will be
 * performed when loading is complete returns true. If the position is not
 * in range or the player will never be ready returns false.
 *
 * @param position The position to seek to in milliseconds
 * @return true if playback was moved or will be moved when ready
 *//*from  w w  w  . ja v  a  2  s.  co m*/
@Override
public boolean onSeekTo(int position) {
    MediaPlayer player = mPlayer;
    if (player != null && (mState & CAN_SEEK) != 0) {
        if (position < 0 || position >= getDuration()) {
            return false;
        } else {
            if (mState == STATE_ENDED) {
                player.start();
                player.pause();
                setState(STATE_PAUSED);
            }
            player.seekTo(position);
        }
    } else if ((mState & CAN_READY_SEEK) != 0) {
        mSeekOnReady = position;
    } else {
        return false;
    }
    return true;
}

From source file:com.rks.musicx.services.MusicXService.java

/**
 * FadeOut/*from  w w  w .  ja  va  2  s . co m*/
 *
 * @param _player
 * @param duration
 */
public void fadeOut(final MediaPlayer _player, final int duration) {
    final float deviceVolume = getDeviceVolume();
    final Handler h = new Handler();
    h.postDelayed(new Runnable() {
        private float time = duration;
        private float volume = 0.0f;

        @Override
        public void run() {
            // can call h again after work!
            time -= 100;
            volume = (deviceVolume * time) / duration;
            _player.setVolume(volume, volume);
            if (time > 0)
                h.postDelayed(this, 100);
            else {
                _player.pause();
            }
        }
    }, 100); // 1 second delay (takes millis)
}

From source file:de.azapps.mirakel.helper.TaskDialogHelpers.java

public static void playbackFile(final Activity context, final FileMirakel file, final boolean loud) {
    final MediaPlayer mPlayer = new MediaPlayer();
    final AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    if (!loud) {/*  w ww  . j  av  a 2  s . c  o m*/
        am.setSpeakerphoneOn(false);
        am.setMode(AudioManager.MODE_IN_CALL);
        context.setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
    }
    try {
        mPlayer.reset();
        if (!loud) {
            mPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
        }
        mPlayer.setDataSource(file.getFileStream(context).getFD());
        mPlayer.prepare();
        mPlayer.start();
        mPlayer.setOnCompletionListener(new OnCompletionListener() {
            @Override
            public void onCompletion(final MediaPlayer mp) {
                audio_playback_dialog.dismiss();
            }
        });
        am.setMode(AudioManager.MODE_NORMAL);
        audio_playback_playing = true;
    } catch (final IOException e) {
        Log.e(TAG, "prepare() failed");
    }
    audio_playback_dialog = new AlertDialog.Builder(context).setTitle(R.string.audio_playback_title)
            .setPositiveButton(R.string.audio_playback_pause, null)
            .setNegativeButton(R.string.audio_playback_stop, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(final DialogInterface dialog, final int which) {
                    mPlayer.release();
                }
            }).setOnCancelListener(new OnCancelListener() {
                @Override
                public void onCancel(final DialogInterface dialog) {
                    mPlayer.release();
                    dialog.cancel();
                }
            }).create();
    audio_playback_dialog.setOnShowListener(new OnShowListener() {
        @Override
        public void onShow(final DialogInterface dialog) {
            final Button button = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                    if (audio_playback_playing) {
                        button.setText(R.string.audio_playback_play);
                        mPlayer.pause();
                        audio_playback_playing = false;
                    } else {
                        button.setText(R.string.audio_playback_pause);
                        mPlayer.start();
                        audio_playback_playing = true;
                    }
                }
            });
        }
    });
    audio_playback_dialog.show();
}