Simplify play/pause handling (#410)
I found it awkward to listen to the video tags 'playing' and 'paused' events, when we're controlling the state that defines what state it's in in the first place. This commit removes those listeners, in favor of just setting things to the right state immediately when play(), pause(), or playPause() is called. Added play(), pause() methods for clarity.
This commit is contained in:
@@ -254,10 +254,7 @@ function dispatch (action, ...args) {
|
||||
},
|
||||
onbeforeunload: closePlayer
|
||||
})
|
||||
playPause(false)
|
||||
}
|
||||
if (action === 'pause') {
|
||||
playPause(true)
|
||||
play()
|
||||
}
|
||||
if (action === 'playbackJump') {
|
||||
jumpToTime(args[0] /* seconds */)
|
||||
@@ -271,17 +268,6 @@ function dispatch (action, ...args) {
|
||||
if (action === 'openSubtitles') {
|
||||
openSubtitles()
|
||||
}
|
||||
if (action === 'mediaPlaying') {
|
||||
state.playing.isPaused = false
|
||||
ipcRenderer.send('blockPowerSave')
|
||||
}
|
||||
if (action === 'mediaPaused') {
|
||||
// When removing the <video>/<audio> tag to switch to Chromecast, it sends
|
||||
// a false 'paused' event. Ignore that:
|
||||
if (state.playing.location !== 'local') return
|
||||
state.playing.isPaused = true
|
||||
ipcRenderer.send('unblockPowerSave')
|
||||
}
|
||||
if (action === 'mediaStalled') {
|
||||
state.playing.isStalled = true
|
||||
}
|
||||
@@ -330,16 +316,30 @@ function updateAvailable (version) {
|
||||
state.modal = { id: 'update-available-modal', version: version }
|
||||
}
|
||||
|
||||
// Plays or pauses the video. If isPaused is undefined, acts as a toggle
|
||||
function playPause (isPaused) {
|
||||
if (isPaused === state.playing.isPaused) {
|
||||
return // Nothing to do
|
||||
}
|
||||
// Either isPaused is undefined, or it's the opposite of the current state. Toggle.
|
||||
function play () {
|
||||
if (!state.playing.isPaused) return
|
||||
state.playing.isPaused = false
|
||||
if (isCasting()) {
|
||||
Cast.playPause()
|
||||
Cast.play()
|
||||
}
|
||||
ipcRenderer.send('blockPowerSave')
|
||||
}
|
||||
|
||||
function pause () {
|
||||
if (state.playing.isPaused) return
|
||||
state.playing.isPaused = true
|
||||
if (isCasting()) {
|
||||
Cast.pause()
|
||||
}
|
||||
ipcRenderer.send('unblockPowerSave')
|
||||
}
|
||||
|
||||
function playPause () {
|
||||
if (state.playing.isPaused) {
|
||||
play()
|
||||
} else {
|
||||
pause()
|
||||
}
|
||||
state.playing.isPaused = !state.playing.isPaused
|
||||
}
|
||||
|
||||
function jumpToTime (time) {
|
||||
|
||||
@@ -5,7 +5,8 @@ module.exports = {
|
||||
init,
|
||||
open,
|
||||
close,
|
||||
playPause,
|
||||
play,
|
||||
pause,
|
||||
seek,
|
||||
setVolume
|
||||
}
|
||||
@@ -81,9 +82,12 @@ function chromecastPlayer (player) {
|
||||
})
|
||||
}
|
||||
|
||||
function playPause (callback) {
|
||||
if (!state.playing.isPaused) player.pause(callback)
|
||||
else player.play(null, null, callback)
|
||||
function play (callback) {
|
||||
player.play(null, null, callback)
|
||||
}
|
||||
|
||||
function pause (callback) {
|
||||
player.pause(callback)
|
||||
}
|
||||
|
||||
function stop (callback) {
|
||||
@@ -113,7 +117,8 @@ function chromecastPlayer (player) {
|
||||
return {
|
||||
player: player,
|
||||
open: open,
|
||||
playPause: playPause,
|
||||
play: play,
|
||||
pause: pause,
|
||||
stop: stop,
|
||||
status: status,
|
||||
seek: seek,
|
||||
@@ -138,9 +143,12 @@ function airplayPlayer (player) {
|
||||
})
|
||||
}
|
||||
|
||||
function playPause (callback) {
|
||||
if (!state.playing.isPaused) player.rate(0, callback)
|
||||
else player.rate(1, callback)
|
||||
function play (callback) {
|
||||
player.rate(1, callback)
|
||||
}
|
||||
|
||||
function pause (callback) {
|
||||
player.rate(0, callback)
|
||||
}
|
||||
|
||||
function stop (callback) {
|
||||
@@ -172,7 +180,8 @@ function airplayPlayer (player) {
|
||||
return {
|
||||
player: player,
|
||||
open: open,
|
||||
playPause: playPause,
|
||||
play: play,
|
||||
pause: pause,
|
||||
stop: stop,
|
||||
status: status,
|
||||
seek: seek,
|
||||
@@ -217,9 +226,12 @@ function dlnaPlayer (player) {
|
||||
})
|
||||
}
|
||||
|
||||
function playPause (callback) {
|
||||
if (!state.playing.isPaused) player.pause(callback)
|
||||
else player.play(null, null, callback)
|
||||
function play (callback) {
|
||||
player.play(null, null, callback)
|
||||
}
|
||||
|
||||
function pause (callback) {
|
||||
player.pause(callback)
|
||||
}
|
||||
|
||||
function stop (callback) {
|
||||
@@ -253,7 +265,8 @@ function dlnaPlayer (player) {
|
||||
return {
|
||||
player: player,
|
||||
open: open,
|
||||
playPause: playPause,
|
||||
play: play,
|
||||
pause: pause,
|
||||
stop: stop,
|
||||
status: status,
|
||||
seek: seek,
|
||||
@@ -317,10 +330,17 @@ function getDevice (location) {
|
||||
}
|
||||
}
|
||||
|
||||
function playPause () {
|
||||
function play () {
|
||||
var device = getDevice()
|
||||
if (device) {
|
||||
device.playPause(castCallback)
|
||||
device.play(castCallback)
|
||||
}
|
||||
}
|
||||
|
||||
function pause () {
|
||||
var device = getDevice()
|
||||
if (device) {
|
||||
device.pause(castCallback)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -78,8 +78,6 @@ function renderMedia (state) {
|
||||
ondblclick=${dispatcher('toggleFullScreen')}
|
||||
onloadedmetadata=${onLoadedMetadata}
|
||||
onended=${onEnded}
|
||||
onplay=${dispatcher('mediaPlaying')}
|
||||
onpause=${dispatcher('mediaPaused')}
|
||||
onstalling=${dispatcher('mediaStalled')}
|
||||
onerror=${dispatcher('mediaError')}
|
||||
ontimeupdate=${dispatcher('mediaTimeUpdate')}
|
||||
|
||||
Reference in New Issue
Block a user