Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

音声ファイルの音量をデバイス内で同期するように #4397

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
/>
<audio-player-volume-slider
v-model:volume="volume"
:is-mute="isMute"
:class="$style.volumeSlider"
:disabled="cantPlay"
keep-expanded
@toggle-mute="toggleMute"
/>
<audio-player-loop-button
v-model:loop="loop"
Expand Down Expand Up @@ -88,7 +90,7 @@ const {
isPlaying,
currentTime,
duration,
volume,
volume: { volume, isMute, toggleMute },
loop,
isPinPShown,
startPinP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
/>
<audio-player-volume-slider
v-model:volume="volume"
:is-mute="isMute"
:class="$style.volumeSlider"
:disabled="duration === 0"
@toggle-mute="toggleMute"
/>
</div>
</div>
Expand All @@ -52,11 +54,13 @@ const { fileMeta, fileRawPath } = useFileMeta(
reactive({ fileId: computed(() => fileId.value ?? '') })
)
const name = computed(() => fileMeta.value?.name ?? '')
const { isPlaying, currentTime, duration, volume, loop } = useAudio(
fileMeta,
fileRawPath,
audio
)
const {
isPlaying,
currentTime,
duration,
volume: { volume, toggleMute, isMute },
loop
} = useAudio(fileMeta, fileRawPath, audio)
</script>

<style lang="scss" module>
Expand Down
9 changes: 4 additions & 5 deletions src/components/UI/AudioPlayer/AudioPlayerVolumeSlider.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
:class="$style.icon"
mdi
:size="20"
:name="volume > 0 ? 'volume-high' : 'volume-off'"
@click.prevent="toggleVolume"
:name="isMute ? 'volume-off' : 'volume-high'"
@click.prevent="emit('toggleMute')"
/>
</div>
</template>
Expand All @@ -30,6 +30,7 @@ import { computed } from 'vue'
const props = withDefaults(
defineProps<{
volume: number
isMute: boolean | undefined
keepExpanded?: boolean
disabled?: boolean
}>(),
Expand All @@ -41,6 +42,7 @@ const props = withDefaults(

const emit = defineEmits<{
(e: 'update:volume', _val: number): void
(e: 'toggleMute'): void
}>()

const roundedVolume = computed({
Expand All @@ -51,9 +53,6 @@ const roundedVolume = computed({
emit('update:volume', v)
}
})
const toggleVolume = () => {
roundedVolume.value = props.volume > 0 ? 0 : 100
}
</script>

<style lang="scss" module>
Expand Down
4 changes: 3 additions & 1 deletion src/components/UI/ChromeAudio.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
/>
<audio-player-volume-slider
v-model:volume="volume"
:is-mute="isMute"
:class="$style.volumeSlider"
:disabled="duration === 0"
@toggle-mute="toggleMute"
/>
</div>
<audio-player-pin-p-button
Expand Down Expand Up @@ -61,7 +63,7 @@ const {
isPlaying,
currentTime,
duration,
volume,
volume: { volume, toggleMute, isMute },
isPinPShown,
startPinP
} = useAudio(fileMeta, fileRawPath)
Expand Down
45 changes: 39 additions & 6 deletions src/composables/media/useAudio.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import type { Ref } from 'vue'
import { ref, computed, watch, readonly, shallowRef, onUnmounted } from 'vue'
import {
ref,
computed,
watch,
readonly,
shallowRef,
onUnmounted,
onMounted
} from 'vue'
import usePictureInPicture from './usePictureInPicture'
import type { FileInfo } from '@traptitech/traq'
import { useAudioController } from '/@/store/ui/audioController'
import { useMediaSettingsStore } from '/@/store/app/mediaSettings'

const toFinite = (n: number | undefined, def: number) =>
Number.isFinite(n) ? (n as number) : def
Expand Down Expand Up @@ -135,11 +144,35 @@ export const useDuration = (audio: Ref<HTMLAudioElement | undefined>) => {
}

const useVolume = (audio: Ref<HTMLAudioElement | undefined>) => {
const nativeVolume = ref(toFinite(audio.value?.volume, 1))
const { audioVolume, restoringPromise } = useMediaSettingsStore()
const storeVolume = computed(() => audioVolume.value ?? 1)

onMounted(async () => {
await restoringPromise.value
setVolume(toFinite(storeVolume.value, 1))
})

const isMute = ref(audio.value?.muted)

const toggleMute = () => {
if (!audio.value) return
if (isMute.value === true) {
audio.value.muted = false
isMute.value = false
} else {
audio.value.muted = true
isMute.value = true
}
}

const setVolume = (v: number) => {
if (audio.value) audio.value.volume = v
audioVolume.value = v
}

const onVolumeChange = () => {
if (!audio.value) return
nativeVolume.value = toFinite(audio.value.volume, 1)
setVolume(toFinite(audio.value.volume, storeVolume.value))
}

watch(
Expand All @@ -149,7 +182,7 @@ const useVolume = (audio: Ref<HTMLAudioElement | undefined>) => {
oldAudio.removeEventListener('volumechange', onVolumeChange)
}
if (newAudio) {
nativeVolume.value = toFinite(newAudio.volume, 1)
setVolume(toFinite(storeVolume.value, 1))
newAudio.addEventListener('volumechange', onVolumeChange)
}
},
Expand All @@ -158,14 +191,14 @@ const useVolume = (audio: Ref<HTMLAudioElement | undefined>) => {

const volume = computed<number>({
get() {
return nativeVolume.value
return storeVolume.value
},
set(v) {
if (!audio.value) return
audio.value.volume = v / 100
}
})
return volume
return { volume, isMute, toggleMute }
}

const useLoop = (audio: Ref<HTMLAudioElement | undefined>) => {
Expand Down
37 changes: 37 additions & 0 deletions src/store/app/mediaSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { defineStore, acceptHMRUpdate } from 'pinia'
import { toRefs } from 'vue'
import { convertToRefsStore } from '/@/store/utils/convertToRefsStore'
import useIndexedDbValue from '/@/composables/utils/useIndexedDbValue'

export type IDBState = {
audioVolume: Readonly<number> | undefined
}

const useMediaSettingsStorePinia = defineStore('app/mediaSettings', () => {
const initialValue: IDBState = {
audioVolume: 1
}

const [state, restoring, restoringPromise] = useIndexedDbValue(
'store/app/mediaSettings',
1,
{},
initialValue
)

return {
...toRefs(state),
restoring,
restoringPromise
}
})

export const useMediaSettingsStore = convertToRefsStore(
useMediaSettingsStorePinia
)

if (import.meta.hot) {
import.meta.hot.accept(
acceptHMRUpdate(useMediaSettingsStorePinia, import.meta.hot)
)
}
Loading