florent37 / Flutter-AssetsAudioPlayer

Play simultaneously music/audio from assets/network/file directly from Flutter, compatible with android / ios / web / macos, displays notifications
https://pub.dartlang.org/packages/assets_audio_player
Apache License 2.0
740 stars 337 forks source link

Music player paused accidentally when the app is launching #112

Closed AlexV525 closed 4 years ago

AlexV525 commented 4 years ago

Flutter Version

My version : 1.17.1

Lib Version

My version : 1.6.0+4

Platform (Android / iOS / web) + version

Platform : Android (iOS not tested) Device Model: OnePlus 8 Pro (IN2020) / OnePlus 6T (A6010)

Describe the bug

When a flutter app implemented this package (e.g. just import it), start the app will pause the current playing audio (Music that was playing).

Step to reproduce

  1. Add assets_audio_player: ^1.6.0+4 into pubspec.yaml.
  2. Play a song with some music player.
  3. Run the app.
  4. When the app start, the song was paused.
florent37 commented 4 years ago

when you tell current playing audio, it's for example "youtube music player" ?

AlexV525 commented 4 years ago

Yes, just like the one in the screenshot. Screenshot_20200526-003123__01

florent37 commented 4 years ago

Ok thanks that's my next bug to fix !

florent37 commented 4 years ago

found, it's because of the requestAudioFocus, used to pause the media player on phone call

florent37 commented 4 years ago

maybe I can make this optional, like "requestAudioFocus: true" on open method (by default), which enable to listen audio focus & stop on phone call

setting it to false will solve your issue, but recieving a phone call will not pause the audio !

florent37 commented 4 years ago

or maybe migrate to https://stackoverflow.com/questions/5948961/how-to-know-whether-i-am-in-a-call-on-android, to only pause on phone call :) I'll look for this

florent37 commented 4 years ago

this solution requires runtime permission :/ it's not a good solution for the user

AlexV525 commented 4 years ago

found, it's because of the requestAudioFocus, used to pause the media player on phone call

This sounds good. How if we can requestAudioFocus only when we start the first play?

florent37 commented 4 years ago

If we do this, it will stop the other media player at the start, like your qq music player

I think you'll need to make a choice,

  1. make this media player require focus (pause others players, intercept phone call state)

  2. Don't mind if audio focus : don't pause other players, but don't know if the phone is receiving a call

AlexV525 commented 4 years ago
  1. make this media player require focus (pause others players, intercept phone call state)

Am I missing something? I mean pause other players when the app which calling this package to play an audio. This behaviour is fine because it calls by user's request.

e.g. I'm playing some music with QQMusic, then I start my flutter app, choose an audio and try to play it, at that time it should stop my music and play the audio I requested.

florent37 commented 4 years ago

Oh ok I see, I just have to move the request audio, sorry ^^ I'll do that today

florent37 commented 4 years ago

I fixed this, can you try the ^1.6.2+5 ?

florent37 commented 4 years ago

I close it but if this does not work, reopen it please :)

AlexV525 commented 4 years ago

Thanks for your rapid response! And it's been resolved through I tested with my devices.

vinh-savvycom commented 3 years ago

Hi @AlexV525 , current I'm using version assets_audio_player: ^2.0.13+8. But when I play a AssetsAudioPlayerGroup. Sound play normally but when I go to background and open app again. Sound auto stopped. I check log when I open app again see that: D/AudioManager(16302): dispatching onAudioFocusChange(-1) to android.media.AudioManager@f3d4723com.github.florent37.assets_audio_player.stopwhencall.StopWhenCallAudioFocus$sam$i$android_media_AudioManager_OnAudioFocusChangeListener$0@682fee7

Please help me check it. Thanks

kalismeras61 commented 3 years ago

@vinh-savvycom can you paste your open method here ?

vinh-savvycom commented 3 years ago

@kalismeras61 here is code init AssetsAudioPlayerGroup

AudioPlayer() {
    playerGroup = AssetsAudioPlayerGroup(
      updateNotification: (player, audios) async {
        return PlayerGroupMetas(
          title: currentSong.title,
          subTitle: currentSong.categoryName,
          image: MetasImage.network(
              getUrlFromServer(currentSong.thumb.path, currentSong.thumb.uuid)),
        );
      },
      respectSilentMode: true,
      showNotification: true,
      notificationStopEnabled: false,
      onNotificationPause: (player, audios) {
        togglePlay();
      },
      onNotificationPlay: (player, audios) {
        togglePlay();
      },
    );
  }

and here sample code add audio and play

Audio audio = Audio.network(
            getMediaURLFromServer(soundData.path, soundData.uuid),
            metas: Metas(
              title: song.title,
              album: song.categoryName,
              image: MetasImage.network(
                getUrlFromServer(song.thumb.path, song.thumb.uuid),
              ),
            ),
          );
          playerGroup.add(
            audio,
            volume: soundData.fileVolume / 100.0,
            loopMode: LoopMode.single,
          );

I used this command for play sound

playerGroup.playOrPause();

I have issue on Android real device and debug mode.

Thanks