将声音设置为通知频道
Uri alarmUri = Uri.fromFile(new File(<path>)); AudioAttributes attributes = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_ALARM) .build(); channel.setSound(alarmUri, attributes);
如果您想要播放默认通知声音,则可以使用 setDefaults(INT) 的方法 NotificationCompat.Builder 类:
NotificationCompat.Builder
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_notification) .setContentTitle(getString(R.string.app_name)) .setContentText(someText) .setDefaults(Notification.DEFAULT_SOUND) .setAutoCancel(true);
我相信这是完成任务的最简单方法。
我有几乎相同的问题。经过一些研究,我认为如果你想播放默认系统“通知声音”,你几乎必须显示一个通知并告诉它使用默认声音。对于其他一些答案中的论点,有一些事情可以说,如果你正在播放通知声音,你也应该提出一些通知信息。
但是,稍微调整一下通知API,您就可以得到您想要的内容。您可以显示空白通知,然后在几秒钟后自动将其删除。我认为这对我有用;也许它会对你有用。
我已经创建了一套方便的方法 com.globalmentor.android.app.Notifications.java 允许您创建这样的通知声音:
com.globalmentor.android.app.Notifications.java
Notifications.notify(this);
LED也会闪烁,如果您有振动许可,则会发生振动。是的,通知栏中会出现一个通知图标,但几秒钟后会消失。
此时您可能会意识到,由于通知无论如何都会消失,您可能还会在通知栏中显示滚动的自动收报机消息;你可以这样做:
Notifications.notify(this, 5000, "This text will go away after five seconds.");
这个课程还有许多其他方便的方法。您可以从其Subversion存储库下载整个库,并使用Maven构建它。它取决于globalmentor-core库,它也可以使用Maven构建和安装。
您可以使用 通知 和 NotificationManager 显示所需的通知。然后,您可以自定义要通知的声音。
现在,您可以通过在构建通知时包含声音而不是单独调用声音来执行此操作。
//Define Notification Manager NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); //Define sound URI Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext()) .setSmallIcon(icon) .setContentTitle(title) .setContentText(message) .setSound(soundUri); //This sets the sound to play //Display notification notificationManager.notify(0, mBuilder.build());
我认为“通知声音”的概念在某些方面对Android UI来说是错误的。
Android预期的行为是使用标准通知来提醒用户。如果您在没有状态栏图标的情况下播放通知声音,则会让用户感到困惑(“那是什么声音?这里没有图标,也许我有听力问题?”)。
例如,如何在通知上设置声音: 设置通知声音
你的问题已经有一段时间了,但是......你尝试过设置音频流类型吗?
mp.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
必须在准备之前完成。
试试这个:
public void ringtone(){ try { Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification); r.play(); } catch (Exception e) { e.printStackTrace(); } }
Intent intent = new Intent(this, MembersLocation.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("type",type); intent.putExtra("sender",sender); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); String channelId = getString(R.string.default_notification_channel_id); Uri Emergency_sound_uri=Uri.parse("android.resource://"+getPackageName()+"/raw/emergency_sound"); // Uri Default_Sound_uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); if(type.equals("emergency")) { playSound=Emergency_sound_uri; } else { playSound= Settings.System.DEFAULT_NOTIFICATION_URI; } NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId) .setSmallIcon(R.drawable.ic_notification) .setContentTitle(title) .setContentText(body) .setSound(playSound, AudioManager.STREAM_NOTIFICATION) .setAutoCancel(true) .setColor(getColor(R.color.dark_red)) .setPriority(NotificationCompat.PRIORITY_HIGH) .setContentIntent(pendingIntent); // notificationBuilder.setOngoing(true);//for Android notification swipe delete disabling... NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // Since android Oreo notification channel is needed. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(channelId, "Channel human readable title", NotificationManager.IMPORTANCE_HIGH); AudioAttributes att = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_NOTIFICATION) .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH) .build(); channel.setSound(Emergency_sound_uri, att); if (notificationManager != null) { notificationManager.createNotificationChannel(channel); } } if (notificationManager != null) { notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); } }