我想知道如何在不通过媒体流播放声音的情况下播放通知声音。目前我可以通过媒体播放器实现这一点,但我不希望它以媒体文件的形式播放,而是希望它作为通知、警报或铃声来播放。以下是我当前代码的示例:
MediaPlayer mp = new MediaPlayer();
mp.reset();
mp.setDataSource(notificationsPath+ (String) apptSounds.getSelectedItem());
mp.prepare();
mp.start();
我想知道如何在不通过媒体流播放声音的情况下播放通知声音。目前我可以通过媒体播放器实现这一点,但我不希望它以媒体文件的形式播放,而是希望它作为通知、警报或铃声来播放。以下是我当前代码的示例:
MediaPlayer mp = new MediaPlayer();
mp.reset();
mp.setDataSource(notificationsPath+ (String) apptSounds.getSelectedItem());
mp.prepare();
mp.start();
如果还有人在寻找解决方案,我在How to play ringtone/alarm sound in Android找到了答案。
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
你可以将TYPE_NOTIFICATION更改为TYPE_ALARM,但你需要跟踪你的铃声(Ringtone r),以便在用户点击按钮或其他情况下停止播放它。
现在您可以在构建通知时包含声音,而无需单独调用声音。
//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());
NotificationCompat.Builder
类的setDefaults(int)方法: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);
我相信这是完成您任务最简单的方式。
试一下这个:
public void ringtone(){
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
}
虽然你的问题已经有一段时间了,但是...您是否尝试设置音频流类型?
mp.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
在准备之前必须完成。
com.globalmentor.android.app.Notifications.java
中创建了一组方便的方法,允许你创建一个通知声音,如下所示:Notifications.notify(this);
Notifications.notify(this, 5000, "This text will go away after five seconds.");
这个类中还有许多其他方便的方法。您可以从其Subversion存储库下载整个库,并使用Maven构建它。它依赖于globalmentor-core库,该库也可以使用Maven构建和安装。
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());
}
}
我认为“通知声音”的概念在Android UI中有些错误。
Android的预期行为是使用标准通知来提醒用户。如果您播放通知声音而没有状态栏图标,那么会让用户感到困惑(“那是什么声音?这里没有图标,也许我听力有问题?”)。
如何设置通知声音,例如在这里:设置通知声音
您可以使用Notification和NotificationManager来显示您想要的通知。然后,您可以自定义您想要播放的通知声音。
设置声音到通知通道
Uri alarmUri = Uri.fromFile(new File(<path>));
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM)
.build();
channel.setSound(alarmUri, attributes);
MediaPlayer - 应该已经设置了字幕控制器
。这是什么意思? - Deqing