Example usage for android.media MediaPlayer release

List of usage examples for android.media MediaPlayer release

Introduction

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

Prototype

public void release() 

Source Link

Document

Releases resources associated with this MediaPlayer object.

Usage

From source file:inujini_.hatate.service.PierceReceiver.java

@Override
protected void asyncOnReceive(Context context, Intent intent) {
    Util.dbUpdate(context);//from w ww  .  j  ava  2  s  .  c  om

    _statistics = StatisticsDao.getStatistics(context);

    // 
    if (context.isScream()) {
        val mp = MediaPlayer.create(context, Love.getVoice(_statistics.getLove()));
        mp.seekTo(0);
        mp.start();
        mp.setOnCompletionListener(new OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer x) {
                x.release();
            }
        });
    }

    // 
    val notifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    val killIntent = PendingIntent.getService(context, REQ_KILL, new Intent(context, Kill.class),
            PendingIntent.FLAG_UPDATE_CURRENT);

    val notify = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher)
            .setTicker("!!!").setWhen(System.currentTimeMillis())
            .setContentTitle("Hatate Houtyou Alarm")
            .setContentText("!!!").setContentIntent(killIntent)
            .setDeleteIntent(killIntent);

    // LED
    if (context.isLight()) {
        notify.setLights((int) context.getLightColor(), 3000, 3000);
    }

    // ?
    if (context.isVibration()) {
        notify.setVibrate(context.getVibrationPattern());
    }

    notifyManager.notify(NOTIFY_HATATE_HOUTYOU, notify.build());

    // 
    if (context.isSnooze()) {
        Util.setSnooze(context);
    }

    onAfterPierced(context, intent);
}

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.xlythe.engine.theme.Theme.java

public static long getDurationOfSound(Context context, Theme.Res res) {
    int millis = 0;
    MediaPlayer mp = new MediaPlayer();
    try {//from   w ww  .j  a va 2s.c om
        AssetFileDescriptor afd;
        int id = getId(context, res.getType(), res.getName());
        if (id == 0) {
            id = context.getResources().getIdentifier(res.getName(), res.getType(), context.getPackageName());
            afd = context.getResources().openRawResourceFd(id);
        }

        afd = getThemeContext(context).getResources().openRawResourceFd(id);
        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        afd.close();
        mp.prepare();
        millis = mp.getDuration();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        mp.release();
        mp = null;
    }

    return millis;
}

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

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

From source file:mozilla.voicejam.singwithme.CameraActivity.java

void play() {
    Uri uri = Uri.parse(filePath);/*w  ww  .  j a va  2  s  . c  o m*/
    MediaPlayer mediaPlayer = MediaPlayer.create(this, uri);
    mediaPlayer.start();
    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer MP) {
            MP.release();
        }
    });
}

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   w ww . j  a va2  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 (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   w w  w  .  j  a  v  a  2  s  .co  m
    } 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:mozilla.voicejam.singwithme.CameraActivity.java

void start() {
    fragment.takePictureAndBlur(0.5f);/*w w w .  j  ava2 s  .  c o  m*/

    mediaPlayer = MediaPlayer.create(this, R.raw.music_sample);
    mediaPlayer.start();
    mediaPlayer.getCurrentPosition();
    songDuration = mediaPlayer.getDuration();

    start = System.currentTimeMillis();
    runnable.run();

    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer1) {
            mediaPlayer1.release();
            mediaPlayer = null;
            handler.removeCallbacks(runnable);
        }
    });
}

From source file:edu.northwestern.cbits.activitydetector.app.MainActivity.java

private void UpdateGUI() {
    i++;/*from w ww  .  j  a va  2 s  . co  m*/

    final MainActivity me = this;

    // Runs once a second
    mHandler.post(new Runnable() {
        public void run() {
            Log.d(getString(R.string.app_name), ActivityRecognitionIntentService.ACTIVITY_NAME);
            nameView.setText(
                    ActivityRecognitionIntentService.ACTIVITY_NAME + " " + System.currentTimeMillis() / 1000);

            //setting up tone for on_foot
            Uri defaultRingtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            MediaPlayer mediaPlayer = new MediaPlayer();

            try {
                mediaPlayer.setDataSource(me, defaultRingtoneUri);
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
                mediaPlayer.prepare();
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        mp.release();
                    }
                });

                if (ActivityRecognitionIntentService.ACTIVITY_NAME == "on_foot") {
                    mediaPlayer.start();
                } else {
                    mediaPlayer.release();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            ;
        }
    });
}

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

private void freeMediaPlayers() {
    final int N = mMediaPlayers.size();
    for (int i = 0; i < N; i++) {
        MediaPlayer mp = mMediaPlayers.get(mMediaPlayers.keyAt(i));
        if (mp != null) {
            mp.stop();/*from w w w. j a v a  2  s  .  com*/
            mp.release();
        }
    }
    mMediaPlayers.clear();
}