tlenclos / react-native-audio-streaming

iOS & Android react native module to play an audio stream, with background support and media controls
MIT License
782 stars 255 forks source link

Android Background Audio #80

Open alexduckmanton opened 7 years ago

alexduckmanton commented 7 years ago

Hey there,

I've got the library up and running for Android, but I'm having some issues getting the app to continue playing audio in the background.

It will appear to be fine, but after switching between 5 or 6 other apps and leaving mine in the background, eventually the whole app closes down and the audio playback stops. When I bring my app to the foreground, it's as if I'm opening it fresh.

Is there some flag I need to pass to React Native or to the library to keep things running? I've tried playing around with START_STICKY inside onStartCommand of Signal.java, but it doesn't seem to help.

Thanks!

jerodb commented 7 years ago

Same issue here. I've been struggling with this for a couple of days but couldn't find what's causing the problem. Any progress with this @alexduckmanton ?

alexduckmanton commented 7 years ago

Yeah I made a change to the notification to make sure the process doesn't get killed. In Signal.java, instead of showing the notification using notifyManager.notify(NOTIFY_ME_ID, notifyBuilder.build());, try using startForeground(NOTIFY_ME_ID, notifyBuilder.build());.

Then you can use stopForeground(true); instead of notifyManager.cancel(NOTIFY_ME_ID); to kill the notification and tell Android it doesn't need to keep this process running. stopForeground(false); will release the foreground process without also clearing the notification.

jerodb commented 7 years ago

This solves the problem. You are my hero @alexduckmanton, thanks! I'll prepare a PR to include this change in the repo.

tlenclos commented 7 years ago

@alexduckmanton @jerodb cool job guys, I'm sorry I really neglected the maintenance of this package. Looking forward for your PR and then I will make a new release with all the other fixes and latests contributions.

cinder92 commented 6 years ago

is this issue fixed al ready?

graphee-gabriel commented 6 years ago

It does not seem to be. The audio stops when I kill the app, while it shouldn't. Also it keeps playing when I receive a phone call, which is another problem. Regarding the audio stopping, when I looked at the Java part, it seems to be related to the fact that the current library is using a Bound Service. From what I understand it should use startService at least to start the service, and could bind to it later on (or any preferred way of communicating with the service). Services that are only bound but not 'started', will stop as soon as the activity is destroyed (app killed by the user, killed by the system due to low memory after a few app switches...) Only 'started' services can run indefinitely. More info here: https://developer.android.com/guide/components/bound-services Is anyone currently working on a fix?

zerbfra commented 6 years ago

Same problem here - trying the startForeground solution. The android module should be rewritten from the ground-up... :(

graphee-gabriel commented 6 years ago

I agree. I don't have time sadly so I forked the library, fixed this issue and adapted the rest to my specific needs. Someday when I have more time I would be glad to rewrite it cause it's not actually so much hassle, but it won't be before some months that I get the chance to do it.

zerbfra commented 6 years ago

@graphee-gabriel I tried to apply your commits....

you missed: android:name="com.audioStreaming.SignalService"

in the manifest ;)

Anyway, it seems a little buggy...the stop seems not working, but I will make it work

hernanbruno97 commented 5 years ago

when i add stopForeground(boolean) appear this issue error: cannot find symbol stopForeground(true); ^ symbol: method stopForeground(boolean)