Are there any good sample projects out there with ...
# compose-android
d
Are there any good sample projects out there with a more complex setup of media3 with Compose for audio/video playing with best practices on how to share it's state across multiple screens? Or maybe someone has some suggestions about this?
2
m
I made an open source podcast listening app which might be what you're looking for here. the way I implemented this is basically I managed the playback state in a singleton AppState that lives throughout the whole app's lifecycle https://github.com/mr3y-the-programmer/Podcaster/blob/ef9bdc5f74de8f24cd38f97fff35[…]ain/kotlin/com/mr3y/podcaster/ui/presenter/PodcasterAppState.kt and when you put the app in the background the audio continues to play smoothly through Media3
MediaSessionService
https://github.com/mr3y-the-programmer/Podcaster/blob/ef9bdc5f74de8f24cd38f97fff35[…]p/src/main/kotlin/com/mr3y/podcaster/service/PlaybackService.kt BTW, the app is on google play so you play with it to see if it fits your needs or take a look at the code directly https://github.com/mr3y-the-programmer/Podcaster
d
Very nice app, thanks 👍🏼! I guess my only problem as that it seems to use the podcastRepository to get the current list in the code you posted, I guess adding other audio (or video) to the player from other views in the app, expanding it, managing next up list in the big player, are probably elsewhere. I'll try cloning the repo and playing around with it...
m
Yeah, you can use it as an inspiration. see what works for you and make any changes based on your requirements.
d
Well, I need an API_KEY/SECRET in local.properties to compile... where do I get them?
podcast index: I'm sorry. Due to high volume API scraping, we are not currently allowing signups from free email providers such as Gmail, Outlook, etc.
maybe our business account in gmail, yeah that works
m
If your intent is to play with & compile the project and you don't need Real network API you can comment the part that instructs you to add API_KEY or SECRET
d
Does it have sample data then?
m
or if you can provide the credentials then go ahead, it is really up to you
yes it has some sample data
d
Well, I did get an api key... but there's only one key, (no key/secret pair, just one single key...)
The key and secret are the same?
No they are different things
d
Ok, I missed the dialog that shows the secret... now I got both, thanks!
m
glad you solved it
d
The code is very clean and understandable -- this is really a great example! I just hope I'll be able to get some sanity back into the app I'm working on based on this... (it's been developed by a few people, and landed in my hands as a complete mess...), thanks again!
👌 1
@MR3Y I've been looking through the source code, but I have a bit of trouble understanding how the currentlyPlayed entry gets updated when, say, the episode is changed in the notification's player... I'm having exactly that problem in our project, whereas in your sample it works perfectly...
We're not using a database to save the currently played entry... but the viewmodel for the player should be saving the list...
m
d
Yeah, I just saw that.. in my case, I'd have to either start using a database, or try sharing a mutable state flow from the di for the service to update it, and the ui to receive the changes... 😵‍💫... also, I noticed you update it in the appstate play function... I'm wondering if the listener wouldn't be enough here...?
m
the call from
play
function is for a different purpose, it is called when you explicitly click on a different episode's play button from the app's UI. honestly, I haven't tried removing the call from appstate
play
and see if it will work with no issues but as per my understanding I thought that
onMediaItemTransition
will be triggered when transitioning between the end of one episode and the beginning of a new one.
So, it might work without it but haven't tested that.
Also, keep in mind the timing of when
onMediaItemTransition
gets called, there might be delay that will in turn cause the app's UI update to be delayed which the user might perceive as "unresponsiveness" or lag
d
Oh, thanks! I'll test things out a bit to check this.
Another little question... doesn't the player itself keep some kind of playing queue? Maybe it can be queried? Or do I need to manage it myself, and synchronise with my calls to add/replaceMediaItems with their index against my own list...?
In your case, you need to manage this to persist the play list in the database for the next app restart, but in my case, I just really need the list while the app is running.
m
Of course the player has its own queue see Media3 Playlists documentation but I'm using a database to save more metadata about each podcast & episode which isn't possible and doesn't make any sense to save them in the player's queue/playlist
👍🏼 1
if you only need in-memory data and don't need to save any extra info about each episode(whether it has been played before or not?, the podcast Id it belongs to?...etc), then it is fine