Example usage for android.media AudioAttributes USAGE_NOTIFICATION

List of usage examples for android.media AudioAttributes USAGE_NOTIFICATION

Introduction

In this page you can find the example usage for android.media AudioAttributes USAGE_NOTIFICATION.

Prototype

int USAGE_NOTIFICATION

To view the source code for android.media AudioAttributes USAGE_NOTIFICATION.

Click Source Link

Document

Usage value to use when the usage is notification.

Usage

From source file:com.sxt.chat.utils.NotificationHelper.java

/**
 * ?// w  ww .  j a  va  2  s  . c  o m
 * <p>
 * note : notify() ? ???,?? so, Create?
 */
public NotificationHelper(Context ctx) {
    super(ctx);
    context = ctx;
    soundUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notify_message);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        AudioAttributes att = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH).build();
        NotificationChannel channel = new NotificationChannel(DEFAULT_CHANNEL, "Channel",
                NotificationManager.IMPORTANCE_HIGH);
        channel.setSound(soundUri, att);
        channel.setLightColor(Color.YELLOW);
        channel.setShowBadge(true);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        getManager().createNotificationChannel(channel);

        NotificationChannel chanCustom = new NotificationChannel(CUSTOM_NOTIFY_CHANNEL, "Custom Layout Channel",
                NotificationManager.IMPORTANCE_HIGH);
        channel.setSound(soundUri, att);
        chanCustom.setLightColor(Color.RED);
        chanCustom.setShowBadge(true);
        chanCustom.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        getManager().createNotificationChannel(chanCustom);
    }
}

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

private void notifyStepFinish() {
    if (vibrateWhenStepFinish()) {
        ((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(500);
    }/* w w  w.  ja v  a  2s.  c om*/

    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();
            }
        }
    }
}

From source file:org.wso2.iot.agent.AlertActivity.java

/**
 * This method is used to start ringing the phone.
 *//*w  w  w.  j  av a2  s.  c  om*/
@TargetApi(21)
private void startRing() {
    if (audio != null) {
        ringerMode = audio.getRingerMode();
        ringerVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
        audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        audio.setStreamVolume(AudioManager.STREAM_RING, audio.getStreamMaxVolume(AudioManager.STREAM_RING),
                AudioManager.FLAG_PLAY_SOUND);

        defaultRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE);

        if (defaultRingtoneUri != null) {
            defaultRingtone = RingtoneManager.getRingtone(this, defaultRingtoneUri);

            if (defaultRingtone != null) {
                if (deviceInfo.getSdkVersion() >= Build.VERSION_CODES.LOLLIPOP) {
                    AudioAttributes attributes = new AudioAttributes.Builder()
                            .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).build();
                    defaultRingtone.setAudioAttributes(attributes);
                } else {
                    defaultRingtone.setStreamType(AudioManager.STREAM_NOTIFICATION);
                }
                defaultRingtone.play();
            }
        }
    }
}

From source file:org.wso2.emm.agent.AlertActivity.java

/**
 * This method is used to start ringing the phone.
 *//*from ww  w . j  a  va 2  s .  c o  m*/
@TargetApi(21)
private void startRing() {
    if (audio != null) {
        audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        audio.setStreamVolume(AudioManager.STREAM_RING, audio.getStreamMaxVolume(AudioManager.STREAM_RING),
                AudioManager.FLAG_PLAY_SOUND);

        defaultRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE);

        if (defaultRingtoneUri != null) {
            defaultRingtone = RingtoneManager.getRingtone(this, defaultRingtoneUri);

            if (defaultRingtone != null) {
                if (deviceInfo.getSdkVersion() >= Build.VERSION_CODES.LOLLIPOP) {
                    AudioAttributes attributes = new AudioAttributes.Builder()
                            .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).build();
                    defaultRingtone.setAudioAttributes(attributes);
                } else {
                    defaultRingtone.setStreamType(AudioManager.STREAM_NOTIFICATION);
                }
                defaultRingtone.play();
            }
        }
    }
}