Example usage for android.media MediaPlayer stop

List of usage examples for android.media MediaPlayer stop

Introduction

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

Prototype

public void stop() throws IllegalStateException 

Source Link

Document

Stops playback after playback has been started or paused.

Usage

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

private void setNextMusicInfo(MediaPlayer mp, boolean isShuffle, boolean isRepeat) throws IOException {

    if (isShuffle) {
        mCurrentPosition = shuffle(mCurrentPlaylist.size());
    } else {/*  w w w  . ja  va  2 s.  c o m*/
        if (mCurrentPosition < getCurrentPlaylistSize()) {
            mCurrentPosition++;
        } else {
            mCurrentPosition = 0;
        }
    }

    mp.setDataSource(getApplicationContext(), switchIdToUri(mCurrentPlaylist.get(mCurrentPosition)));
    mCurrentMusicInfo = MusicInfoLoadUtil.getSelectedMusicInfo(getApplicationContext(),
            mCurrentPlaylist.get(mCurrentPosition));

    if (isShuffle) {
        mp.prepare();
        mp.start();
        isReady = true;
    } else {
        if (isRepeat) {
            mp.prepare();
            mp.start();
            isReady = true;
        } else {
            if (mCurrentPosition != 0) {
                mp.prepare();
                mp.start();
                isReady = true;
            } else {
                mp.stop();
                mp.reset();
                isReady = false;
            }
        }
    }
}

From source file:com.sentaroh.android.TaskAutomation.TaskExecutor.java

final static public void playBackRingtone(TaskManagerParms taskMgrParms, EnvironmentParms envParms,
        CommonUtilities util, TaskResponse taskResponse, ActionResponse ar, String dlg_id, String rt, String rn,
        String rp, String vol_left, String vol_right) {
    ar.action_resp = ActionResponse.ACTION_SUCCESS;
    if (!isRingerModeNormal(envParms)) {
        ar.action_resp = ActionResponse.ACTION_WARNING;
        ar.resp_msg_text = taskMgrParms.teMsgs.msgs_thread_task_exec_ignore_sound_ringer_not_normal;
        return;//from w  ww. j a v  a  2 s  . c  om
    }
    MediaPlayer player = MediaPlayer.create(taskMgrParms.context, Uri.parse("content://media" + rp));
    //      taskResponse.active_action_name=action;
    if (player != null) {
        int duration = 0;
        if (!rt.equals(PROFILE_ACTION_RINGTONE_TYPE_NOTIFICATION))
            duration = RINGTONE_PLAYBACK_TIME;
        else
            duration = player.getDuration();

        if (duration >= 5000) {
            TaskManager.showMessageDialog(taskMgrParms, envParms, util, taskResponse.active_group_name,
                    taskResponse.active_task_name, taskResponse.active_action_name,
                    taskResponse.active_dialog_id, MESSAGE_DIALOG_MESSAGE_TYPE_SOUND, rt + " " + rn);

        }
        if (!vol_left.equals("-1") && !vol_left.equals(""))
            player.setVolume(Float.valueOf(vol_left) / 100, Float.valueOf(vol_right) / 100);
        player.start();
        waitTimeTc(taskResponse, duration + 10);
        if (!taskResponse.active_thread_ctrl.isEnable()) {
            ar.action_resp = ActionResponse.ACTION_CANCELLED;
            ar.resp_msg_text = "Action was cancelled";
        }
        player.stop();
        player.release();
        if (duration >= 5000) {
            TaskManager.closeMessageDialog(taskMgrParms, envParms, util, taskResponse);
        }
    } else {
        ar.action_resp = ActionResponse.ACTION_ERROR;
        ar.resp_msg_text = String.format(taskMgrParms.teMsgs.msgs_thread_task_play_sound_error, rp);
    }
}

From source file:org.botlibre.sdk.activity.ChatActivity.java

public MediaPlayer playAudio(String audio, boolean loop, boolean cache, boolean start) {
    try {/*from   w  w  w .  j a  v  a2 s . c  om*/
        Uri audioUri = null;
        if (cache) {
            audioUri = HttpGetVideoAction.fetchVideo(this, audio);
        }
        if (audioUri == null) {
            audioUri = Uri.parse(MainActivity.connection.fetchImage(audio).toURI().toString());
        }
        final MediaPlayer audioPlayer = new MediaPlayer();
        audioPlayer.setDataSource(getApplicationContext(), audioUri);
        audioPlayer.setOnErrorListener(new OnErrorListener() {

            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.wtf("Audio error", "what:" + what + " extra:" + extra);
                audioPlayer.stop();
                audioPlayer.release();
                return true;
            }
        });
        audioPlayer.setOnCompletionListener(new OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                audioPlayer.release();
                runOnUiThread(new Runnable() {
                    public void run() {
                        try {
                            beginListening();
                        } catch (Exception e) {
                            Log.e("ChatActivity", "MediaPlayer: " + e.getMessage());
                        }
                    }
                });
            }
        });
        audioPlayer.prepare();
        audioPlayer.setLooping(loop);
        if (start) {
            audioPlayer.start();
        }
        return audioPlayer;
    } catch (Exception exception) {
        Log.wtf(exception.toString(), exception);
        return null;
    }
}

From source file:com.shinymayhem.radiopresets.ServiceRadioPlayer.java

protected void stopAndReleasePlayer(MediaPlayer player) {
    //TODO need to release looping background metadata task here somehow too
    if (LOCAL_LOGV)
        log("stopAndReleasePlayer()", "v");
    if (player != null) {
        try {/*from  w w w . jav a2s . c o  m*/
            if (player.isPlaying()) {
                player.stop();
                if (LOCAL_LOGV)
                    log("stopped mediaPlayer", "v");
            }
            if (LOCAL_LOGV)
                log("mediaPlayer not playing", "v");
        } catch (IllegalStateException e) {
            if (LOCAL_LOGV)
                log("player in wrong state to stop", "v");
        }
        try {
            player.reset();
            if (LOCAL_LOGV)
                log("reset mediaPlayer", "v");
        } catch (IllegalStateException e) {
            if (LOCAL_LOGV)
                log("player in wrong state to reset", "v");
        }
        player.release();
    }
}

From source file:com.halseyburgund.rwframework.core.RWService.java

/**
 * Creates a media player for sound playback, with initial volume of 0.
 *//*from   w  ww .  java  2s .c  o m*/
private void createPlayer() {
    if (mPlayer == null) {
        mPlayer = new MediaPlayer();
        mPlayer.setWakeMode(this, PowerManager.PARTIAL_WAKE_LOCK);

        float volume = (float) 0.0;
        mPlayer.setVolume(volume, volume);
        mVolumeLevel = 0;
        mPlayer.pause();

        mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                if (D) {
                    Log.d(TAG, "MediaPlayer prepared event");
                }
                broadcast(RW.READY_TO_PLAY);
                if (mStartPlayingWhenReady) {
                    playbackFadeIn(mVolumeLevel);
                }
                mAssetTracker.start();
            }
        });

        mPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mp, int what, int extra) {
                if (D) {
                    Log.i(TAG, "MediaPlayer info event");
                    if (MediaPlayer.MEDIA_INFO_METADATA_UPDATE == what) {
                        Log.i(TAG, "MediaPlayer metadata updated, extra = " + extra);
                    }
                }
                return true;
            }
        });

        mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                if (D) {
                    Log.d(TAG, "MediaPlayer error event");
                }
                mAssetTracker.reset();
                return true;
            }
        });

        mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                if (D) {
                    Log.d(TAG, "MediaPlayer completion event");
                }
                mAssetTracker.stop();
                mp.stop();
                broadcast(RW.PLAYBACK_FINISHED);
            }
        });

        mPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
            @Override
            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                if (D) {
                    Log.d(TAG, "MediaPlayer buffering event, %=" + percent);
                }
            }
        });
    }
}

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. ja  v  a2  s. c o 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();
        }
    }
}