如何在Flutter中为警报铃声设置音频会话?

huangapple go评论65阅读模式
英文:

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();

huangapple
  • 本文由 发表于 2023年2月6日 12:35:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定