guo-yu / player

a command line player, supports play mp3 both from uri and local stream.
261 stars 66 forks source link

Playback stops when window loses focus (electron / atom-shell) #57

Closed kieraneglin closed 8 years ago

kieraneglin commented 8 years ago

When using the default implementation:

var player = new Player(MUSIC_DIR + currentSong);

player.play(function(err, player){
  if (err) throw (err);
  console.log('playend!');
});

the file plays fine when window is in focus, but when focus is lost, playback stops after a few seconds.

Playback resumes immediately when the window is back in focus, or when I switch between apps.

No errors are being thrown

kieraneglin commented 8 years ago

Looks like this was an easy fix!

For anyone else with this issue, you can can either disable backgrounding (tells Chromium to not reduce performances (and save CPU cycles) when the window is in background)

Instead of doing that, you can also set the BrowserWindow visibility state to always be in the "visible" state. This saves CPU cycles, but occasionally causes freezing upon your app regaining focus

I got this solution from pracucci.com

Disable backgrounding

var app = require("app");

app.commandLine.appendSwitch("disable-renderer-backgrounding");
app.on("ready", function () {
    // ...
});

Set visible state

new BrowserWindow({
    "web-preferences": {
      "page-visibility": true
    },
    // ...
});