use of android.media.AudioAttributes in project coursera-android by aporter.
the class AudioVideoAudioManagerActivity method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get reference to the AudioManager
mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
// Display current volume level in TextView
mTextView = findViewById(R.id.textView1);
mTextView.setText(String.valueOf(mVolume));
final Button playButton = findViewById(R.id.button3);
// Disable the Play Button so user can't click it before sounds are ready
playButton.setEnabled(false);
// Create a SoundPool
AudioAttributes audioAttributes = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build();
mSoundPool = new SoundPool.Builder().setAudioAttributes(audioAttributes).build();
// Load bubble popping sound into the SoundPool
mSoundId = mSoundPool.load(this, R.raw.slow_whoop_bubble_pop, 1);
// Set an OnLoadCompleteListener on the SoundPool
mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
// If sound loading was successful enable the play Button
if (0 == status) {
playButton.setEnabled(true);
} else {
Log.i(TAG, "Unable to load sound");
finish();
}
}
});
// Request audio focus
int result = mAudioManager.requestAudioFocus(afChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
// Set to true if app has audio focus
mCanPlayAudio = AudioManager.AUDIOFOCUS_REQUEST_GRANTED == result;
}
use of android.media.AudioAttributes in project Shuttle by timusus.
the class MediaButtonIntentReceiver method beep.
static void beep(Context context) {
if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean("pref_headset_beep", true)) {
AsyncPlayer beepPlayer = new AsyncPlayer("BeepPlayer");
Uri beepSoundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getResources().getResourcePackageName(R.raw.beep) + '/' + context.getResources().getResourceTypeName(R.raw.beep) + '/' + context.getResources().getResourceEntryName(R.raw.beep));
if (ShuttleUtils.hasMarshmallow()) {
AudioAttributes audioAttributes = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).build();
beepPlayer.play(context, beepSoundUri, false, audioAttributes);
} else {
beepPlayer.play(context, beepSoundUri, false, AudioManager.STREAM_MUSIC);
}
}
}
use of android.media.AudioAttributes in project Android-SDK by Backendless.
the class PushTemplateHelper method updateNotificationChannel.
private static NotificationChannel updateNotificationChannel(Context context, NotificationChannel notificationChannel, final AndroidPushTemplate template) {
if (template.getShowBadge() != null)
notificationChannel.setShowBadge(template.getShowBadge());
if (template.getPriority() != null && template.getPriority() > 0 && template.getPriority() < 6)
// NotificationManager.IMPORTANCE_DEFAULT
notificationChannel.setImportance(template.getPriority());
AudioAttributes audioAttributes = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE).setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED).setLegacyStreamType(AudioManager.STREAM_NOTIFICATION).build();
notificationChannel.setSound(PushTemplateHelper.getSoundUri(context, template.getSound()), audioAttributes);
if (template.getLightsColor() != null) {
notificationChannel.enableLights(true);
notificationChannel.setLightColor(template.getLightsColor() | 0xFF000000);
}
if (template.getVibrate() != null && template.getVibrate().length > 0) {
long[] vibrate = new long[template.getVibrate().length];
int index = 0;
for (long l : template.getVibrate()) vibrate[index++] = l;
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(vibrate);
}
return notificationChannel;
}
use of android.media.AudioAttributes in project Android-SDK by Backendless.
the class BackendlessFCMService method fallBackMode.
private void fallBackMode(Context context, String message, String contentTitle, String summarySubText, String soundResource, Bundle bundle, final int notificationId) {
final String channelName = "Fallback";
final NotificationCompat.Builder notificationBuilder;
Bundle newBundle = new Bundle();
newBundle.putAll(bundle);
newBundle.putInt(PublishOptions.NOTIFICATION_ID, notificationId);
// android.os.Build.VERSION_CODES.O == 26
if (android.os.Build.VERSION.SDK_INT > 25) {
final String channelId = PushTemplateHelper.getChannelId(channelName);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel notificationChannel = notificationManager.getNotificationChannel(channelId);
if (notificationChannel == null) {
notificationChannel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
AudioAttributes audioAttributes = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE).setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED).setLegacyStreamType(AudioManager.STREAM_NOTIFICATION).build();
notificationChannel.setSound(PushTemplateHelper.getSoundUri(context, soundResource), audioAttributes);
notificationManager.createNotificationChannel(notificationChannel);
}
notificationBuilder = new NotificationCompat.Builder(context, notificationChannel.getId());
} else
notificationBuilder = new NotificationCompat.Builder(context);
notificationBuilder.setSound(PushTemplateHelper.getSoundUri(context, soundResource), AudioManager.STREAM_NOTIFICATION);
int appIcon = context.getApplicationInfo().icon;
if (appIcon == 0)
appIcon = android.R.drawable.sym_def_app_icon;
Intent notificationIntent = context.getPackageManager().getLaunchIntentForPackage(context.getApplicationInfo().packageName);
notificationIntent.putExtras(newBundle);
PendingIntent contentIntent = PendingIntent.getActivity(context, notificationId * 3, notificationIntent, 0);
notificationBuilder.setContentIntent(contentIntent).setSmallIcon(appIcon).setContentTitle(contentTitle).setSubText(summarySubText).setContentText(message).setWhen(System.currentTimeMillis()).setAutoCancel(true).build();
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
notificationManager.notify(channelName, notificationId, notificationBuilder.build());
}
});
}
use of android.media.AudioAttributes in project robolectric by robolectric.
the class ShadowVibratorTest method vibratePattern_withVibrationAttributes.
@Config(minSdk = S)
@Test
public void vibratePattern_withVibrationAttributes() {
AudioAttributes audioAttributes = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_REQUEST).setFlags(AudioAttributes.FLAG_BYPASS_INTERRUPTION_POLICY).build();
vibrator.vibrate(VibrationEffect.createPredefined(EFFECT_CLICK), audioAttributes);
assertThat(shadowOf(vibrator).getVibrationAttributesFromLastVibration()).isEqualTo(new VibrationAttributes.Builder(audioAttributes, VibrationEffect.createPredefined(EFFECT_CLICK)).build());
}
Aggregations