安卓通知生成器:如何设置声音以使声音在循环器中播放

9
我正在实现以下通知功能。默认的警报声音正常播放,但仅播放一次。我想要的是在注册点击之前反复播放它。
 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的第二个参数没有产生任何效果。请帮忙!
4个回答

24

你需要在通知中使用FLAG_INSISTENT。根据文档

public static final int FLAG_INSISTENT

如果设置了此标志,音频将重复播放直到通知被取消或通知窗口被打开时,该位将被按位或到flags字段中。

常量值: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
FLAG_INSISTENT在Oreo以上版本中无法正常工作。您能否提供一些建议? - Pooja Shukla
1
@PoojaShukla 我已经检查了 Android 10 上的标志,它可以正常工作。 - Vikrant_Dev
@代码在Android 12上进行了检查,第一次运行时声音重复,但完美工作。但是从第二次开始,有时声音不会播放。 - HuyLe

1
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(); 停止声音。


如果用户直接通过向左或向右滑动从通知托盘中删除通知,那么就不会有通知,但声音将持续播放。 - Jigar
你可以通过应用程序停止它。我们使用重复的媒体播放器来手动停止它。在你的情况下,使用Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); - Kapil Parmar

0

我已经在SendNotification方法体中调用了这个。

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:

当 onDestroy 立即被调用时,Handler android.media.MediaPlayer$EventHandler 发送消息到一个死线程上的 Handler。有没有什么方法可以防止这种情况发生?


-2

您应该将通知声音静音,并使用媒体播放器从您的应用程序播放声音文件。并将媒体播放器实例设置为循环。

然后,您应该在通知中添加一个待处理意图变量,一旦用户点击通知,相关事件就会触发,您可以从这里停止媒体播放器实例。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接