clicktone 0.0.2 → 1.0.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://5mq6lq.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://n2q8x8.csb.app/">Demo</a></p>
14
15
  <br>
15
16
 
16
17
  &#10148; **Install**
@@ -30,10 +31,11 @@ import ClickTone from 'clicktone';
30
31
 
31
32
  &#10148; **Usage**
32
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>
33
35
  ```javascript
34
- const clickTone = new ClickTone();
36
+ const clickSound = new ClickTone('./sound.mp3');
35
37
 
36
- clickTone.init('myButton', 'sound.mp3');
38
+ myButton.addEventListener('click', () => clickSound.play());
37
39
  ```
38
40
  <br>
39
41
 
package/dist/index.js CHANGED
@@ -11,13 +11,12 @@ $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(file){
15
+ this.file = file;
17
16
  this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
18
- this.setupIOSAudioContextFix();
17
+ this.iOSFixAudioContext();
19
18
  }
20
- setupIOSAudioContextFix() {
19
+ iOSFixAudioContext() {
21
20
  if (this.audioContext.state === "suspended" && "ontouchstart" in window) {
22
21
  const unlock = ()=>{
23
22
  this.audioContext.resume().then(()=>{
@@ -29,20 +28,22 @@ class $4fa36e821943b400$var$ClickTone {
29
28
  document.body.addEventListener("touchend", unlock, false);
30
29
  }
31
30
  }
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);
31
+ audio(url) {
32
+ return new Promise((resolve, reject)=>{
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
+ resolve();
39
+ }).catch((error)=>{
40
+ reject(error);
41
+ });
44
42
  });
45
43
  }
44
+ play(url = this.file) {
45
+ return this.audio(url);
46
+ }
46
47
  }
47
48
  var $4fa36e821943b400$export$2e2bcd8739ae039 = $4fa36e821943b400$var$ClickTone;
48
49
 
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,IAC1C,OAAO,kBAAkB,AAAD;QAC1B,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,gBACJ,OAAO,IAAI,CAAC,EAAE,KAAK,WAAW,SAAS,aAAa,CAAC,IAAI,CAAC,EAAE,IAAI,IAAI,CAAC,EAAE;QAEzE,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 =\n 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,IAAI,CAAE;QAChB,IAAI,CAAC,IAAI,GAAG;QACZ,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;gBAEnD,OAAO,MAAM,GAAG;gBAChB,OAAO,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,WAAW;gBAC5C,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(file) {\n this.file = file;\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\n source.buffer = audioData;\n source.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,10 @@
1
1
  class $cf838c15c8b009ba$var$ClickTone {
2
- constructor({ el: el = "", sound: sound = "" } = {}){
3
- this.el = el;
4
- this.sound = sound;
2
+ constructor(file){
3
+ this.file = file;
5
4
  this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
6
- this.setupIOSAudioContextFix();
5
+ this.iOSFixAudioContext();
7
6
  }
8
- setupIOSAudioContextFix() {
7
+ iOSFixAudioContext() {
9
8
  if (this.audioContext.state === "suspended" && "ontouchstart" in window) {
10
9
  const unlock = ()=>{
11
10
  this.audioContext.resume().then(()=>{
@@ -17,20 +16,22 @@ class $cf838c15c8b009ba$var$ClickTone {
17
16
  document.body.addEventListener("touchend", unlock, false);
18
17
  }
19
18
  }
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);
19
+ audio(url) {
20
+ return new Promise((resolve, reject)=>{
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
+ resolve();
27
+ }).catch((error)=>{
28
+ reject(error);
29
+ });
32
30
  });
33
31
  }
32
+ play(url = this.file) {
33
+ return this.audio(url);
34
+ }
34
35
  }
35
36
  var $cf838c15c8b009ba$export$2e2bcd8739ae039 = $cf838c15c8b009ba$var$ClickTone;
36
37
 
@@ -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,IAC1C,OAAO,kBAAkB,AAAD;QAC1B,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,gBACJ,OAAO,IAAI,CAAC,EAAE,KAAK,WAAW,SAAS,aAAa,CAAC,IAAI,CAAC,EAAE,IAAI,IAAI,CAAC,EAAE;QAEzE,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 =\n 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,IAAI,CAAE;QAChB,IAAI,CAAC,IAAI,GAAG;QACZ,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;gBAEnD,OAAO,MAAM,GAAG;gBAChB,OAAO,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,WAAW;gBAC5C,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(file) {\n this.file = file;\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\n source.buffer = audioData;\n source.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.2",
4
- "description": "A simple class to create elements that play audio when clicked.",
3
+ "version": "1.0.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,11 @@
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(file) {
3
+ this.file = file;
4
+ this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
5
+ this.iOSFixAudioContext();
9
6
  }
10
7
 
11
- setupIOSAudioContextFix() {
8
+ iOSFixAudioContext() {
12
9
  if (this.audioContext.state === 'suspended' && 'ontouchstart' in window) {
13
10
  const unlock = () => {
14
11
  this.audioContext.resume().then(() => {
@@ -22,27 +19,29 @@ class ClickTone {
22
19
  }
23
20
  }
24
21
 
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
- }
22
+ audio(url) {
23
+ return new Promise((resolve, reject) => {
24
+ fetch(url)
25
+ .then((response) => response.arrayBuffer())
26
+ .then((buffer) => this.audioContext.decodeAudioData(buffer))
27
+ .then((audioData) => {
28
+ const source = this.audioContext.createBufferSource();
37
29
 
38
- init() {
39
- const targetElement =
40
- typeof this.el === 'string' ? document.querySelector(this.el) : this.el;
30
+ source.buffer = audioData;
31
+ source.connect(this.audioContext.destination);
32
+ source.start(0);
41
33
 
42
- targetElement.addEventListener('click', () => {
43
- this.playAudio(this.sound);
34
+ resolve();
35
+ })
36
+ .catch((error) => {
37
+ reject(error);
38
+ });
44
39
  });
45
40
  }
41
+
42
+ play(url = this.file) {
43
+ return this.audio(url);
44
+ }
46
45
  }
47
46
 
48
47
  export default ClickTone;