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
|
onbeforeunload: closePlayer
|
||||||
})
|
})
|
||||||
playPause(false)
|
play()
|
||||||
}
|
|
||||||
if (action === 'pause') {
|
|
||||||
playPause(true)
|
|
||||||
}
|
}
|
||||||
if (action === 'playbackJump') {
|
if (action === 'playbackJump') {
|
||||||
jumpToTime(args[0] /* seconds */)
|
jumpToTime(args[0] /* seconds */)
|
||||||
@@ -271,17 +268,6 @@ function dispatch (action, ...args) {
|
|||||||
if (action === 'openSubtitles') {
|
if (action === 'openSubtitles') {
|
||||||
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') {
|
if (action === 'mediaStalled') {
|
||||||
state.playing.isStalled = true
|
state.playing.isStalled = true
|
||||||
}
|
}
|
||||||
@@ -330,16 +316,30 @@ function updateAvailable (version) {
|
|||||||
state.modal = { id: 'update-available-modal', version: version }
|
state.modal = { id: 'update-available-modal', version: version }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Plays or pauses the video. If isPaused is undefined, acts as a toggle
|
function play () {
|
||||||
function playPause (isPaused) {
|
if (!state.playing.isPaused) return
|
||||||
if (isPaused === state.playing.isPaused) {
|
state.playing.isPaused = false
|
||||||
return // Nothing to do
|
|
||||||
}
|
|
||||||
// Either isPaused is undefined, or it's the opposite of the current state. Toggle.
|
|
||||||
if (isCasting()) {
|
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) {
|
function jumpToTime (time) {
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ module.exports = {
|
|||||||
init,
|
init,
|
||||||
open,
|
open,
|
||||||
close,
|
close,
|
||||||
playPause,
|
play,
|
||||||
|
pause,
|
||||||
seek,
|
seek,
|
||||||
setVolume
|
setVolume
|
||||||
}
|
}
|
||||||
@@ -81,9 +82,12 @@ function chromecastPlayer (player) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function playPause (callback) {
|
function play (callback) {
|
||||||
if (!state.playing.isPaused) player.pause(callback)
|
player.play(null, null, callback)
|
||||||
else player.play(null, null, callback)
|
}
|
||||||
|
|
||||||
|
function pause (callback) {
|
||||||
|
player.pause(callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
function stop (callback) {
|
function stop (callback) {
|
||||||
@@ -113,7 +117,8 @@ function chromecastPlayer (player) {
|
|||||||
return {
|
return {
|
||||||
player: player,
|
player: player,
|
||||||
open: open,
|
open: open,
|
||||||
playPause: playPause,
|
play: play,
|
||||||
|
pause: pause,
|
||||||
stop: stop,
|
stop: stop,
|
||||||
status: status,
|
status: status,
|
||||||
seek: seek,
|
seek: seek,
|
||||||
@@ -138,9 +143,12 @@ function airplayPlayer (player) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function playPause (callback) {
|
function play (callback) {
|
||||||
if (!state.playing.isPaused) player.rate(0, callback)
|
player.rate(1, callback)
|
||||||
else player.rate(1, callback)
|
}
|
||||||
|
|
||||||
|
function pause (callback) {
|
||||||
|
player.rate(0, callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
function stop (callback) {
|
function stop (callback) {
|
||||||
@@ -172,7 +180,8 @@ function airplayPlayer (player) {
|
|||||||
return {
|
return {
|
||||||
player: player,
|
player: player,
|
||||||
open: open,
|
open: open,
|
||||||
playPause: playPause,
|
play: play,
|
||||||
|
pause: pause,
|
||||||
stop: stop,
|
stop: stop,
|
||||||
status: status,
|
status: status,
|
||||||
seek: seek,
|
seek: seek,
|
||||||
@@ -217,9 +226,12 @@ function dlnaPlayer (player) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function playPause (callback) {
|
function play (callback) {
|
||||||
if (!state.playing.isPaused) player.pause(callback)
|
player.play(null, null, callback)
|
||||||
else player.play(null, null, callback)
|
}
|
||||||
|
|
||||||
|
function pause (callback) {
|
||||||
|
player.pause(callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
function stop (callback) {
|
function stop (callback) {
|
||||||
@@ -253,7 +265,8 @@ function dlnaPlayer (player) {
|
|||||||
return {
|
return {
|
||||||
player: player,
|
player: player,
|
||||||
open: open,
|
open: open,
|
||||||
playPause: playPause,
|
play: play,
|
||||||
|
pause: pause,
|
||||||
stop: stop,
|
stop: stop,
|
||||||
status: status,
|
status: status,
|
||||||
seek: seek,
|
seek: seek,
|
||||||
@@ -317,10 +330,17 @@ function getDevice (location) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function playPause () {
|
function play () {
|
||||||
var device = getDevice()
|
var device = getDevice()
|
||||||
if (device) {
|
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')}
|
ondblclick=${dispatcher('toggleFullScreen')}
|
||||||
onloadedmetadata=${onLoadedMetadata}
|
onloadedmetadata=${onLoadedMetadata}
|
||||||
onended=${onEnded}
|
onended=${onEnded}
|
||||||
onplay=${dispatcher('mediaPlaying')}
|
|
||||||
onpause=${dispatcher('mediaPaused')}
|
|
||||||
onstalling=${dispatcher('mediaStalled')}
|
onstalling=${dispatcher('mediaStalled')}
|
||||||
onerror=${dispatcher('mediaError')}
|
onerror=${dispatcher('mediaError')}
|
||||||
ontimeupdate=${dispatcher('mediaTimeUpdate')}
|
ontimeupdate=${dispatcher('mediaTimeUpdate')}
|
||||||
|
|||||||
Reference in New Issue
Block a user