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

Aperta
#7,034 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
android, dart, flutter, python
Ambito
mobile

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug platform: android
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.

Lingua principale
Python
Stelle
17.5k
Fork
740
Merge medio
1g 15h
PR unite (30g)
73

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di flet-dev/flet

Tutte le issue di flet-dev/flet

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.