Android Notification Builder:如何设置声音以便声音在looper中播放

问题描述 投票:5回答:4

我正在实施以下通知。默认警报声音正常,但只有一次。我只想重复播放,直到水龙头注册。

 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的第二个参数没有显示任何效果。请帮忙 !

android android-notifications
4个回答
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);

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发送消息。有什么方法可以防止这种情况吗?


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


-2
投票

您应该静音通知声音,只需使用媒体播放器播放应用中的声音文件。并设置媒体播放器的实例循环。

之后,您应该在通知中添加待处理的intent变量,一旦用户点击通知,相关事件就会被触发,您可以从此处停止媒体播放器实例。

© www.soinside.com 2019 - 2024. All rights reserved.