baobao-audio-playback 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.
Files changed (2) hide show
  1. package/index.js +102 -105
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,122 +1,119 @@
1
- const _BaobaoAudioPlayback = (options) => {
2
- options = Object.assign(
3
- {
4
- src: "",
5
- loop: false,
6
- // autoplay: false,
7
- icon: true,
8
- el: "body",
9
- preload: "auto",
10
- },
11
- options
12
- );
13
-
14
- this.loading = true;
15
- this.el = options.el;
16
- this.audioInstance = document.createElement("audio");
17
- this.audioInstance.preload = options.preload;
18
- this.audioInstance.src = options.src;
19
- this.audioInstance.loop = options.loop;
20
- this.instanceId = "commonAudio" + Date.parse(new Date());
1
+ class _BaobaoAudioPlayback {
2
+ constructor(options = {}) {
3
+ this.options = Object.assign(
4
+ {
5
+ src: "",
6
+ loop: false,
7
+ el: "body",
8
+ icon: true,
9
+ preload: "auto",
10
+ autoPlay: true,
11
+ loadingText: "Loading...",
12
+ failedText: "Retry",
13
+ volume: 1,
14
+ },
15
+ options,
16
+ );
21
17
 
22
- const templateString = "<a></a><p class='loadinghint'>音乐载入中</p>";
23
- const templateNode = document.createElement("div");
24
- templateNode.setAttribute("id", this.instanceId);
25
- templateNode.setAttribute("class", "common_musicbutton_wrapper");
26
- if (options.icon) {
27
- templateNode.innerHTML = templateString;
28
- }
29
- document.querySelector(options.el).appendChild(templateNode);
18
+ this.audio = document.createElement("audio");
19
+ this.audio.src = this.options.src;
20
+ this.audio.loop = this.options.loop;
21
+ this.audio.preload = this.options.preload;
22
+ this.audio.volume = this.options.volume;
30
23
 
31
- if (options.icon) {
32
- templateNode.classList.add("active");
33
- templateNode.classList.add("loading");
24
+ this.id = "audio_" + Date.now() + Math.random(1000);
25
+ this.createUI();
26
+ this.setupEvent();
27
+ this.tryPlay();
34
28
  }
35
- _BaobaoAudioPlayback.prototype.play();
36
29
 
37
- this.instanceElement = document.getElementById(this.instanceId);
30
+ createUI() {
31
+ const wrapper = document.createElement("div");
32
+ wrapper.setAttribute("id", this.id);
33
+ wrapper.setAttribute("class", "common_musicbutton_wrapper loading");
38
34
 
39
- this.instanceElement.addEventListener("click", () => {
40
- if (this.audioInstance.paused) {
41
- _BaobaoAudioPlayback.prototype.play();
42
- this.instanceElement.classList.add("active");
43
- } else {
44
- _BaobaoAudioPlayback.prototype.pause();
45
- this.instanceElement.classList.remove("active");
35
+ if (this.options.icon) {
36
+ wrapper.innerHTML = `<a></a><p class="loadinghint">${this.options.loadingText}</p>`;
46
37
  }
47
- });
48
- };
49
38
 
50
- _BaobaoAudioPlayback.prototype = {
51
- constructor: _BaobaoAudioPlayback,
52
- play: () => {
53
- const play = () => {
54
- document.getElementById(this.instanceId).classList.remove("loading");
55
- this.audioInstance
56
- .play()
57
- .then(() => {})
58
- .catch((error) => {
59
- console.log(error);
60
- });
61
- };
39
+ document.querySelector(this.options.el).appendChild(wrapper);
40
+ this.el = wrapper;
41
+ }
42
+
43
+ setupEvent() {
44
+ this.el.addEventListener("click", () => {
45
+ this.options.autoPlay = true;
46
+ if (this.audio.paused) {
47
+ this.play();
48
+ } else {
49
+ this.pause();
50
+ }
51
+ });
52
+ }
53
+
54
+ tryPlay() {
55
+ const environment = this.getEnv();
62
56
 
63
- if (_BaobaoAudioPlayback.prototype.checkEnvironment() !== "ios") {
64
- this.audioInstance.addEventListener(
65
- "canplaythrough",
66
- (e) => {
67
- play();
68
- },
69
- false
70
- );
57
+ if (environment === "ios") {
58
+ this.el.classList.remove("loading");
59
+ if (this.options.autoPlay) {
60
+ this.play();
61
+ }
71
62
  } else {
72
- play();
63
+ try {
64
+ // Non-iOS: Listen for 'canplaythrough' before playing
65
+ this.audio.addEventListener(
66
+ "canplaythrough",
67
+ () => {
68
+ this.el.classList.remove("loading");
69
+ if (this.options.autoPlay) {
70
+ this.play();
71
+ }
72
+ },
73
+ { once: true },
74
+ );
75
+ } catch (error) {
76
+ console.log(error);
77
+ }
73
78
  }
79
+ }
80
+ autoPlay() {}
81
+ play() {
82
+ this.el.classList.add("active");
83
+ this.audio.play().catch((e) => {
84
+ console.log("Playback failed:", e);
85
+ this.el.classList.add("error");
86
+ this.el.classList.remove("active");
87
+ this.el.innerHTML = `<a></a><p class="errorhint">${this.options.failedText}</p>`;
88
+ });
89
+ }
90
+
91
+ pause() {
92
+ this.audio.pause();
93
+ this.el.classList.remove("active");
94
+ }
74
95
 
75
- return this;
76
- },
77
- pause: () => {
78
- this.audioInstance.pause();
79
- return this;
80
- },
81
- checkEnvironment: () => {
96
+ getEnv() {
82
97
  const ua = navigator.userAgent;
83
- console.log("us", ua);
84
- const environmentDictionary = [
85
- {
86
- name: "wechat",
87
- checker: ua.toLowerCase().match(/MicroMessenger/i),
88
- getter: "",
89
- status: false,
90
- },
91
- {
92
- name: "ios",
93
- method: "window.webkit.messageHandlers.token.postMessage('')",
94
- checker: !!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),
95
- getter: "window.webkit.messageHandlers.token",
96
- status: false,
97
- },
98
- {
99
- name: "android",
100
- method: "window.android",
101
- checker: ua.match(/(Android)\s+([\d]+)/),
102
- getter: "window.android.getToken()",
103
- status: false,
104
- },
105
- ];
106
- let environment;
107
- environmentDictionary.forEach((item, index) => {
108
- if (item.checker) {
109
- environment = item.name;
110
- }
111
- });
112
- const result = environmentDictionary.filter((item) => item.checker);
113
- environment = result.length > 0 ? result[0].name : "unknown";
114
98
 
115
- return environment;
116
- },
117
- };
99
+ if (
100
+ /iPhone|iPad|iPod/.test(ua) ||
101
+ /\(i[^;]+;( U;)? CPU.+Mac OS X/.test(ua)
102
+ ) {
103
+ return "ios";
104
+ }
105
+ if (/Android/.test(ua)) {
106
+ return "android";
107
+ }
108
+ if (/MicroMessenger/i.test(ua)) {
109
+ return "wechat";
110
+ }
111
+ return "desktop";
112
+ }
113
+ }
118
114
 
119
- const BaobaoAudioPlayback = (options) => {
115
+ // Factory
116
+ var BaobaoAudioPlayback = function (options) {
120
117
  return new _BaobaoAudioPlayback(options);
121
118
  };
122
119
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "baobao-audio-playback",
3
- "version": "0.0.2",
3
+ "version": "1.0.0",
4
4
  "dependencies": {},
5
5
  "description": "",
6
6
  "main": "index.js",