ryanheise / audio_service

Flutter plugin to play audio in the background while the screen is off.
806 stars 480 forks source link

Trouble closing audio session #1058

Closed appcapsergen closed 9 months ago

appcapsergen commented 9 months ago

Documented behaviour

Could not find what it's about, it doesn't give an error, the behavior I want just doesn't happen.

Actual behaviour

In my Flutter app the user is able to play a video, I use the chewie package for this. This player uses a VideoPlayerController (from the video_player package) to somewhat control the video player, which ultimately allows me to use the audio_service package to create an audio session. I use this to continue playing the video when the app is paused (in the background or when the phone is locked).

The problem I'm faced with now is that when I've started a video with background audio playback successfully, but then want to close that video, I can't seem to get the audio session to stop as well. This is especially visible on iOS (like in this screenshot), where the media player stays active even though I shut down the video and attempted to shut down the audio session as well.

Runtime error

There's no error.

Minimal reproduction project

https://github.com/appcapsergen/audio_service

Reproduction steps

  1. Use the audio handler I provided.
  2. Start a video, at least with a videoController, with an audio session using the audio handler.
  3. Make sure the media controls appear in the notifications centre.
  4. Close the video by closing the video or closing the entire screen (if applicable).
  5. You can still see the media controls in the notifications centre.

    Output of flutter doctor

    
    Doctor summary (to see all details, run flutter doctor -v):
    [✓] Flutter (Channel stable, 3.16.9, on macOS 14.3 23D56 darwin-arm64, locale en-NL)
    [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    [✓] Xcode - develop for iOS and macOS (Xcode 15.2)
    [✓] Chrome - develop for the web
    [✓] Android Studio (version 2023.1)
    [✓] VS Code (version 1.85.2)
    [✓] Connected device (3 available)
    [✓] Network resources

• No issues found!


### Devices exhibiting the bug
At least on iOS 16.7.5, pretty sure it's on any iOS version though.
ryanheise commented 9 months ago

I can't seem to get the audio session to stop as well

Do you mean the audio service? The audio session is handled by a separate package.

This is especially visible on iOS (like in this screenshot), where the media player stays active even though I shut down the video and attempted to shut down the audio session as well.

I don't see any provided screenshot, and I am also not sure what you mean by "the media player". Do you mean chewie? Or do you possibly mean the player in the notification or control center?

If you're talking about the notification or control center, can you confirm whether this issue only happens when using chewie or video_player, and does not happen with the official example/s in this repo?

ryanheise commented 9 months ago

I just checked through the source code of video_player and it doesn't seem to contain any code that conflicts with my own code for the notification / control center, but it does have code that could potentially conflict with the audio session. So I think I need you to clarify if you really mean the issue is on the audio session side of things.

appcapsergen commented 9 months ago

@ryanheise My apologies, here's the screenshot I do mean the media controls in the notifications enter of an iOS device, yes!

I will test it out now with your example, but I do assume it is the audio session side of things simply because I am able to stop the video, just not the audio (session) of the video that I linked together with this package. (Both chewie and this package can use a video_player for playing of a video or audio respectively)

appcapsergen commented 9 months ago

I found the issue and it was honestly pretty stupid (of me) :P Basically, all I needed to do was call await super.stop(); in the overriden stop method in my custom audio handler... That solved it 🤦🏼‍♂️

github-actions[bot] commented 9 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs, or use StackOverflow if you need help with audio_service.