android - NotificationCompat setSound(声音,STREAM_ALARM)不工作

标签 android audio android-notifications android-permissions

我正在构建一个 ping 功能,用于通过蓝牙寻找丢失的手机。我需要手机发出声音,即使它设置为静音/无声,就像闹钟通常的工作方式一样。我以为我可以将我的 notificationstreamtype 放入 AudioManager.STREAM_ALARM 但它不起作用。它仅在电话声音打开时发出声音。我是这样设置的:

NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setSmallIcon(R.drawable.ic_spenwallet)
        .setContentTitle("Ping")
        .setContentText("Device is trying to find your phone.")
        .setAutoCancel(false)
        .setSound(sound, STREAM_ALARM)
        .setVibrate(vibratePattern)
        .addAction(cancelAction);

如果我尝试:

    Notification notification = builder.build();
    notification.audioStreamType = AudioManager.STREAM_ALARM;

我从 Android Studio 收到一条警告,指出 audioStreamType 已弃用。是这样吗?即使开启了静音模式,还有其他方法可以发出 notificaiton 声音吗? (最好也振动)

我为此目的创建了一个专用的媒体播放器,但我认为不需要这样做。无论如何,我是这样做的:

    MediaPlayer mediaPlayer = new MediaPlayer();
    final String packageName = getApplicationContext().getPackageName();
    Uri sound = Uri.parse("android.resource://" + packageName + "/" + R.raw.ping_sound);

    try {
        mediaPlayer.setDataSource(this, sound);
    } catch (IOException e) {
        e.printStackTrace();
    }

    final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
        mediaPlayer.setLooping(false);
        try {
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mediaPlayer.start();
    } 

最佳答案

使用 builder.setSound(alarmSound, AudioManager.STREAM_AUDIO) 正是我让闹钟继续运行所需要的!也许您的问题与您正在使用的 R.raw.ping_sound 声音样本有关。在尝试了一堆可怕的实现之后,我在网上找到了警报通知(这是我找到 Settings.System.DEFAULT_RINGTONE_URI 的地方),我遵循了 official notification documentation然后使用 NotificationCompat.Builder定制文档。

这是我的工作警报通知:

private void showNotification(){
    // Setup Intent for when Notification clicked
    Intent intent = new Intent(mContext, MedsActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); // See https://developer.android.com/training/notify-user/navigation for better navigation
    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);

    // Setup Ringtone & Vibrate
    Uri alarmSound = Settings.System.DEFAULT_RINGTONE_URI;
    long[] vibratePattern = { 0, 100, 200, 300 };

    // Setup Notification
    String channelID = mContext.getResources().getString(R.string.channel_id_alarms);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext, channelID)
            .setContentText(notificationMessage)
            .setContentTitle(notificationTitle)
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setCategory(NotificationCompat.CATEGORY_ALARM)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setContentIntent(pendingIntent)
            .setSound(alarmSound, AudioManager.STREAM_ALARM)
            .setOnlyAlertOnce(true)
            .setVibrate(vibratePattern)
            .setAutoCancel(true);

    // Send Notification
    NotificationManager manager = (NotificationManager) mContext.getSystemService(NOTIFICATION_SERVICE);
    manager.notify(NOTIFICATION_ID, mBuilder.build());
}

关于android - NotificationCompat setSound(声音,STREAM_ALARM)不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41975187/

相关文章:

安卓网络接口(interface)。 NetworkInterface 名称的含义是什么?

c# - 如何使用 .NET (Windows/Mac/Linux) 将 PCM S16LE 音频转换为 MU-LAW/8000

java - 使用java识别mp3末尾的静音

android - Android 中是否有针对 "users blocking our notifications"的 API?

Android AppWidget : new instance freezing old one

android - 如何在 tabView 中调用撰写消息 Activity?

javascript - 来自缓存的 HTML 音频元素源

java - 让 android 通知打开新 Activity

android - 在 Android Wear 中显示持续通知

android - 蓝牙音频不会立即恢复