且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Android 上以静音模式播放声音的问题

更新时间:2022-03-09 00:55:17

自己回答了问题,花更多时间深入阅读文档.

Answered the question myself, spend more time reading the documentation in-depth.

Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    if (alert == null){
        // alert is null, using backup
        alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        if (alert == null){
            // alert backup is null, using 2nd backup
            alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
    }
}
ringtone = RingtoneManager.getRingtone(getApplicationContext(), alert);

设置铃声后,我必须设置铃声的流类型:

after setting the ringtone I had to set the stream type for the ringtone:

    ringtone.setStreamType(AudioManager.STREAM_ALARM);