ryanheise / audio_service

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

Currently Playing Info #451

Open shinyford opened 4 years ago

shinyford commented 4 years ago

Is your feature request related to a problem? Please describe. I'm looking for a way of monitoring what is currently being played on a device, or on connected BlueTooth speakers, whether or not that media comes from the current app. Artist, Track, Album Art, that sort of thing - much like a connected player such as Alexa or a car are able to monitor and display that information, but via a different app on the device playing the music.

Ideally some kind of rudimentary control would be available too - play, pause, next track - but that's a nice-to-have.

Describe the solution you'd like A listenable stream of MediaItems which updates whenever the device starts, pauses, stops playing, regardless of source app. Ideally to contain information such as, if available, Artist, Track, Album title, Art; and also current playing state (playing, paused, stopped etc.)

Would only need to be a foreground operation; but would want to pick up/update what's currently playing when the app starts or foregrounds.

Describe alternatives you've considered Apparently it's possible to listen to system-wide notifications, which could reveal this information. However, that's is probably Android only, so not the best solution.

Additional context The use-case is a treadmill running app: shows basic data such as distance covered, progress made etc. I want to be able to play music via backgorunded Spotify or an on-app music player, or Audible or BigFinish talking books, while the treadmill app is foregrounded, and display basic information about what's currently playing to the user. (Don't want them to have to switch foreground apps to see what track's just come on.)

shinyford commented 4 years ago

Happy to pick up the dev work on this, BTW; but some discussion around the best way to achieve it would be appreciated.

ryanheise commented 4 years ago

Thanks for offering to contribute to the project!

I am not 100% sure whether this is possible, but at least on Android if you read the documentation for the MediaSession class, it claims that one process can connect to the media session of another process. This could be a good starting point. Currently the Android implementation of "connect" always connects to the media session within the same process. Note that there may be multiple media sessions from different apps/processes, but there should be only one active media session.

shinyford commented 4 years ago

Okay, I'll start there. Many thanks.

ryanheise commented 4 years ago

Note also that in a recent change, I reimplemented the "play", "pause" and pretty much all of the other cases to communicate directly with the background isolate rather than go through the media session. There were reasons for this, but if you want these controls to work with another media session outside of the same process, you will need to bring back the old approach that went through the transport controls, but you should use an if/else so that if it's within the same process, do it the way I'm currently doing it, and if it's to a separate media session outside the process, use the transport controls.

shinyford commented 4 years ago

Ah! Sadly, looks like this won't work as it stands: to use the Android MediaSessionManager requires the MEDIA_CONTENT_CONTROL permission - but that's only available to system or vendor apps. Us mere mortals can't use it.

Oh well. Back to the drawing board.

hacker1024 commented 4 years ago

You might be able to use the notification instead on Android. I've seen apps do it that way before.

shinyford commented 4 years ago

Yes - I've been investigating that, with quite a lot of success. Thanks for the comment.

shinyford commented 4 years ago

So, I eventually decided that the functionality I'm looking for here is quite a long way away from audio_service, so put my effort into creating a separate package instead: https://pub.dev/packages/nowplaying. If you feel it has a place in audio_service after all, you're very welcome to cannibalise that for anything useful.

Thanks @ryanheise for the conversation and inspiration. Helped a great deal.

ryanheise commented 4 years ago

For audio_service I would probably still prefer to go in the direction of using a MediaSession-based solution (i.e. MediaSessionManager) since this plugin intends to expose the media session API.

That said, congratulations! Your plugin looks really useful.