英文:
How to setup audio session for alarm ringtones in flutter?
问题
我正在创建一个闹钟应用程序,使用just_audio来播放闹钟铃声,使用audio_session来配置音频类型。如果不进行音频配置,该应用程序将使用媒体音量进行播放。我希望应用程序告诉系统这是一个闹钟音频,因此在静音模式等情况下也会使用系统的闹钟音量进行播放。
我尝试过的方法
这是我目前的设置:
// 使用audio_session配置音频会话
final session = await AudioSession.instance;
await session.configure(const AudioSessionConfiguration(
androidAudioAttributes: AndroidAudioAttributes(
flags: AndroidAudioFlags.audibilityEnforced,
usage: AndroidAudioUsage.alarm,
),
));
// 使用just_audio初始化并播放音频
player = AudioPlayer();
await player.setAudioSource(AudioSource.uri(ringtoneUri));
await player.setLoopMode(loopMode);
player.play();
在我测试的一些设备上(Android 6和8),它使用手机的铃声音量,而在其他设备上(Android 11),它正确地使用闹钟音量。
请问正确的方法是什么,以便在所有设备上都使用闹钟音量?
英文:
I am creating an alarm clock application, using just_audio to play the alarm ringtones and audio_session to configure the audio type. Without configuring the audio, the app uses the media volume for the audio. I want the app to tell the system that this is an alarm audio, so it uses the system alarm volume for the ringtones and play it even in DnD etc.
What I have tried
Here is the setup that I currently have:
// Configuring audio session using audio_session
final session = await AudioSession.instance;
await session.configure(const AudioSessionConfiguration(
androidAudioAttributes: AndroidAudioAttributes(
flags: AndroidAudioFlags.audibilityEnforced,
usage: AndroidAudioUsage.alarm,
),
));
// Initializing and playing the audio using just_audio
player = AudioPlayer();
await player.setAudioSource(AudioSource.uri(ringtoneUri));
await player.setLoopMode(loopMode);
player.play();
On some devices that I have tested (Android 6 and 8), it uses the phone ringtone volume, while on others (Android 11), it correctly uses the alarm volume.
What is the correct method to do it, so it uses the alarm volume on all devices?
答案1
得分: 0
Turns out, it was the AndroidAudioFlags.audibilityEnforced
flag, which seems to be pretty unreliable, and can cause differing behavior on different devices. Removing it causes the volume to be controlled by alarm stream volume.
Android Docs: FLAG_AUDIBILITY_ENFORCED
Working code
final session = await AudioSession.instance;
await session.configure(const AudioSessionConfiguration(
androidAudioAttributes: AndroidAudioAttributes(
usage: AndroidAudioUsage.alarm,
),
));
// Initializing and playing the audio using just_audio
player = AudioPlayer();
await player.setAudioSource(AudioSource.uri(ringtoneUri));
await player.setLoopMode(loopMode);
player.play();
英文:
Turns out, it was the AndroidAudioFlags.audibilityEnforced
flag, which seems to be pretty unrealiable, and can cause differing behavior on different devices. Removing it causes the volume to be controlled by alarm stream volume.
Android Docs: FLAG_AUDIBILITY_ENFORCED
Working code
final session = await AudioSession.instance;
await session.configure(const AudioSessionConfiguration(
androidAudioAttributes: AndroidAudioAttributes(
usage: AndroidAudioUsage.alarm,
),
));
// Initializing and playing the audio using just_audio
player = AudioPlayer();
await player.setAudioSource(AudioSource.uri(ringtoneUri));
await player.setLoopMode(loopMode);
player.play();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论