Skip to content

AudioFocus does not seem to work #36

Closed

Description

@Ro-El

Version of sounds

sounds: ^1.0.0-beta.1

flutter doctor

--

Platforms you faced the error (IOS or Android or both?)

Android (not tested on IOS)

Expected behavior

If spotify is currently playing, my expectation is that AudioFocus.focusAndHushOthers should lower spotify's volume while playing the sounds audio.

Actual behavior

AudioFocus.focusAndHushOther and AudioFocus.focusAndKeepOthers both stop spotify playback without resuming it post playback.
I have tried this with headless playback and with the shade UI.

Tested environment (Emulator? Real Device?)

Real Device (Galaxy 8+), can't imagine it'd be any different on the emulator.

Steps to reproduce the behavior

static void playSound(){
var track = Track.fromAsset('assets/sounds/english/finish.mp3');
var player = SoundPlayer.noUI();
player.onStopped = ({wasUser}) => player.release();
player.audioFocus(AudioFocus.focusAndHushOthers);
player.play(track);
}

Activity

  1. relf108 commented on Sep 1, 2020

    @relf108
    Collaborator

    The short term fix is you simply need to release the audio focus in the player.onStopped method by setting the audioFocus to abandonFocus.
    player.audioFocus(AudioFocus.abandonFocus)
    That being said I'm currently working on some changes which will allow you to automate this process. They should be available in a release soon. Ill update this thread when it's done.

  2. Ro-El commented on Sep 1, 2020

    @Ro-El
    Author

    Is something like this what you are referring to?
    Because this also does not seem to work:

    static void playSound(){
    var track = Track.fromAsset('assets/sounds/english/Finish.mp3');
    var player = SoundPlayer.noUI();
    // player.onStopped = ({wasUser}) => player.release();
    player.onStopped = ({wasUser}){
    player.audioFocus(AudioFocus.abandonFocus);
    player.release();
    };
    player.audioFocus(AudioFocus.focusAndHushOthers);
    player.play(track);
    }

    Thanks again for all the help!

  3. relf108 commented on Sep 2, 2020

    @relf108
    Collaborator

    So this is actually a bug. We're currently working through some API design issues regarding focus management as a whole but the coming changes will fix your issue. Once we get past this we'll do a release, most likely this coming Tuesday.
    From that point all the code you'll need to do what you are doing is
    var track = Track.fromAsset('assets/sample.mp3'); var player = SoundPlayer.noUI(); player.onStopped = ({wasUser}) => player.release(); player.play(track);
    Sorry for the incorrect fix I gave you and thanks for the detailed reports.

  4. relf108 commented on Sep 7, 2020

    @relf108
    Collaborator

    These changes including the fix to this bug are now available in version 1.0.0-beta13.
    The release focuses heavily on focus management, most notably implementing the autoFocus property (true by default) on SoundPlayer and SoundPlayerUI which automatically sets audio focus to hushOthersWithResume and abandons focus after the audio completes. Furthermore focus can still be managed explicitly and the focus options have been renamed to be more clear. The options are now:
    stopOthersNoResume,
    stopOthersWithResume,
    hushOthersWithResume.

    WithResume refers to whether other audio streams will return to their initial volume/state after abandon focus is called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions