Example usage for android.media MediaPlayer create

List of usage examples for android.media MediaPlayer create

Introduction

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

Prototype

public static MediaPlayer create(Context context, int resid, AudioAttributes audioAttributes,
        int audioSessionId) 

Source Link

Document

Same factory method as #create(Context,int) but that lets you specify the audio attributes and session ID to be used by the new MediaPlayer instance.

Usage

From source file:io.github.carlorodriguez.morningritual.MainActivity.java

private void notifyStepFinish() {
    if (vibrateWhenStepFinish()) {
        ((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(500);
    }//w  w  w.jav a 2s . c o m

    if (soundWhenStepFinish()) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION).build();

            AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

            MediaPlayer.create(this, R.raw.sound, audioAttributes, audioManager.generateAudioSessionId())
                    .start();
        } else {
            try {
                playLegacyNotificationSound();
            } catch (IOException e) {
                Toast.makeText(MainActivity.this, R.string.cannot_load_notification_sound, Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }
}