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 CHANGED
@@ -9,8 +9,9 @@
9
9
 
10
10
  </div>
11
11
 
12
- <p align="center">A simple class to create elements that play audio when clicked.</p>
13
- <p align="center"><a href="https://n2q8x8.csb.app/">Demo</a></p>
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
  &#10148; **Install**
@@ -30,14 +31,14 @@ import ClickTone from 'clicktone';
30
31
 
31
32
  &#10148; **Usage**
32
33
 
33
- <sup>You can pass type, class, or ID selectors, as well as the DOM element itself, as parameters for 'el'.</sup>
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 clickTone = new ClickTone({
36
- el: '#button',
37
- sound: 'sound.mp3',
36
+ const click = new ClickTone({
37
+ file: './sound.mp3',
38
+ volume: 0.7,
38
39
  });
39
40
 
40
- clickTone.init();
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({ el: el = "", sound: sound = "" } = {}){
15
- this.el = el;
16
- this.sound = sound;
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.setupIOSAudioContextFix();
18
+ this.iOSFixAudioContext();
19
19
  }
20
- setupIOSAudioContextFix() {
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
- playAudio(url) {
33
- fetch(url).then((response)=>response.arrayBuffer()).then((buffer)=>this.audioContext.decodeAudioData(buffer)).then((audioData)=>{
34
- const source = this.audioContext.createBufferSource();
35
- source.buffer = audioData;
36
- source.connect(this.audioContext.destination);
37
- source.start(0);
38
- }).catch();
39
- }
40
- init() {
41
- const targetElement = typeof this.el === "string" ? document.querySelector(this.el) : this.el;
42
- targetElement.addEventListener("click", ()=>{
43
- this.playAudio(this.sound);
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
- {"mappings":";;;;;;;;;;;;AAAA,MAAM;IACJ,YAAY,MAAE,KAAK,WAAI,QAAQ,IAAI,GAAG,CAAC,CAAC,CAAE;QACxC,IAAI,CAAC,EAAE,GAAG;QACV,IAAI,CAAC,KAAK,GAAG;QAEb,IAAI,CAAC,YAAY,GAAG,IAAK,CAAA,OAAO,YAAY,IACvC,OAAO,kBAAkB,AAAD;QAC7B,IAAI,CAAC,uBAAuB;IAC9B;IAEA,0BAA0B;QACxB,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,UAAU,GAAG,EAAE;QACb,MAAM,KACH,IAAI,CAAC,CAAC,WAAa,SAAS,WAAW,IACvC,IAAI,CAAC,CAAC,SAAW,IAAI,CAAC,YAAY,CAAC,eAAe,CAAC,SACnD,IAAI,CAAC,CAAC;YACL,MAAM,SAAS,IAAI,CAAC,YAAY,CAAC,kBAAkB;YACnD,OAAO,MAAM,GAAG;YAChB,OAAO,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,WAAW;YAC5C,OAAO,KAAK,CAAC;QACf,GACC,KAAK;IACV;IAEA,OAAO;QACL,MAAM,gBAAgB,OAAO,IAAI,CAAC,EAAE,KAAK,WAAW,SAAS,aAAa,CAAC,IAAI,CAAC,EAAE,IAAI,IAAI,CAAC,EAAE;QAE7F,cAAc,gBAAgB,CAAC,SAAS;YACtC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK;QAC3B;IACF;AACF;IAEA,2CAAe","sources":["src/index.js"],"sourcesContent":["class ClickTone {\n constructor({ el = '', sound = '' } = {}) {\n this.el = el;\n this.sound = sound;\n\n this.audioContext = new (window.AudioContext\n || window.webkitAudioContext)();\n this.setupIOSAudioContextFix();\n }\n\n setupIOSAudioContextFix() {\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 playAudio(url) {\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 source.buffer = audioData;\n source.connect(this.audioContext.destination);\n source.start(0);\n })\n .catch();\n }\n\n init() {\n const targetElement = typeof this.el === 'string' ? document.querySelector(this.el) : this.el;\n\n targetElement.addEventListener('click', () => {\n this.playAudio(this.sound);\n });\n }\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"index.js.map"}
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.js.map"}
@@ -1,11 +1,11 @@
1
1
  class $cf838c15c8b009ba$var$ClickTone {
2
- constructor({ el: el = "", sound: sound = "" } = {}){
3
- this.el = el;
4
- this.sound = sound;
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.setupIOSAudioContextFix();
6
+ this.iOSFixAudioContext();
7
7
  }
8
- setupIOSAudioContextFix() {
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
- playAudio(url) {
21
- fetch(url).then((response)=>response.arrayBuffer()).then((buffer)=>this.audioContext.decodeAudioData(buffer)).then((audioData)=>{
22
- const source = this.audioContext.createBufferSource();
23
- source.buffer = audioData;
24
- source.connect(this.audioContext.destination);
25
- source.start(0);
26
- }).catch();
27
- }
28
- init() {
29
- const targetElement = typeof this.el === "string" ? document.querySelector(this.el) : this.el;
30
- targetElement.addEventListener("click", ()=>{
31
- this.playAudio(this.sound);
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
 
@@ -1 +1 @@
1
- {"mappings":"AAAA,MAAM;IACJ,YAAY,MAAE,KAAK,WAAI,QAAQ,IAAI,GAAG,CAAC,CAAC,CAAE;QACxC,IAAI,CAAC,EAAE,GAAG;QACV,IAAI,CAAC,KAAK,GAAG;QAEb,IAAI,CAAC,YAAY,GAAG,IAAK,CAAA,OAAO,YAAY,IACvC,OAAO,kBAAkB,AAAD;QAC7B,IAAI,CAAC,uBAAuB;IAC9B;IAEA,0BAA0B;QACxB,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,UAAU,GAAG,EAAE;QACb,MAAM,KACH,IAAI,CAAC,CAAC,WAAa,SAAS,WAAW,IACvC,IAAI,CAAC,CAAC,SAAW,IAAI,CAAC,YAAY,CAAC,eAAe,CAAC,SACnD,IAAI,CAAC,CAAC;YACL,MAAM,SAAS,IAAI,CAAC,YAAY,CAAC,kBAAkB;YACnD,OAAO,MAAM,GAAG;YAChB,OAAO,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,WAAW;YAC5C,OAAO,KAAK,CAAC;QACf,GACC,KAAK;IACV;IAEA,OAAO;QACL,MAAM,gBAAgB,OAAO,IAAI,CAAC,EAAE,KAAK,WAAW,SAAS,aAAa,CAAC,IAAI,CAAC,EAAE,IAAI,IAAI,CAAC,EAAE;QAE7F,cAAc,gBAAgB,CAAC,SAAS;YACtC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK;QAC3B;IACF;AACF;IAEA,2CAAe","sources":["src/index.js"],"sourcesContent":["class ClickTone {\n constructor({ el = '', sound = '' } = {}) {\n this.el = el;\n this.sound = sound;\n\n this.audioContext = new (window.AudioContext\n || window.webkitAudioContext)();\n this.setupIOSAudioContextFix();\n }\n\n setupIOSAudioContextFix() {\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 playAudio(url) {\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 source.buffer = audioData;\n source.connect(this.audioContext.destination);\n source.start(0);\n })\n .catch();\n }\n\n init() {\n const targetElement = typeof this.el === 'string' ? document.querySelector(this.el) : this.el;\n\n targetElement.addEventListener('click', () => {\n this.playAudio(this.sound);\n });\n }\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"index.module.js.map"}
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": "0.0.3",
4
- "description": "A simple class to create elements that play audio when clicked.",
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({ el = '', sound = '' } = {}) {
3
- this.el = el;
4
- this.sound = sound;
5
-
6
- this.audioContext = new (window.AudioContext
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
- setupIOSAudioContextFix() {
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
- playAudio(url) {
26
- fetch(url)
27
- .then((response) => response.arrayBuffer())
28
- .then((buffer) => this.audioContext.decodeAudioData(buffer))
29
- .then((audioData) => {
30
- const source = this.audioContext.createBufferSource();
31
- source.buffer = audioData;
32
- source.connect(this.audioContext.destination);
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
- init() {
39
- const targetElement = typeof this.el === 'string' ? document.querySelector(this.el) : this.el;
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
- targetElement.addEventListener('click', () => {
42
- this.playAudio(this.sound);
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;