Currently I create my exoplayer player object in my viewmodel using the builder class. Is this the correct way, or should I instead create a single instance of the player and use dependency injection to inject it where i need. I've seen other apps such as newpipe use a single instance.
CodePudding user response:
Yes, you should create a single instance as media players can consume a lot of resources. And yes, dependency injection is a good idea, specially if you have in mind testing. As for where it should be initialized, it should be in a foreground service. Implement media players in service instance so it can keep playing in background when the user leaves the app. Otherwise It gets killed by the system. If that is your goal, then use a service.
There is a handy guide that explains you what arquitecture to follow when implementing this kind of app. Audio app overview