-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfix-pico8-ios-sound.js
58 lines (49 loc) · 1.58 KB
/
fix-pico8-ios-sound.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
(function() {
// fetch original file name
var s = document.scripts[document.scripts.length - 1]
var file = s.getAttribute('data-original-file')
if (!file) throw new Error('Missing data-original-file attribute.')
// strip vendor prefixes
window.AudioContext = window.AudioContext
|| window.webkitAudioContext
|| window.mozAudioContext
|| window.oAudioContext
|| window.msAudioContext
// make AudioContext a singleton so we control it
var ctx = new window.AudioContext
window.AudioContext = function() { return ctx }
// create overlay
var o = document.createElement('div')
o.innerHTML = 'tap screen to load game'
o.style.cssText = [
'position: fixed',
'top: 0',
'left: 0',
'right: 0',
'bottom: 0',
'background: rgb(128, 128, 128)',
'background: rgba(128, 128, 128, 0.5)',
'color: white',
'text-align: center',
'padding-top: 200px',
].map(function(p) { return p + ';' }).join('')
document.body.appendChild(o)
// disable scrolling
document.body.style.overflow = 'hidden'
o.onclick = function() {
// ...until overlay is clicked
document.body.style.overflow = ''
// then unlock AudioContext on iOS
var buffer = ctx.createBuffer(1, 1, 22050)
var source = ctx.createBufferSource()
source.connect(ctx.destination)
if (source.noteOn) source.noteOn(0)
else source.start(0)
// dynamically load original script
var s = document.createElement('script')
s.setAttribute('src', file)
document.body.appendChild(s)
// and delete overlay div
document.body.removeChild(o)
}
})()