-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Running code while streaming in background #419
Comments
Maybe not exactly what you want, but I'm using this to periodically check the state and progress of the player. https://github.com/ocetnik/react-native-background-timer |
That seems to be exactly what I want. |
NOTE: On android if you try to access the player after it has been destroyed, this causes some errors. I haven't worked out all the kinks on my implementation yet, but in general this has been working for me so far.
|
I am still struggling with this actually. If the app is backgrounded for a bit. Android decides to kill the player because it is idle. At this point any code that attempts to run
|
I should clarify, this is if the player has been setup, but not played anything yet, then the app is backgrounded. |
I will try getting around this by only running my background code while the player is in its playing state. |
Yes, the playback service will run as long as the player runs, it's the perfect section to add player-related code that should also run in background. I'm pretty sure react-native-background-timer will create more overhead in this case. @curiousdustin the issue happens because the app is trying to start a regular service while it's in background. Since Android 8, the app needs to start a foreground service instead of starting a regular one and then foregrounding it. The problem is that a foreground service requires a notification to be up, and the service only shows a notification after it starts playing. I can think about two fixes:
The first workaround seems like the proper behavior, as the app should never play anything without an user action in the first place. What do you think? |
In my case, I determined that I only need to run my background timer while the player is in the playing state. So I start and stop that timer/interval as needed. For now this seems to have avoided all the crashing cases I was running into. However, now you made me curious. When you say the "playback service" is the perfect place to add more code, are you saying there is a way to put more functionality into the background task by adding something to the code below? Is there a way I could create a timer as part of this?
Right now my |
You're right, you can run any type of code there. One interesting use-case is for recording analytics data. There is no event for destroying the service as of yet. Although in you can achieve your goal by registering an interval in let interval = null;
TrackPlayer.addEventListener('playback-track-changed', () => {
if (interval == null) interval = setInterval(...);
});
TrackPlayer.addEventListener('playback-queue-ended', () => {
if (interval != null) {
clearInterval(interval);
interval = null;
}
}); I haven't tested something like this, but it should work. |
React Native shares the same Javascript context for both the service and the app itself. When the app is closed and the service is destroyed, it should also destroy the context, stopping the timer. |
Perfect, I will give this a shot and see if I can do it this way instead of using a separate library. |
I did try @Guichaguri's proposal, but the |
|
@helloagain-dev Possibly updating metadata on the fly is not available yet. There's two open pull request in this regard. #384 and #384 |
Hi, I am having the same crash on Android very occasionnaly using the version
|
@helloagain-dev, Look #552 |
Closing this as the original question has been answered. Also metadata update is part of v2. |
I want to stream an icecast radio stream which works fine.
However, I also want to update the title/artist and cover regularly.
I can fetch this data from a REST API.
How can I run code that fetches this data every 10 seconds (or so) and updates the Player regularly?
Can I put a long-running task that does the fetch into
TrackPlayer.registerPlaybackService
?The text was updated successfully, but these errors were encountered: