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:Main.java

public static void stopRing(MediaPlayer mediaPlayer) {
    mediaPlayer.stop();
}

From source file:Main.java

public static void stopAudioStream(MediaPlayer mediaPlayer) {
    mediaPlayer.stop();
}

From source file:Main.java

public static void stopPlaying(MediaPlayer mediaPlayer) {
    mediaPlayer.stop();
    mediaPlayer.release();
}

From source file:es.deustotech.piramide.utils.tts.TextToSpeechWeb.java

private static synchronized void speech(final Context context, final String text, final String language) {
    executor.submit(new Runnable() {
        @Override/* w w w  .j av a2s . c om*/
        public void run() {
            try {
                final String encodedUrl = Constants.URL + language + "&q="
                        + URLEncoder.encode(text, Encoding.UTF_8.name());
                final DefaultHttpClient client = new DefaultHttpClient();
                HttpParams params = new BasicHttpParams();
                params.setParameter("http.protocol.content-charset", "UTF-8");
                client.setParams(params);
                final FileOutputStream fos = context.openFileOutput(Constants.MP3_FILE,
                        Context.MODE_WORLD_READABLE);
                try {
                    try {
                        final HttpResponse response = client.execute(new HttpGet(encodedUrl));
                        downloadFile(response, fos);
                    } finally {
                        fos.close();
                    }
                    final String filePath = context.getFilesDir().getAbsolutePath() + "/" + Constants.MP3_FILE;
                    final MediaPlayer player = MediaPlayer.create(context.getApplicationContext(),
                            Uri.fromFile(new File(filePath)));
                    player.start();
                    Thread.sleep(player.getDuration());
                    while (player.isPlaying()) {
                        Thread.sleep(100);
                    }
                    player.stop();

                } finally {
                    context.deleteFile(Constants.MP3_FILE);
                }
            } catch (InterruptedException ie) {
                // ok
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

From source file:com.dudka.rich.streamingmusicplayer.ServiceMusicPlayer.java

@Override
public void onCompletion(MediaPlayer mp) {
    mp.stop();
    mp.release();
    sendLocalBroadcast(MainActivity.PLAYER_COMPLETED);
    stopSelf();
}

From source file:com.dudka.rich.streamingmusicplayer.ServiceMusicPlayer.java

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
    mp.reset();/*w  w w.ja v a 2 s .  co  m*/
    mp.stop();
    mp.release();
    sendLocalBroadcast(MainActivity.PLAYER_ERROR);
    stopSelf();
    return false;
}

From source file:com.android.settings.beanstalk.service.AlarmService.java

public void startAlarmSound() throws java.io.IOException, IllegalArgumentException, IllegalStateException {

    Uri alertSound = QuietHoursController.getInstance(this).returnUserRingtone();

    if (mPlaying) {
        stopAlarm();//from www .j a  va  2s .  c om
    } else {
        mAlarmVolumeSetting = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM);
    }

    if (mMediaPlayer == null) {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnErrorListener(new OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                mp.stop();
                mp.release();
                mMediaPlayer = null;
                return true;
            }
        });
    }

    mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM,
            mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM), 0);

    mMediaPlayer.setDataSource(this, alertSound);
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);

    if (QuietHoursController.getInstance(this).returnUserRingtoneLoop()) {
        mMediaPlayer.setLooping(true);
    } else {
        mMediaPlayer.setLooping(false);
        mMediaPlayer.setOnSeekCompleteListener(stopSelf);
    }
    mMediaPlayer.prepare();
    mMediaPlayer.start();
    mPlaying = true;
}

From source file:com.android.settings.service.AlarmService.java

public void startAlarmSound() throws java.io.IOException, IllegalArgumentException, IllegalStateException {

    Uri alertSound = SmsCallHelper.returnUserRingtone(this);

    if (mPlaying) {
        stopAlarm();/*from ww w .  j  a  v  a  2 s . c om*/
    } else {
        mAlarmVolumeSetting = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM);
    }

    if (mMediaPlayer == null) {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnErrorListener(new OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                mp.stop();
                mp.release();
                mMediaPlayer = null;
                return true;
            }
        });
    }

    mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM,
            mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM), 0);

    mMediaPlayer.setDataSource(this, alertSound);
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);

    if (SmsCallHelper.returnUserRingtoneLoop(this)) {
        mMediaPlayer.setLooping(true);
    } else {
        mMediaPlayer.setLooping(false);
        mMediaPlayer.setOnSeekCompleteListener(stopSelf);
    }
    mMediaPlayer.prepare();
    mMediaPlayer.start();
    mPlaying = true;
}

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

@Override
public void onDestroy() {
    super.onDestroy();
    for (final MediaPlayer player : mPlayers) {
        player.stop();
    }/*w w w . ja va2 s .com*/
}

From source file:com.meiste.tempalarm.ui.Alarm.java

@Override
protected void onResume() {
    super.onResume();

    final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    // do not play alarms if stream volume is 0 (typically because ringer mode is silent).
    if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
        final Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            public boolean onError(final MediaPlayer mp, final int what, final int extra) {
                Timber.e("Error occurred while playing audio.");
                mp.stop();
                mp.reset();/*from   w w  w.  j a  v a  2 s . co  m*/
                mp.release();
                mMediaPlayer = null;
                return true;
            }
        });
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
        try {
            mMediaPlayer.setDataSource(this, alert);
            mMediaPlayer.setLooping(true);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
        } catch (final IOException e) {
            Timber.e("Failed to play alarm tone: %s", e);
        }
    }

    mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    mVibrator.vibrate(sVibratePattern, 0);

    mPlaying = true;
    mHandler.sendEmptyMessageDelayed(KILLER, ALARM_TIMEOUT);
}