nin*_*nse 159 audio notifications android
我想知道如何在不播放媒体流的情况下播放通知声音.现在我可以通过媒体播放器这样做,但我不希望它作为媒体文件播放,我希望它作为通知或警报或铃声播放.下面是我的代码现在看起来像一个例子:
MediaPlayer mp = new MediaPlayer();
mp.reset();
mp.setDataSource(notificationsPath+ (String) apptSounds.getSelectedItem());
mp.prepare();
mp.start();
Run Code Online (Sandbox Code Playgroud)
小智 405
如果有人还在寻找解决方案,我找到了如何在Android中播放铃声/闹钟声音的答案
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
您可以将TYPE_NOTIFICATION更改为TYPE_ALARM,但是您需要跟踪您的铃声r以便停止播放...比如,当用户点击按钮或其他内容时.
Rob*_*dle 205
现在,您可以通过在构建通知时包含声音而不是单独调用声音来完成此操作.
//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());
Run Code Online (Sandbox Code Playgroud)
aga*_*aga 47
如果要播放默认通知声音,则可以使用类的setDefaults(int)方法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);
Run Code Online (Sandbox Code Playgroud)
我相信这是完成任务的最简单方法.
Rag*_*agu 11
试试这个:
public void ringtone(){
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
你的问题已经有一段时间了,但是......你尝试过设置音频流类型吗?
mp.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
Run Code Online (Sandbox Code Playgroud)
必须在准备之前完成.
| 归档时间: |
|
| 查看次数: |
206339 次 |
| 最近记录: |