clicktone 0.0.3 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -7
- package/dist/index.js +22 -17
- package/dist/index.js.map +1 -1
- package/dist/index.module.js +22 -17
- package/dist/index.module.js.map +1 -1
- package/package.json +13 -3
- package/src/index.js +28 -24
package/README.md
CHANGED
|
@@ -9,8 +9,9 @@
|
|
|
9
9
|
|
|
10
10
|
</div>
|
|
11
11
|
|
|
12
|
-
<p align="center">A simple class
|
|
13
|
-
|
|
12
|
+
<p align="center">A simple class that provides a convenient abstraction for handling<br>
|
|
13
|
+
audio playback by web applications, with support for iOS devices.</p>
|
|
14
|
+
<p align="center"><a href="https://codepen.io/ux-ui/pen/yLwbmMr">Demo</a></p>
|
|
14
15
|
<br>
|
|
15
16
|
|
|
16
17
|
➤ **Install**
|
|
@@ -30,14 +31,14 @@ import ClickTone from 'clicktone';
|
|
|
30
31
|
|
|
31
32
|
➤ **Usage**
|
|
32
33
|
|
|
33
|
-
<
|
|
34
|
+
<sub>Class uses the Web Audio API, which supports many audio file formats: MP3, WAV, OGG, AAC and others. Please note that not all browsers support these formats.</sub>
|
|
34
35
|
```javascript
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
const click = new ClickTone({
|
|
37
|
+
file: './sound.mp3',
|
|
38
|
+
volume: 0.7,
|
|
38
39
|
});
|
|
39
40
|
|
|
40
|
-
|
|
41
|
+
myButton.addEventListener('click', () => click.play());
|
|
41
42
|
```
|
|
42
43
|
<br>
|
|
43
44
|
|
package/dist/index.js
CHANGED
|
@@ -11,13 +11,13 @@ $parcel$defineInteropFlag(module.exports);
|
|
|
11
11
|
|
|
12
12
|
$parcel$export(module.exports, "default", () => $4fa36e821943b400$export$2e2bcd8739ae039);
|
|
13
13
|
class $4fa36e821943b400$var$ClickTone {
|
|
14
|
-
constructor(
|
|
15
|
-
this.
|
|
16
|
-
this.
|
|
14
|
+
constructor(options){
|
|
15
|
+
this.file = options.file;
|
|
16
|
+
this.volume = options.volume || 1.0;
|
|
17
17
|
this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
|
18
|
-
this.
|
|
18
|
+
this.iOSFixAudioContext();
|
|
19
19
|
}
|
|
20
|
-
|
|
20
|
+
iOSFixAudioContext() {
|
|
21
21
|
if (this.audioContext.state === "suspended" && "ontouchstart" in window) {
|
|
22
22
|
const unlock = ()=>{
|
|
23
23
|
this.audioContext.resume().then(()=>{
|
|
@@ -29,20 +29,25 @@ class $4fa36e821943b400$var$ClickTone {
|
|
|
29
29
|
document.body.addEventListener("touchend", unlock, false);
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
32
|
+
audio(url) {
|
|
33
|
+
return new Promise((resolve, reject)=>{
|
|
34
|
+
fetch(url).then((response)=>response.arrayBuffer()).then((buffer)=>this.audioContext.decodeAudioData(buffer)).then((audioData)=>{
|
|
35
|
+
const source = this.audioContext.createBufferSource();
|
|
36
|
+
const gainNode = this.audioContext.createGain();
|
|
37
|
+
source.buffer = audioData;
|
|
38
|
+
gainNode.gain.value = this.volume;
|
|
39
|
+
source.connect(gainNode);
|
|
40
|
+
gainNode.connect(this.audioContext.destination);
|
|
41
|
+
source.start(0);
|
|
42
|
+
resolve();
|
|
43
|
+
}).catch((error)=>{
|
|
44
|
+
reject(error);
|
|
45
|
+
});
|
|
44
46
|
});
|
|
45
47
|
}
|
|
48
|
+
play(url = this.file) {
|
|
49
|
+
return this.audio(url);
|
|
50
|
+
}
|
|
46
51
|
}
|
|
47
52
|
var $4fa36e821943b400$export$2e2bcd8739ae039 = $4fa36e821943b400$var$ClickTone;
|
|
48
53
|
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1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BACN,OAAO;YACT;QACJ;IACF;IAEA,KAAK,MAAM,IAAI,CAAC,IAAI,EAAE;QACpB,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB;AACF;IAEA,2CAAe","sources":["src/index.js"],"sourcesContent":["class ClickTone {\n constructor(options) {\n this.file = options.file;\n this.volume = options.volume || 1.0;\n this.audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.iOSFixAudioContext();\n }\n\n iOSFixAudioContext() {\n if (this.audioContext.state === 'suspended' && 'ontouchstart' in window) {\n const unlock = () => {\n this.audioContext.resume().then(() => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n });\n };\n\n document.body.addEventListener('touchstart', unlock, false);\n document.body.addEventListener('touchend', unlock, false);\n }\n }\n\n audio(url) {\n return new Promise((resolve, reject) => {\n fetch(url)\n .then((response) => response.arrayBuffer())\n .then((buffer) => this.audioContext.decodeAudioData(buffer))\n .then((audioData) => {\n const source = this.audioContext.createBufferSource();\n const gainNode = this.audioContext.createGain();\n\n source.buffer = audioData;\n gainNode.gain.value = this.volume;\n source.connect(gainNode);\n gainNode.connect(this.audioContext.destination);\n source.start(0);\n\n resolve();\n })\n .catch((error) => {\n reject(error);\n });\n });\n }\n\n play(url = this.file) {\n return this.audio(url);\n }\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"index.js.map"}
|
package/dist/index.module.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
class $cf838c15c8b009ba$var$ClickTone {
|
|
2
|
-
constructor(
|
|
3
|
-
this.
|
|
4
|
-
this.
|
|
2
|
+
constructor(options){
|
|
3
|
+
this.file = options.file;
|
|
4
|
+
this.volume = options.volume || 1.0;
|
|
5
5
|
this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
|
6
|
-
this.
|
|
6
|
+
this.iOSFixAudioContext();
|
|
7
7
|
}
|
|
8
|
-
|
|
8
|
+
iOSFixAudioContext() {
|
|
9
9
|
if (this.audioContext.state === "suspended" && "ontouchstart" in window) {
|
|
10
10
|
const unlock = ()=>{
|
|
11
11
|
this.audioContext.resume().then(()=>{
|
|
@@ -17,20 +17,25 @@ class $cf838c15c8b009ba$var$ClickTone {
|
|
|
17
17
|
document.body.addEventListener("touchend", unlock, false);
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
20
|
+
audio(url) {
|
|
21
|
+
return new Promise((resolve, reject)=>{
|
|
22
|
+
fetch(url).then((response)=>response.arrayBuffer()).then((buffer)=>this.audioContext.decodeAudioData(buffer)).then((audioData)=>{
|
|
23
|
+
const source = this.audioContext.createBufferSource();
|
|
24
|
+
const gainNode = this.audioContext.createGain();
|
|
25
|
+
source.buffer = audioData;
|
|
26
|
+
gainNode.gain.value = this.volume;
|
|
27
|
+
source.connect(gainNode);
|
|
28
|
+
gainNode.connect(this.audioContext.destination);
|
|
29
|
+
source.start(0);
|
|
30
|
+
resolve();
|
|
31
|
+
}).catch((error)=>{
|
|
32
|
+
reject(error);
|
|
33
|
+
});
|
|
32
34
|
});
|
|
33
35
|
}
|
|
36
|
+
play(url = this.file) {
|
|
37
|
+
return this.audio(url);
|
|
38
|
+
}
|
|
34
39
|
}
|
|
35
40
|
var $cf838c15c8b009ba$export$2e2bcd8739ae039 = $cf838c15c8b009ba$var$ClickTone;
|
|
36
41
|
|
package/dist/index.module.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"AAAA,MAAM;IACJ,YAAY,
|
|
1
|
+
{"mappings":"AAAA,MAAM;IACJ,YAAY,OAAO,CAAE;QACnB,IAAI,CAAC,IAAI,GAAG,QAAQ,IAAI;QACxB,IAAI,CAAC,MAAM,GAAG,QAAQ,MAAM,IAAI;QAChC,IAAI,CAAC,YAAY,GAAG,IAAK,CAAA,OAAO,YAAY,IAAI,OAAO,kBAAkB,AAAD;QACxE,IAAI,CAAC,kBAAkB;IACzB;IAEA,qBAAqB;QACnB,IAAI,IAAI,CAAC,YAAY,CAAC,KAAK,KAAK,eAAe,kBAAkB,QAAQ;YACvE,MAAM,SAAS;gBACb,IAAI,CAAC,YAAY,CAAC,MAAM,GAAG,IAAI,CAAC;oBAC9B,SAAS,IAAI,CAAC,mBAAmB,CAAC,cAAc;oBAChD,SAAS,IAAI,CAAC,mBAAmB,CAAC,YAAY;gBAChD;YACF;YAEA,SAAS,IAAI,CAAC,gBAAgB,CAAC,cAAc,QAAQ;YACrD,SAAS,IAAI,CAAC,gBAAgB,CAAC,YAAY,QAAQ;QACrD;IACF;IAEA,MAAM,GAAG,EAAE;QACT,OAAO,IAAI,QAAQ,CAAC,SAAS;YAC3B,MAAM,KACH,IAAI,CAAC,CAAC,WAAa,SAAS,WAAW,IACvC,IAAI,CAAC,CAAC,SAAW,IAAI,CAAC,YAAY,CAAC,eAAe,CAAC,SACnD,IAAI,CAAC,CAAC;gBACL,MAAM,SAAS,IAAI,CAAC,YAAY,CAAC,kBAAkB;gBACnD,MAAM,WAAW,IAAI,CAAC,YAAY,CAAC,UAAU;gBAE7C,OAAO,MAAM,GAAG;gBAChB,SAAS,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,MAAM;gBACjC,OAAO,OAAO,CAAC;gBACf,SAAS,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,WAAW;gBAC9C,OAAO,KAAK,CAAC;gBAEb;YACF,GACC,KAAK,CAAC,CAAC;gBACN,OAAO;YACT;QACJ;IACF;IAEA,KAAK,MAAM,IAAI,CAAC,IAAI,EAAE;QACpB,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB;AACF;IAEA,2CAAe","sources":["src/index.js"],"sourcesContent":["class ClickTone {\n constructor(options) {\n this.file = options.file;\n this.volume = options.volume || 1.0;\n this.audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.iOSFixAudioContext();\n }\n\n iOSFixAudioContext() {\n if (this.audioContext.state === 'suspended' && 'ontouchstart' in window) {\n const unlock = () => {\n this.audioContext.resume().then(() => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n });\n };\n\n document.body.addEventListener('touchstart', unlock, false);\n document.body.addEventListener('touchend', unlock, false);\n }\n }\n\n audio(url) {\n return new Promise((resolve, reject) => {\n fetch(url)\n .then((response) => response.arrayBuffer())\n .then((buffer) => this.audioContext.decodeAudioData(buffer))\n .then((audioData) => {\n const source = this.audioContext.createBufferSource();\n const gainNode = this.audioContext.createGain();\n\n source.buffer = audioData;\n gainNode.gain.value = this.volume;\n source.connect(gainNode);\n gainNode.connect(this.audioContext.destination);\n source.start(0);\n\n resolve();\n })\n .catch((error) => {\n reject(error);\n });\n });\n }\n\n play(url = this.file) {\n return this.audio(url);\n }\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"index.module.js.map"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clicktone",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "A simple class
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "A simple class that provides a convenient abstraction for handling audio playback by web applications, with support for iOS devices.",
|
|
5
5
|
"author": "ux-ui.pro",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
@@ -34,5 +34,15 @@
|
|
|
34
34
|
"parcel": "^2.11.0",
|
|
35
35
|
"postcss-html": "^1.6.0"
|
|
36
36
|
},
|
|
37
|
-
"keywords": [
|
|
37
|
+
"keywords": [
|
|
38
|
+
"MP3",
|
|
39
|
+
"WAV",
|
|
40
|
+
"OGG",
|
|
41
|
+
"AAC",
|
|
42
|
+
"iOS",
|
|
43
|
+
"sound",
|
|
44
|
+
"audio",
|
|
45
|
+
"playback",
|
|
46
|
+
"mobile"
|
|
47
|
+
]
|
|
38
48
|
}
|
package/src/index.js
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
class ClickTone {
|
|
2
|
-
constructor(
|
|
3
|
-
this.
|
|
4
|
-
this.
|
|
5
|
-
|
|
6
|
-
this.
|
|
7
|
-
|| window.webkitAudioContext)();
|
|
8
|
-
this.setupIOSAudioContextFix();
|
|
2
|
+
constructor(options) {
|
|
3
|
+
this.file = options.file;
|
|
4
|
+
this.volume = options.volume || 1.0;
|
|
5
|
+
this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
|
6
|
+
this.iOSFixAudioContext();
|
|
9
7
|
}
|
|
10
8
|
|
|
11
|
-
|
|
9
|
+
iOSFixAudioContext() {
|
|
12
10
|
if (this.audioContext.state === 'suspended' && 'ontouchstart' in window) {
|
|
13
11
|
const unlock = () => {
|
|
14
12
|
this.audioContext.resume().then(() => {
|
|
@@ -22,26 +20,32 @@ class ClickTone {
|
|
|
22
20
|
}
|
|
23
21
|
}
|
|
24
22
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
source.start(0);
|
|
34
|
-
})
|
|
35
|
-
.catch();
|
|
36
|
-
}
|
|
23
|
+
audio(url) {
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
25
|
+
fetch(url)
|
|
26
|
+
.then((response) => response.arrayBuffer())
|
|
27
|
+
.then((buffer) => this.audioContext.decodeAudioData(buffer))
|
|
28
|
+
.then((audioData) => {
|
|
29
|
+
const source = this.audioContext.createBufferSource();
|
|
30
|
+
const gainNode = this.audioContext.createGain();
|
|
37
31
|
|
|
38
|
-
|
|
39
|
-
|
|
32
|
+
source.buffer = audioData;
|
|
33
|
+
gainNode.gain.value = this.volume;
|
|
34
|
+
source.connect(gainNode);
|
|
35
|
+
gainNode.connect(this.audioContext.destination);
|
|
36
|
+
source.start(0);
|
|
40
37
|
|
|
41
|
-
|
|
42
|
-
|
|
38
|
+
resolve();
|
|
39
|
+
})
|
|
40
|
+
.catch((error) => {
|
|
41
|
+
reject(error);
|
|
42
|
+
});
|
|
43
43
|
});
|
|
44
44
|
}
|
|
45
|
+
|
|
46
|
+
play(url = this.file) {
|
|
47
|
+
return this.audio(url);
|
|
48
|
+
}
|
|
45
49
|
}
|
|
46
50
|
|
|
47
51
|
export default ClickTone;
|