Java Code Examples for android.media.AudioAttributes#Builder

The following examples show how to use android.media.AudioAttributes#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KcaAlarmService.java    From kcanotify_h5-master with GNU General Public License v3.0 6 votes vote down vote up
private void createAlarmChannel(String uri) {
    Log.e("KCA-A", "recv: " + uri);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String soundKind = getStringPreferences(getApplicationContext(), PREF_KCA_NOTI_SOUND_KIND);
        boolean isVibrate = soundKind.equals(getString(R.string.sound_kind_value_mixed)) || soundKind.equals(getString(R.string.sound_kind_value_vibrate));
        notificationManager.deleteNotificationChannel(ALARM_CHANNEL_ID);
        while (!alarmChannelList.isEmpty()) {
            notificationManager.deleteNotificationChannel(alarmChannelList.poll());
        }

        AudioAttributes.Builder attrs = new AudioAttributes.Builder();
        attrs.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION);
        attrs.setUsage(AudioAttributes.USAGE_NOTIFICATION);

        String channel_name = createAlarmId(uri, isVibrate);
        alarmChannelList.add(channel_name);
        NotificationChannel channel = new NotificationChannel(alarmChannelList.peek(),
                getStringWithLocale(R.string.notification_appinfo_title), NotificationManager.IMPORTANCE_HIGH);
        channel.setSound(Uri.parse(uri), attrs.build());
        channel.enableVibration(isVibrate);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        notificationManager.createNotificationChannel(channel);
    }
}
 
Example 2
Source File: KcaAlarmService.java    From kcanotify with GNU General Public License v3.0 6 votes vote down vote up
private void createAlarmChannel(String uri) {
    Log.e("KCA-A", "recv: " + uri);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String soundKind = getStringPreferences(getApplicationContext(), PREF_KCA_NOTI_SOUND_KIND);
        boolean isVibrate = soundKind.equals(getString(R.string.sound_kind_value_mixed)) || soundKind.equals(getString(R.string.sound_kind_value_vibrate));
        notificationManager.deleteNotificationChannel(ALARM_CHANNEL_ID);
        while (!alarmChannelList.isEmpty()) {
            notificationManager.deleteNotificationChannel(alarmChannelList.poll());
        }

        AudioAttributes.Builder attrs = new AudioAttributes.Builder();
        attrs.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION);
        attrs.setUsage(AudioAttributes.USAGE_NOTIFICATION);

        String channel_name = createAlarmId(uri, isVibrate);
        alarmChannelList.add(channel_name);
        NotificationChannel channel = new NotificationChannel(alarmChannelList.peek(),
                getStringWithLocale(R.string.notification_appinfo_title), NotificationManager.IMPORTANCE_HIGH);
        channel.setSound(Uri.parse(uri), attrs.build());
        channel.enableVibration(isVibrate);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        notificationManager.createNotificationChannel(channel);
    }
}
 
Example 3
Source File: VideoView.java    From ExoMedia with Apache License 2.0 5 votes vote down vote up
/**
 * Requests to obtain the audio focus
 *
 * @return True if the focus was granted
 */
public boolean requestFocus() {
    if (!handleAudioFocus || currentFocus == AudioManager.AUDIOFOCUS_GAIN) {
        return true;
    }

    if (audioManager == null) {
        return false;
    }
    int status;
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        AudioFocusRequest.Builder afBuilder = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN);
        AudioAttributes.Builder aaBuilder = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_MEDIA).setContentType(AudioAttributes.CONTENT_TYPE_MUSIC);
        lastFocusRequest = afBuilder.setAudioAttributes(aaBuilder.build()).build();
        status = audioManager.requestAudioFocus(lastFocusRequest);
    } else {
        status = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    }
    if (AudioManager.AUDIOFOCUS_REQUEST_GRANTED == status) {
        currentFocus = AudioManager.AUDIOFOCUS_GAIN;
        return true;
    }

    startRequested = true;
    return false;
}
 
Example 4
Source File: SimpleAudioOutput.java    From android-midisuite with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.M)
protected AudioTrack createAudioTrack() {
    AudioAttributes.Builder attributesBuilder = new AudioAttributes.Builder()
            .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC);
    boolean doLowLatency = (AudioLatencyTuner.isLowLatencySupported()
            && mLatencyController.isLowLatencyEnabled());
    if (doLowLatency) {
        Log.i(TAG, "createAudioTrack() using FLAG_LOW_LATENCY");
        attributesBuilder.setFlags(AudioLatencyTuner.getLowLatencyFlag());
    }
    AudioAttributes attributes = attributesBuilder.build();

    AudioFormat format = new AudioFormat.Builder()
            .setEncoding(AudioFormat.ENCODING_PCM_FLOAT)
            .setChannelMask(AudioFormat.CHANNEL_OUT_STEREO)
            .build();
    AudioTrack.Builder builder = new AudioTrack.Builder()
            .setAudioAttributes(attributes)
            .setAudioFormat(format);
    if (doLowLatency) {
        // Start with a bigger buffer because we can lower it later.
        int bufferSizeInFrames = LOW_LATENCY_BUFFER_CAPACITY_IN_FRAMES;
        builder.setBufferSizeInBytes(bufferSizeInFrames * BYTES_PER_FRAME);
    }
    AudioTrack track = builder.build();
    if (track == null) {
        throw new RuntimeException("Could not make the Audio Track! attributes = "
                + attributes + ", format = " + format);
    }
    return track;
}
 
Example 5
Source File: NotificationChannelManagerHelper.java    From notification-channel-compat with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the notification channel settings for a given channel id.
 * <p>
 * The channel must belong to your package, or it will not be returned.
 */
public NotificationChannelCompat getNotificationChannel(String channelId) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        return new NotificationChannelCompat(_manager.getNotificationChannel(channelId));

    }
    if (!_channelIds.contains(channelId))
        return null;

    int importance = Integer.parseInt(prefsGetString(PREF_KEY_CHANNEL_IMPORTANCE, channelId, Integer.toString(NotificationManagerCompat.IMPORTANCE_DEFAULT))); // as Pref is a string, we have to jump through loop to get is as int
    NotificationChannelCompat channel = new NotificationChannelCompat(channelId, prefsGetString(PREF_KEY_CHANNEL_NAME, channelId, "Error"), importance);
    channel.setDescription(prefsGetString(PREF_KEY_CHANNEL_DESCRIPTION, channelId, "None"));
    channel.setEnabled(prefsGetBoolean(PREF_KEY_CHANNEL_ENABLED, channelId, channel.isEnabled()));
    channel.setGroup(prefsGetString(PREF_KEY_CHANNEL_GROUP, channelId, null));
    channel.setLockscreenVisibility(prefsGetInt(PREF_KEY_CHANNEL_LOCKSCREENVISIBILITY, channelId, channel.getLockscreenVisibility()));
    channel.enableLights(prefsGetBoolean(PREF_KEY_CHANNEL_LIGHTS, channelId, channel.shouldShowLights()));
    channel.setLightColor(prefsGetInt(PREF_KEY_CHANNEL_LIGHTCOLOR, channelId, channel.getLightColor()));

    String soundUriStr = prefsGetString(PREF_KEY_CHANNEL_SOUND, channelId, null);
    Uri soundUri = TextUtils.isEmpty(soundUriStr) ? null : Uri.parse(soundUriStr);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && _prefs.contains(makeKey(PREF_KEY_CHANNEL_AUDIOATTRIBUTESCONTENTTYPE, channelId))) {
        AudioAttributes.Builder audioBuilder = new AudioAttributes.Builder();
        audioBuilder.setContentType(prefsGetInt(PREF_KEY_CHANNEL_AUDIOATTRIBUTESCONTENTTYPE, channelId, 0));
        audioBuilder.setFlags(prefsGetInt(PREF_KEY_CHANNEL_AUDIOATTRIBUTESFLAGS, channelId, 0));
        audioBuilder.setUsage(prefsGetInt(PREF_KEY_CHANNEL_AUDIOATTRIBUTESUSAGE, channelId, 0));

        channel.setSound(soundUri, audioBuilder.build());
    }
    channel.setSound(soundUri, prefsGetInt(PREF_KEY_CHANNEL_AUDIOSTREAMTYPE, channelId, channel.getAudioStreamType())); // support pre-lollypop

    channel.enableVibration(prefsGetBoolean(PREF_KEY_CHANNEL_VIBRATIONENABLED, channelId, channel.shouldVibrate()));
    String vibrationPatternStr = prefsGetString(PREF_KEY_CHANNEL_VIBRATION, channelId, null);
    if (vibrationPatternStr != null) {
        String[] vibrationPatternStrArray = vibrationPatternStr.split(",");
        int length = vibrationPatternStrArray.length;
        long[] vibrationPattern = new long[length];
        for (int i = 0; i < length; ++i) {
            vibrationPattern[i] = Long.parseLong(vibrationPatternStrArray[i]);
        }
        channel.setVibrationPattern(vibrationPattern);
    }

    return channel;
}
 
Example 6
Source File: MediaSessionCompatApi21.java    From adt-leanback-support with Apache License 2.0 4 votes vote down vote up
public static void setPlaybackToLocal(Object sessionObj, int stream) {
    // TODO update APIs to use support version of AudioAttributes
    AudioAttributes.Builder bob = new AudioAttributes.Builder();
    bob.setLegacyStreamType(stream);
    ((MediaSession) sessionObj).setPlaybackToLocal(bob.build());
}