Skip to content

Commit

Permalink
[web] Fix volume for stream not working in Safari
Browse files Browse the repository at this point in the history
  • Loading branch information
hacketiwack committed Dec 6, 2023
1 parent 85e9b06 commit 39847ba
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions web-src/src/audio.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
/**
* Audio handler object
* Taken from https://github.com/rainner/soma-fm-player (released under MIT licence)
* Inspired by https://github.com/rainner/soma-fm-player
* (released under MIT licence)
*/
export default {
_audio: new Audio(),
_context: null,
_source: null,
_gain: null,

// Setup audio routing
setupAudio() {
const AudioContext = window.AudioContext || window.webkitAudioContext
this._context = new AudioContext()
this._source = this._context.createMediaElementSource(this._audio)
this._gain = this._context.createGain()

this._source.connect(this._gain)
this._gain.connect(this._context.destination)

this._context = new (window.AudioContext || window.webkitAudioContext)()
const source = this._context.createMediaElementSource(this._audio)
source.connect(this._context.destination)
this._audio.addEventListener('canplaythrough', (e) => {
this._audio.play()
})
Expand All @@ -29,11 +23,9 @@ export default {

// Set audio volume
setVolume(volume) {
if (!this._gain) return
volume = parseFloat(volume) || 0.0
volume = volume < 0 ? 0 : volume
volume = volume > 1 ? 1 : volume
this._gain.gain.value = volume
if (this._audio) {
this._audio.volume = Math.min(1, Math.max(0, parseFloat(volume) || 0.0))
}
},

// Play audio source url
Expand Down

0 comments on commit 39847ba

Please sign in to comment.