如何使用带有setAudioAttributes的Alarm音量播放铃声? [英] How to play a ringtone using the Alarm volume with setAudioAttributes?

查看:306
本文介绍了如何使用带有setAudioAttributes的Alarm音量播放铃声?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图围绕音频属性.这是我到目前为止的内容:

So I'm trying to wrap my head around Audio Attributes. Here's what I have so far:

// alarm.getSound() will return a proper URI to pick a ringtone
Ringtone tone = RingtoneManager.getRingtone(this, alarm.getSound());
if (Build.VERSION.SDK_INT >= 21) {
    AudioAttributes aa = new AudioAttributes.Builder()
        .setFlags(AudioAttributes.USAGE_ALARM | AudioAttributes.CONTENT_TYPE_SONIFICATION)
        .build();
    tone.setAudioAttributes(aa);
} else {
    tone.setStreamType(RingtoneManager.TYPE_ALARM);
}
tone.play();

此页面讨论音频属性及其兼容性映射".如果我以前使用的是setStreamType(TYPE_ALARM)(就像上面的我一样),那么它将设置CONTENT_TYPE_SONIFICATIONUSAGE_ALARM标志.我想摆脱setStreamType,所以我在想,如果手动设置这些标志(如我上面的标志),那么当铃声播放时,它将使用警报音量.好吧,它似乎不是那样工作的.

This page talks about Audio Attributes and their "Compatibility Mappings." If I was previously using setStreamType(TYPE_ALARM) (like I am above) then it will set the CONTENT_TYPE_SONIFICATION and USAGE_ALARM flags. I want to get away from setStreamType so I was thinking that if I manually set those flags (like I am above) then when the ringtone plays it will use the Alarm volume. Well, it doesn't seem to work like that.

上面的代码仍然使用我的Nexus 6的Media音量而不是Alarm音量响起.我的MRA68N版本为6.0.使用闹钟音量有什么不同?

The above code still rings using my Nexus 6's Media volume instead of the Alarm volume. I'm on 6.0 with build MRA68N. What could I do different to use the Alarm volume?

推荐答案

在Moto G Android 6.0上测试

Tested on Moto G Android 6.0

       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            AudioAttributes aa = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_ALARM)
                    .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                    .build();
            ringtone.setAudioAttributes(aa);
        } else {
            ringtone.setStreamType(AudioManager.STREAM_ALARM);
        }

这篇关于如何使用带有setAudioAttributes的Alarm音量播放铃声?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆