Skip to content

Instantly share code, notes, and snippets.

@searls
Created May 24, 2020 04:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save searls/48aa86294a8e71b923339ddb6fe6019f to your computer and use it in GitHub Desktop.
Save searls/48aa86294a8e71b923339ddb6fe6019f to your computer and use it in GitHub Desktop.
How to play web audio that won't interrupt background system-wide audio playback in iOS
let audioContext
export function play (url) {
const AudioContext = window.AudioContext || window.webkitAudioContext
if (!audioContext) audioContext = new AudioContext()
const request = new window.XMLHttpRequest()
request.open('GET', url, true)
request.responseType = 'arraybuffer'
request.addEventListener('load', function (e) {
const source = audioContext.createBufferSource()
source.buffer = audioContext.createBuffer(request.response, false)
source.connect(audioContext.destination)
source.start(0)
}, false)
request.send()
}
export function play (url) {
const audio = new window.Audio(url)
audio.play()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment