bug: flet-audio on Android: starting one `Audio` permanently pauses any other playing `Audio`

オープン
#7,034 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
android, dart, flutter, python
領域
mobile

調査の方向性

The cause sits in the Dart side of flet-audio: AudioService in sdk/python/packages/flet-audio/src/flutter/flet_audio/lib/src/audio.dart creates one AudioPlayer per Audio and never calls setAudioContext. Reproducing needs an Android build, since the focus loss happens in audioplayers' Android code. Done would mean exposing a focus setting on the Python Audio control that is applied to its player, with the two-player case no longer pausing the music.

索引モデルが issue の本文から書いたものです。

説明

bug
Duplicate Check
Describe the bug

On Android, playing an Audio pauses every other Audio that is already playing, and the paused one never resumes. For example, looping background music stops as soon as a short sound effect plays. On desktop both play together as expected.

The cause appears to be Android audio focus in audioplayers:

  • AudioService creates one AudioPlayer per Audio and never calls setAudioContext (audio.dart), so every player uses audioplayers' default Android context with audioFocus = AUDIOFOCUS_GAIN (AudioContextAndroid.kt).
  • Each player requests focus with its own AudioFocusRequest when it starts (FocusManager.kt). Android grants focus per request, not per app, so the player that was already playing receives AUDIOFOCUS_LOSS.
  • On a permanent loss, WrappedPlayer calls pause() with the comment "Audio focus won't be recovered" (WrappedPlayer.kt).

There is currently no way to change this from Python, because Audio exposes no audio context or focus setting.

Code sample
Code
import flet as ft
import flet_audio as fta


def main(page: ft.Page):
    status = ft.Text("Music: -")

    def on_music_state(e: fta.AudioStateChangeEvent):
        status.value = f"Music: {e.state.name}"
        status.update()

    fta.Audio(
        src="https://luan.xyz/files/audio/ambient_c_motion.mp3",
        autoplay=True,
        release_mode=fta.ReleaseMode.LOOP,
        on_state_change=on_music_state,
    )

    async def play_effect(e):
        await fta.Audio(src="https://luan.xyz/files/audio/coins.wav").play()

    page.add(status, ft.Button("Play effect", on_click=play_effect))


ft.run(main)
To reproduce
  1. Build the sample with flet build apk (with flet-audio in the dependencies) and install it on an Android device.
  2. The music starts looping and the status shows Music: PLAYING.
  3. Tap "Play effect".
  4. The coin sound plays, the music stops, and the status shows Music: PAUSED. The music does not resume after the effect ends.

Running the same code on desktop keeps the music playing under the effect.

Expected behavior

The effect plays on top of the music, as it does on desktop, or at least Flet offers a way to opt into that behaviour on Android.

Screenshots / Videos
Captures

[Upload media here]

Operating System

Linux

Operating system details

Android 16

Flet version

1.0.5 (flet-audio 1.0.5)

Regression

I'm not sure / I don't know

Suggestions

Expose the audioplayers audio focus on Audio, for example a property mapped to AudioContextConfigFocus (gain, duckOthers, mixWithOthers) and applied with player.setAudioContext(...). Background music could then keep gain, so it still pauses for phone calls and other music apps, while sound effects use mixWithOthers (AndroidAudioFocus.none) and play over it without taking focus.

A global option backed by AudioPlayer.global.setAudioContext(...) would also work around the bug, but it applies to every player, including the music.

Logs
Logs
[Paste your logs here]
Additional details

The same mechanism means narration or any second Audio stops the music, and overlapping effects cut each other off. When the interrupted Audio is an intro that hands over to a loop on COMPLETED, the loop never starts because the intro never completes.

主要言語
Python
スター
17.5k
フォーク
740
平均マージ
1日 15時間
マージ済み PR(30日)
65

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

flet-dev/flet のほかの issue

flet-dev/flet の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。