forked from web-api-examples/web-api-examples.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.html
147 lines (124 loc) · 4.87 KB
/
audio.html
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
<meta name="title" content="Web Audio API">
<meta name="description" content="API for synthesizing and addding effects to audio.">
<meta name="image" content="">
<meta name="site" content="">
<link type="text/css" rel="stylesheet" href="./css/fonts.css" />
<link type="text/css" rel="stylesheet" href="./css/style.css" />
<title>Web APIs Audio Example</title>
<style>
audio {
width:100%;
height:inherit;
}
</style>
<body>
<header>
<div class="header-bars" onclick="toggleNav('.aside-navbar')"><span class="header-bar fa-bars"></span></div>
<div class="title-bar"><h2>Web APIs</h2></div>
<div class="navbar">
<ul class="navbar-nav">
<li class="navbar-nav-item">
<a onclick="toggle('.dropdown')">More APIs</a>
<ul class="dropdown close">
<li class="dropdown-item"><a href="./history.html">History API</a></li>
<li class="dropdown-item"><a href="./vibration.html">Vibration API</a></li>
<li class="dropdown-item"><a href="./fullscreen.html">Fullscreen API</a></li>
<li class="dropdown-item"><a href="./index.html">More ...</a></li>
</ul>
</li>
</ul>
</div>
</header>
<aside class="aside-sidebar" id="leftAside">
<div class="aside-sidebar-header">
<h3>Web APIs Menu</h3></div>
<ul>
<li><a href="./bluetooth.html">Bluetooth API</a></li>
<li><a href="./touch.html">Touch API</a></li>
<li><a href="./battery.html">Battery API</a></li>
<li><a href="./clipboard.html">Clipboard API</a></li>
<li><a class="aside-sidebar-active" href="./audio.html">Web Audio API</a></li>
<li><a href="./broadcastchannel.html">Broadcastchannel Messaging API</a></li>
<li><a href="./channelmessaging.html">Channel Messaging API</a></li>
</ul>
</aside>
<div class="container">
<div class="web-api-cnt">
<div class="web-api-card">
<div class="web-api-card-head">
Demo - Audio
</div>
<div class="web-api-card-body">
<div id="error" class="close"></div>
<div>
<audio controls src="./audio.mp3" id="audio"></audio>
</div>
<div>
<button onclick="audioFromAudioFile.init()">Init</button>
<button onclick="audioFromAudioFile.play()">Play</button>
<button onclick="audioFromAudioFile.pause()">Pause</button>
<button onclick="audioFromAudioFile.stop()">Stop</button>
</div>
<div>
<span>Vol: <input onchange="audioFromAudioFile.changeVolume()" type="range" id="vol" min="1" max="2" step="0.01" value="1" /></span>
<span>Pan: <input onchange="audioFromAudioFile.changePan()" type="range" id="panner" min="-1" max="1" step="0.01" value="0" /></span>
</div>
</div>
</div>
</div>
</div>
</body>
<script src="./js/script.js"></script>
<script>
const l = console.log
let audioFromAudioFile = (function() {
var audioContext
var volNode
var pannerNode
var mediaSource
function init() {
l("Init")
try {
audioContext = new AudioContext()
mediaSource = audioContext.createMediaElementSource(audio)
volNode = audioContext.createGain()
volNode.gain.value = 1
pannerNode = new StereoPannerNode(audioContext, { pan:0 })
mediaSource.connect(volNode).connect(pannerNode).connect(audioContext.destination)
}
catch(e) {
error.innerHTML = "The Web Audio API is not supported in this device."
error.classList.remove("close")
}
}
function play() {
audio.play()
}
function pause() {
audio.pause()
}
function stop() {
audio.stop()
}
function changeVolume() {
volNode.gain.value = this.value
l("Vol Range:",this.value)
}
function changePan() {
pannerNode.gain.value = this.value
l("Pan Range:",this.value)
}
return {
init,
play,
pause,
stop,
changePan,
changeVolume
}
})()
</script>
</html>