ltguillaume / music-folder-player

An elegant HTML5 web folder player for parties and/or private music collections, with sharing options and playlist management that's just better
https://codeberg.org/ltguillaume/music-folder-player
GNU Affero General Public License v3.0
119 stars 33 forks source link

Need help: playback stops in background/when screen off on mobile? #19

Closed ltguillaume closed 6 months ago

ltguillaume commented 5 years ago

2020/08/07

The issue: mobile browsers tend to stop playback in the following cases:

The problem is that JavaScript execution is prevented under those conditions under the guise of saving battery life.

Firefox for Android seems to be less likely to stop playback (I can play music for hours on my S5 with LineageOS, ~while trying this on Bromite will stop playback after a single song~).

There seem to be some hacks available, such as constantly playing a (fake-ish) video in the background, but they're messy. If there's a proper solution, I'd love to be pointed towards it.


@sbncbot @OoLunar @Invictaz @xRadio-us I found that for some people on mobile devices playback tends to stop a while after the screen is turned off. I'm not sure whether the cause is due to resource management of the device itself, or a crappy network connection (or whether these are two separate issues). The playback tends to stop in between songs, so it could be caused by JavaScript not being run when the screen is off.

So, playback of the next song stops when:

My questions to you:

Some links to information on a possibly related issue: https://stackoverflow.com/questions/22061280/javascript-halts-in-inactive-android-chrome-tab https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API https://stackoverflow.com/questions/15959244/is-it-possible-in-javascript-to-detect-when-the-screen-is-turned-off-in-the-an

OoLunar commented 5 years ago

On my app, it only turns off if it's on a different app, i.e the home page, or my contacts, etc. I have not tried enabling Crossfade, but I will take a look into it. The only solution I know is to turn off the screen while I'm still on my app.

EDIT: Enabling Crossfade does not help

OoLunar commented 5 years ago

Also, having a bad internet connection doesn't really seem to be an issue. I took some back roads for a trip, while choosing my songs with no reception, it kept playing (although I believe that's because it preloaded the song, and attempts to preload the next song while the current one playing. Very clever) almost all the way through

OoLunar commented 5 years ago

Service workers? https://stackoverflow.com/a/50309148

OoLunar commented 5 years ago

No sleep JavaScript? Looks more promising than it sounds. https://github.com/richtr/NoSleep.js/blob/master/README.md

ltguillaume commented 5 years ago

On my app, it only turns off if it's on a different app, i.e the home page, or my contacts, etc. I have not tried enabling Crossfade, but I will take a look into it. The only solution I know is to turn off the screen while I'm still on my app.

EDIT: Enabling Crossfade does not help

Thanks for reporting! So, to be clear, you mean it will stop playback after song X (not during song X?!) if you went to another app during playback of song X?

Also, having a bad internet connection doesn't really seem to be an issue. I took some back roads for a trip, while choosing my songs with no reception, it kept playing (although I believe that's because it preloaded the song, and attempts to preload the next song while the current one playing. Very clever) almost all the way through

Yes, you can always use the interface after it's loaded, but it will load the songs only after the previous has finished playback. When crossfade is enabled, it will start playing the next song ~10 seconds before the current has finished. As such, there is no real preloading yet, but it is planned (by using the same code as for crossfade, but by staying paused until the previous song has finished).

Service workers? https://stackoverflow.com/a/50309148

I have no experience with Service Workers yet (and in my desktop browser I have disabled them), but if the very first comment I read in your link is anything to go on, it's not really useful for this problem:

SW are grate but not perfect, it's mostly limited to push notification, network event and caching things. It's no good at staying alive and will force close after 10min or so if things are preventing it from going idle. It have no control over playing music, geo monitoring or p2p stuff :(

OoLunar commented 5 years ago

Shame SW don't work. Crossfade will just end the song normally while fading out.

ltguillaume commented 5 years ago

Shame SW don't work. Crossfade will just end the song normally while fading out.

That's so odd... If it really fades out, that means that JavaScript is still working. This could be because if audio is still being played, the browser will NOT stop JavaScript (Tabs which are playing audio are considered foreground and aren’t throttled. - source). But if that's the case, it should also load the next file AND start playback, all during playback of the last 10 seconds of the current song. If so, there will always be an audio track playing, so JavaScript can't be stopped. So why wouldn't it continue?

ltguillaume commented 5 years ago

@OoLunar Could you tell me exactly how you provoke that it stops playback? I've been trying on an old Galaxy S5 while another app was opened, the screen off (and without crossfade), but it played for several hours without a problem (then I stopped it manually). I was using https://www.bromite.org as a browser on LineageOS 16 (Android 9).

Also, were you using the latest commit? (or just tell me the date of the music.* files, that should be enough of an indication for which commit you use).

OoLunar commented 5 years ago

May 4th. As for the playback thing. Go and download my app, click a song of your choice, leave the app, and turn off the screen. By the end of the song, it stops.

OoLunar commented 5 years ago

Also, I know this is related what-so-ever to this issue, but can you help me with some Javascript related 10 line code? Send me an email at lunar@forsaken-borders.net if you want to help.

ltguillaume commented 5 years ago

Thanks for the quick reply. Hmz, I'm guessing you need the actual Google Chrome for your app: when I click "listen to music" it just opens a browser pick dialog. When I pick Bromite (which is just an "ungoogled" Chrome), it still works (but not as a Custom Chrome Tab!): I started several other apps, then turned off the screen, and still it continued playback with at least 4 songs... It might depend on how aggressively the manufacturer has set the phone to kill background apps (more info at https://dontkillmyapp.com), but I just don't know for sure... What a mess...

OoLunar commented 5 years ago

That's rough then. I haven't have any clue on how to solve this.

ltguillaume commented 5 years ago

I've installed Chrome on an old phone, let's see how it works. I did see that if I force-closed your app, playback also stopped. So perhaps you could try to exclude your app (and Chrome?) from battery optimization: https://www.makeuseof.com/tag/androids-battery-optimization-harm-good/

For now, it keeps playing on my phones.

OoLunar commented 5 years ago

Ah. I've already tried this on the Pixel 3. Doesn't seem to work. Instead I'm just am going to put the required .html, .js, and .php files into the app, basically turning it into a web app. Maybe then it'll work.

ltguillaume commented 5 years ago

I don't know, will it? If you still use Custom Chrome Tabs, it basically still loads the browser. I've recently seen what you described on others' people phones: simply going from the browser into any other (memory hungry) app will close the entire browser and thus playback. Sucks bigtime... I might be able to reduce the memory footprint by "lazy-loading" the library tree, but I doubt that will change anything.

ilPeo commented 5 years ago

I confirm having the same problem on iOS 12.4 - tested on both iPhone and iPad, Safari and Firefox. It plays just one song and then it stops. Everything works fine with any desktop browser on MacOS. I love this project but this issue prevents me from using it everyday. Keep up the good work

ltguillaume commented 5 years ago

I have no experience with iOS, but is there a way to allow "background data" for apps, and if so, is the browser allowed?

ilPeo commented 5 years ago

I don’t think so

Il giorno 29 ago 2019, alle ore 16:22, Guillaume notifications@github.com ha scritto:

I have no experience with iOS, but is there a way to allow "background data" for apps, and if so, is the browser allowed?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

ilPeo commented 5 years ago

A few searches on the Internet told me that's a Webkit/iOS problem. That sucks :(

ltguillaume commented 5 years ago

It does indeed. I'm quite annoyed by this all, since it basically makes my efforts with MFP close to useless in so many use cases. I wish I'd have known this stuff beforehand. If you come by any possible workaround, even if it requires new code, please let me know.

Apart from that, I have not updated the code on GitHub with the new beta preload routine that I currently use. I'm not sure if this will do anything for this issue on iOS, but it could. If you wanna try it out, let me know.

ilPeo commented 5 years ago

Il giorno 29 ago 2019, alle ore 20:35, Guillaume notifications@github.com ha scritto:

It does indeed. I'm quite annoyed by this all, since it basically makes my efforts with MFP close to useless in so many use cases. I wish I'd have known this stuff beforehand. If you come by any possible workaround, even if it requires new code, please let me know.

I searched a little without finding any workaround, but I don’t know how to code, sorry. The core of the issue seems to be how WebKit handles Serviceworkers.

Apart from that, I have not updated the code on GitHub with the new beta preload routine that I currently use. I'm not sure if this will do anything for this issue on iOS, but it could. If you wanna try it out, let me know

Worth trying, let me know how. — You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

ltguillaume commented 5 years ago

but I don’t know how to code, sorry

I just meant I'm willing to spend time coding a workaround, if you'd find something that requires it. :-)

Here's 3.0 beta: https://github.com/ltGuillaume/MusicFolderPlayer/tree/3.0

ilPeo commented 5 years ago

I’ll check it out in a few days and let you know. Thanks

Il giorno 30 ago 2019, alle ore 14:25, Guillaume notifications@github.com ha scritto:

but I don’t know how to code, sorry

I just meant I'm willing to spend time coding a workaround, if you'd find something that requires it. :-)

Here 3.0 beta: https://github.com/ltGuillaume/MusicFolderPlayer/tree/3.0

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

ltguillaume commented 5 years ago

FAQ Things to try, report back here afterwards.

ltguillaume commented 5 years ago

@ilPeo Found anything? (also, see FAQ)

ilPeo commented 5 years ago

I’ve been quite busy elsewhere and didn’t find anything useful to help you fix the problem

Il giorno 24 set 2019, alle ore 03:20, Guillaume notifications@github.com ha scritto:

@ilPeo Found anything? (also, see FAQ)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

Laynee commented 2 years ago

It works fine with latest Firefox Android + Video Background Play Fix Addon. Also works for YouTube playlists.

ltguillaume commented 2 years ago

It works fine with latest Firefox Android + Video Background Play Fix Addon. Also works for YouTube playlists.

Wow, thanks, I'll check it out. If it really does the trick, perhaps I can integrate whatever the add-on does into MFP.

Laynee commented 2 years ago

On older versions of Firefox Android, it even works without plugin. The last working "old" version is Firefox 68.11.0. It's an ESR release from july 2020.

ltguillaume commented 2 years ago

Yes, that corroborates my experience with Fennec (F-Droid). Indeed, the switch to the new Fenic branch introduced some issues with continued playback.

ltguillaume commented 2 years ago

@Laynee I just had a look at the code of https://github.com/mozilla/video-bg-play/blob/master/video-bg-play-content.js of the Video Background Play Fix add-on.

Laynee commented 2 years ago

You're right the plugin doesn't help. Why I though it would I don't know. Sorry for the false hopes. But I've done a few tests on Android 10 and it's encouraging. The player works great with latest Firefox and Chrome.

The connexion was so poor the player couldn't even cache 10 seconds of a song before being disconnected. So it kept disconnecting, sometimes during 2 or 3 minutes but it always managed to reconnect, continue to play the current song, go to the next one etc. Screen off the whole time. That's impressive. Under the same conditions, YouTube usually crashes under 2 minutes. And I played 2 x 7 songs.

I'll try the player on other phones / browsers.

ltguillaume commented 2 years ago

No worries, I'm just happy that you're helping out, plus I wanted to have a look-see at the inner workings of that add-on for a while now anyway.

As for my recent findings:

Shmik2003 commented 2 years ago

Maybe my message wouldn't satisfy the topic, but I met this problem and enabling Autoplay in settings of my browser helped.

I met this issue with another music server and thinked that it's problem of that server. MusicFolderPlayer gave an error that my browser blocks autoplay.

Now both are working and I think that I'll use MusicFolderPlayer.

ltguillaume commented 2 years ago

@Shmik2003 Thanks for responding! Some additional questions: