Created
May 24, 2020 04:05
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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