Search in sources :

Example 31 with AudioAttributes

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;
}
Also used : Button(android.widget.Button) OnLoadCompleteListener(android.media.SoundPool.OnLoadCompleteListener) AudioAttributes(android.media.AudioAttributes) SoundPool(android.media.SoundPool)

Example 32 with AudioAttributes

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);
        }
    }
}
Also used : AsyncPlayer(android.media.AsyncPlayer) AudioAttributes(android.media.AudioAttributes) Uri(android.net.Uri)

Example 33 with AudioAttributes

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;
}
Also used : AudioAttributes(android.media.AudioAttributes)

Example 34 with AudioAttributes

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());
        }
    });
}
Also used : NotificationManager(android.app.NotificationManager) Bundle(android.os.Bundle) NotificationManagerCompat(android.support.v4.app.NotificationManagerCompat) ContextHandler(com.backendless.ContextHandler) Handler(android.os.Handler) AudioAttributes(android.media.AudioAttributes) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) NotificationChannel(android.app.NotificationChannel) NotificationCompat(android.support.v4.app.NotificationCompat) PendingIntent(android.app.PendingIntent)

Example 35 with AudioAttributes

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());
}
Also used : AudioAttributes(android.media.AudioAttributes) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Aggregations

AudioAttributes (android.media.AudioAttributes)46 NotificationChannel (android.app.NotificationChannel)13 Notification (android.app.Notification)11 RemoteException (android.os.RemoteException)10 TargetApi (android.annotation.TargetApi)9 Uri (android.net.Uri)9 NotificationManager (android.app.NotificationManager)6 Test (org.junit.Test)6 Config (org.robolectric.annotation.Config)6 ITransientNotification (android.app.ITransientNotification)5 IRingtonePlayer (android.media.IRingtonePlayer)5 SoundPool (android.media.SoundPool)5 StatusBarNotification (android.service.notification.StatusBarNotification)5 VisibleForTesting (com.android.internal.annotations.VisibleForTesting)5 NotificationChannelGroup (android.app.NotificationChannelGroup)4 PendingIntent (android.app.PendingIntent)3 AudioManager (android.media.AudioManager)3 AudioPlaybackConfiguration (android.media.AudioPlaybackConfiguration)3 RequiresApi (android.support.annotation.RequiresApi)3 Intent (android.content.Intent)2