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

unmuteIosAudio Promise return basic thing #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
30 changes: 18 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ function unmuteIosAudio () {
const sampleRate = (new AudioContext()).sampleRate
const silentAudioFile = createSilentAudioFile(sampleRate)

USER_ACTIVATION_EVENTS.forEach(eventName => {
window.addEventListener(
eventName, handleUserActivation, { capture: true, passive: true }
)
})

// Return a seven samples long 8 bit mono WAVE file
function createSilentAudioFile (sampleRate) {
const arrayBuffer = new ArrayBuffer(10)
Expand All @@ -55,15 +49,17 @@ function unmuteIosAudio () {
return `data:audio/wav;base64,UklGRisAAABXQVZFZm10IBAAAAABAAEA${missingCharacters}AgAZGF0YQcAAACAgICAgICAAAA=`
}

function handleUserActivation (e) {
if (htmlAudioState === 'blocked') {
htmlAudioState = 'pending'
createHtmlAudio()
}
function handleUserActivation (resolve, reject, e) {
if (webAudioState === 'blocked') {
webAudioState = 'pending'
createWebAudio()
}
if (htmlAudioState === 'blocked') {
htmlAudioState = 'pending'
Promise.all([createHtmlAudio(), context.resume()])
.then(() => resolve())
.catch(err = reject(err))
}
}

function createHtmlAudio () {
Expand All @@ -75,7 +71,7 @@ function unmuteIosAudio () {
audio.src = silentAudioFile
audio.load()

audio.play().then(
return audio.play().then(
() => {
htmlAudioState = 'allowed'
maybeCleanup()
Expand All @@ -102,6 +98,7 @@ function unmuteIosAudio () {
if (context.state === 'running') {
webAudioState = 'allowed'
maybeCleanup()
return true
} else {
webAudioState = 'blocked'

Expand All @@ -110,6 +107,7 @@ function unmuteIosAudio () {

context.close()
context = null
return false
}
}

Expand All @@ -122,4 +120,12 @@ function unmuteIosAudio () {
)
})
}

return new Promise((resole, reject) => {
USER_ACTIVATION_EVENTS.forEach(eventName => {
window.addEventListener(
eventName, handleUserActivation.bind(null, resolve, reject), { capture: true, passive: true }
)
})
})
}