我正在实施以下通知。默认警报声音正常,但只有一次。我想要的只是重复播放,直到点击水龙头。
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("I Am Fine")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(NOTIFICATION_MESSAGE))
.setContentText(NOTIFICATION_MESSAGE)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
mBuilder.setSound(alarmSound,AudioManager.STREAM_MUSIC);
setSound的第二个参数没有显示任何效果。请帮忙!
答案 0 :(得分:12)
您必须使用FLAG_INSISTENT进行通知。来自documentation: -
public static final int FLAG_INSISTENT
要按位进入标志字段,如果设置,则音频将为 重复,直到通知被取消或通知窗口为止 打开。
常数值:4(0x00000004)
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Uri soundUri = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
getApplicationContext()).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("title").setContentText("message")
.setSound(soundUri); // This sets the sound to play
Notification mNotification = mBuilder.build();
mNotification.flags |= Notification.FLAG_INSISTENT;
// Display notification
notificationManager.notify(1, mNotification);
答案 1 :(得分:0)
我在SendNotification Method Body下调用了它。
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
PlaySound(this,notification);
} catch (Exception e) {
e.printStackTrace();
}
PlaySound定义如下:
private void PlaySound(Context context,Uri alert){
try{
mPlayer=new MediaPlayer();
mPlayer.setDataSource(context,alert);
final AudioManager am=(AudioManager) getSystemService(Context.AUDIO_SERVICE);
if(am.getStreamVolume(AudioManager.STREAM_ALARM)!=0);
{
mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mPlayer.prepare();
mPlayer.setLooping(true);
mPlayer.start();
}
}catch(IOException e)
{
Log.i("AlaramReciever", "no audio file");
}
}
要在点击通知时停止声音,我已将MediaPlayer对象更改为公共静态对象,如下所示:
if(GCMNotificationIntentService.mPlayer != null)
{
try{
GCMNotificationIntentService.mPlayer.stop();
GCMNotificationIntentService.mPlayer.release();
}finally {
GCMNotificationIntentService.mPlayer = null;
}
}
实现了所需的功能,但我一直关注RunTimeException
处理程序android.media.MediaPlayer $ EventHandler,当onDestroy立即被调用时,它正在向死线程上的Handler发送消息。有什么方法可以阻止这种情况吗?
答案 2 :(得分:0)
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM));
MediaPlayer player = MediaPlayer.create(this, notification);
player.setAudioStreamType(AudioManager.STREAM_ALARM);
player.setLooping(true);
player.start();
player.setLooping(true);
这将允许媒体播放器重复播放声音。在onclick()
按钮上使用player.stop();
停止声音
答案 3 :(得分:-2)
您应该将通知声音静音,只需使用媒体播放器播放应用中的声音文件即可。并设置媒体播放器的实例循环。
之后,您应该在通知中添加待处理的intent变量,一旦用户点击通知,相关事件就会被触发,您可以从此处停止媒体播放器实例。