baobao-audio-playback 0.0.2 → 0.0.14
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/index.js +102 -105
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,122 +1,119 @@
|
|
|
1
|
-
|
|
2
|
-
options =
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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
|
-
|
|
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
|
-
|
|
40
|
-
|
|
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
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
.
|
|
59
|
-
|
|
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 (
|
|
64
|
-
this.
|
|
65
|
-
|
|
66
|
-
(
|
|
67
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
115
|
+
// Factory
|
|
116
|
+
var BaobaoAudioPlayback = function (options) {
|
|
120
117
|
return new _BaobaoAudioPlayback(options);
|
|
121
118
|
};
|
|
122
119
|
|