Skip to content

Commit 083e882

Browse files
skjnldsvnextcloud-command
authored andcommitted
Properly cancel and reset ongoing streams when unmounting
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com> Signed-off-by: nextcloud-command <nextcloud-command@users.noreply.github.com>
1 parent d74175e commit 083e882

6 files changed

Lines changed: 380 additions & 6 deletions

File tree

js/viewer-main.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/viewer-main.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/Audios.vue

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ export default {
6464
options() {
6565
return {
6666
autoplay: this.active === true,
67+
// Used to reset the audio streams https://github.com/sampotts/plyr#javascript-1
68+
blankVideo: '/blank.aac',
6769
controls: ['play-large', 'play', 'progress', 'current-time', 'mute', 'volume', 'settings'],
6870
loadSprite: false,
6971
}
@@ -94,6 +96,14 @@ export default {
9496
})
9597
},
9698
99+
beforeDestroy() {
100+
// Force stop any ongoing request
101+
console.debug('Closing audio stream', { filename: this.filename })
102+
this.$refs.audio.pause()
103+
this.player.stop()
104+
this.player.destroy()
105+
},
106+
97107
methods: {
98108
donePlaying() {
99109
this.$refs.audio.autoplay = false

src/components/Audios.vue.orig

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<!--
2+
- @copyright Copyright (c) 2020 Daniel Kesselberg <mail@danielkesselberg.de>
3+
-
4+
- @author Daniel Kesselberg <mail@danielkesselberg.de>
5+
-
6+
- @license GNU AGPL version 3 or any later version
7+
-
8+
- This program is free software: you can redistribute it and/or modify
9+
- it under the terms of the GNU Affero General Public License as
10+
- published by the Free Software Foundation, either version 3 of the
11+
- License, or (at your option) any later version.
12+
-
13+
- This program is distributed in the hope that it will be useful,
14+
- but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
- GNU Affero General Public License for more details.
17+
-
18+
- You should have received a copy of the GNU Affero General Public License
19+
- along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
-
21+
-->
22+
23+
<template>
24+
<!-- Plyr currently replaces the parent. Wrapping to prevent this
25+
https://github.com/redxtech/vue-plyr/issues/259 -->
26+
<div v-if="davPath">
27+
<VuePlyr
28+
ref="plyr"
29+
:options="options">
30+
<audio
31+
ref="audio"
32+
:autoplay="active"
33+
:src="davPath"
34+
preload="metadata"
35+
@ended="donePlaying"
36+
@canplay="doneLoading">
37+
38+
<!-- Omitting `type` on purpose because most of the
39+
browsers auto detect the appropriate codec.
40+
Having it set force the browser to comply to
41+
the provided mime instead of detecting a potential
42+
compatibility. -->
43+
44+
{{ t('viewer', 'Your browser does not support audio.') }}
45+
</audio>
46+
</VuePlyr>
47+
</div>
48+
</template>
49+
50+
<script>
51+
import Vue from 'vue'
52+
import VuePlyr from '@skjnldsv/vue-plyr'
53+
import '@skjnldsv/vue-plyr/dist/vue-plyr.css'
54+
import logger from '../services/logger'
55+
56+
Vue.use(VuePlyr)
57+
58+
export default {
59+
name: 'Audios',
60+
61+
computed: {
62+
player() {
63+
return this.$refs.plyr.player
64+
},
65+
options() {
66+
return {
67+
autoplay: this.active === true,
68+
// Used to reset the audio streams https://github.com/sampotts/plyr#javascript-1
69+
blankVideo: '/blank.aac',
70+
controls: ['play-large', 'play', 'progress', 'current-time', 'mute', 'volume', 'settings'],
71+
loadSprite: false,
72+
}
73+
},
74+
},
75+
76+
watch: {
77+
active(val, old) {
78+
// the item was hidden before and is now the current view
79+
if (val === true && old === false) {
80+
this.player.play()
81+
82+
// the item was playing before and is now hidden
83+
} else if (val === false && old === true) {
84+
this.player.pause()
85+
}
86+
},
87+
},
88+
89+
mounted() {
90+
// Prevent swiping to the next/previous item when scrubbing the timeline or changing volume
91+
[...this.$el.querySelectorAll('.plyr__controls__item')].forEach(control => {
92+
if (!control?.addEventListener) {
93+
return
94+
}
95+
control.addEventListener('mouseenter', this.disableSwipe)
96+
control.addEventListener('mouseleave', this.enableSwipe)
97+
})
98+
},
99+
100+
beforeDestroy() {
101+
// Force stop any ongoing request
102+
<<<<<<< HEAD
103+
logger.debug('Closing audio stream', { filename: this.filename })
104+
=======
105+
console.debug('Closing audio stream', { filename: this.filename })
106+
>>>>>>> 12b5579 (Properly cancel and reset ongoing streams when unmounting)
107+
this.$refs.audio.pause()
108+
this.player.stop()
109+
this.player.destroy()
110+
},
111+
112+
methods: {
113+
donePlaying() {
114+
this.$refs.audio.autoplay = false
115+
this.$refs.audio.load()
116+
},
117+
},
118+
}
119+
</script>
120+
121+
<style scoped lang="scss">
122+
audio {
123+
background-color: black;
124+
max-width: 100%;
125+
max-height: 100%;
126+
align-self: center;
127+
justify-self: center;
128+
/* over arrows in tiny screens */
129+
z-index: 20050;
130+
}
131+
132+
::v-deep {
133+
.plyr__progress__container {
134+
flex: 1 1;
135+
}
136+
.plyr__volume {
137+
min-width: 80px;
138+
}
139+
// plyr buttons style
140+
.plyr--audio .plyr__progress__buffer,
141+
.plyr--audio .plyr__control {
142+
&.plyr__tab-focus,
143+
&:hover,
144+
&[aria-expanded=true] {
145+
background-color: var(--color-primary-element);
146+
color: var(--color-primary-text);
147+
box-shadow: none !important;
148+
}
149+
}
150+
// plyr volume control
151+
.plyr--full-ui input[type=range] {
152+
color: var(--color-primary-element);
153+
}
154+
}
155+
</style>

src/components/Videos.vue

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ export default {
8585
options() {
8686
return {
8787
autoplay: this.active === true,
88+
// Used to reset the video streams https://github.com/sampotts/plyr#javascript-1
89+
blankVideo: '/blank.mp4',
8890
controls: ['play-large', 'play', 'progress', 'current-time', 'mute', 'volume', 'captions', 'settings', 'fullscreen'],
8991
loadSprite: false,
9092
}
@@ -115,6 +117,14 @@ export default {
115117
})
116118
},
117119
120+
beforeDestroy() {
121+
// Force stop any ongoing request
122+
console.debug('Closing video stream', { filename: this.filename })
123+
this.$refs.video.pause()
124+
this.player.stop()
125+
this.player.destroy()
126+
},
127+
118128
methods: {
119129
// Updates the dimensions of the modal
120130
updateVideoSize() {

0 commit comments

Comments
 (0)