baobao-audio-playback 0.0.0-stage → 0.0.2

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
@@ -1,3 +1,127 @@
1
- # Temporary Holding Version
1
+ # Baobao Audio Playback
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+
4
+ [![npm version](https://img.shields.io/npm/v/baobao-audio-playback.svg)](https://www.npmjs.com/package/baobao-audio-playback)
5
+ [![npm downloads](https://img.shields.io/npm/dm/baobao-audio-playback.svg)](https://www.npmjs.com/package/baobao-audio-playback)
6
+ [![License](https://img.shields.io/npm/l/baobao-audio-playback.svg)](https://github.com/YOUR_USERNAME/baobao-audio-playback/blob/main/LICENSE)
7
+
8
+ A lightweight audio playback plugin for web applications.
9
+
10
+ **NPM:** [baobao-audio-playback](https://www.npmjs.com/package/baobao-audio-playback)
11
+
12
+ A lightweight audio playback plugin for web applications.
13
+
14
+ **Baobao Audio Playback** is a simple and lightweight audio playback utility that makes it easy to integrate audio playback functionality into your web application.
15
+
16
+ ## Features
17
+
18
+ - Lightweight and easy to integrate.
19
+ - Supports creating multiple instances to play multiple audio tracks.
20
+ - Simple API for audio playback.
21
+ - Supports loop playback.
22
+ - Configurable audio preload strategy.
23
+ - Optional built-in playback UI.
24
+ - Supports specifying a custom DOM container.
25
+
26
+ ## Installation
27
+
28
+ Install via npm:
29
+
30
+ ```bash
31
+ npm install baobao-audio-playback
32
+ ```
33
+
34
+ Or using Yarn:
35
+
36
+ ```bash
37
+ yarn add baobao-audio-playback
38
+ ```
39
+
40
+ ## Quick Start
41
+
42
+ ### Configuration Example
43
+
44
+ ```javascript
45
+ const BaobaoAudioPlayback = require('baobao-audio-playback');
46
+
47
+ const audio = BaobaoAudioPlayback({
48
+ src: 'https://example.com/audio.mp3',
49
+ loop: false,
50
+ icon: true,
51
+ el: '.audio-container',
52
+ preload: 'auto'
53
+ });
54
+
55
+ audio.play();
56
+ ```
57
+
58
+ ## API Reference
59
+
60
+ ### Options
61
+
62
+ The plugin accepts a configuration object when initialized.
63
+
64
+ | Option | Type | Default | Description |
65
+ | :--- | :--- | :--- | :--- |
66
+ | `src` | `String` | `''` | URL of the audio file. |
67
+ | `loop` | `Boolean` | `false` | Whether to enable loop playback. |
68
+ | `icon` | `Boolean` | `false` | Whether to display the built-in playback UI. |
69
+ | `el` | `String` | `'body'` | CSS selector of the parent DOM element where the component will be mounted. |
70
+ | `preload` | `String` | `'auto'` | Audio preload strategy. Supports native HTML audio preload values: `none`, `metadata`, and `auto`. |
71
+
72
+ ### Methods
73
+
74
+ | Method | Description |
75
+ | :--- | :--- |
76
+ | `play()` | Loads the audio resource and starts playback. |
77
+
78
+ ## Usage Examples
79
+
80
+ ### 1. Loop Playback
81
+
82
+ Enable continuous playback by setting `loop` to `true`.
83
+
84
+ ```javascript
85
+ const audio = BaobaoAudioPlayback({
86
+ src: 'https://example.com/background-music.mp3',
87
+ loop: true
88
+ });
89
+
90
+ audio.play();
91
+ ```
92
+
93
+ ### 2. Custom DOM Container
94
+
95
+ Specify a DOM container using the `el` option.
96
+
97
+ ```javascript
98
+ const audio = BaobaoAudioPlayback({
99
+ src: 'https://example.com/notification.mp3',
100
+ el: '#app'
101
+ });
102
+
103
+ audio.play();
104
+ ```
105
+
106
+ ### 3. Enable Playback UI
107
+
108
+ Set `icon` to `true` to enable the built-in playback UI.
109
+
110
+ ```javascript
111
+ const audio = BaobaoAudioPlayback({
112
+ src: 'https://example.com/audio.mp3',
113
+ icon: true,
114
+ });
115
+
116
+ audio.play();
117
+ ```
118
+
119
+ ## Notes
120
+
121
+ - Make sure the audio URL is accessible from your application.
122
+ - Browser autoplay policies may restrict audio playback without prior user interaction. For the most reliable experience, trigger `play()` from a user interaction such as a click.
123
+ - The `el` option should point to an existing DOM element when the plugin is initialized.
124
+
125
+ ## License
126
+
127
+ MIT
Binary file
package/index.js ADDED
@@ -0,0 +1,123 @@
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());
21
+
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);
30
+
31
+ if (options.icon) {
32
+ templateNode.classList.add("active");
33
+ templateNode.classList.add("loading");
34
+ }
35
+ _BaobaoAudioPlayback.prototype.play();
36
+
37
+ this.instanceElement = document.getElementById(this.instanceId);
38
+
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");
46
+ }
47
+ });
48
+ };
49
+
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
+ };
62
+
63
+ if (_BaobaoAudioPlayback.prototype.checkEnvironment() !== "ios") {
64
+ this.audioInstance.addEventListener(
65
+ "canplaythrough",
66
+ (e) => {
67
+ play();
68
+ },
69
+ false
70
+ );
71
+ } else {
72
+ play();
73
+ }
74
+
75
+ return this;
76
+ },
77
+ pause: () => {
78
+ this.audioInstance.pause();
79
+ return this;
80
+ },
81
+ checkEnvironment: () => {
82
+ 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
+
115
+ return environment;
116
+ },
117
+ };
118
+
119
+ const BaobaoAudioPlayback = (options) => {
120
+ return new _BaobaoAudioPlayback(options);
121
+ };
122
+
123
+ export default BaobaoAudioPlayback;
package/package.json CHANGED
@@ -1,6 +1,20 @@
1
1
  {
2
2
  "name": "baobao-audio-playback",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.2",
4
+ "dependencies": {},
5
+ "description": "",
6
+ "main": "index.js",
7
+ "scripts": {
8
+ "test": "echo \"Error: no test specified\" && exit 1"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/spikespiegel5112/BaobaoAudioPlayback.git"
13
+ },
14
+ "author": "spikespiegel",
15
+ "license": "ISC",
16
+ "bugs": {
17
+ "url": "https://github.com/spikespiegel5112/BaobaoAudioPlayback/issues"
18
+ },
19
+ "homepage": "https://github.com/spikespiegel5112/BaobaoAudioPlayback#readme"
20
+ }
@@ -0,0 +1,40 @@
1
+ .baobao_musicbutton_wrapper {
2
+ display: inline-block;
3
+ width: 1rem;
4
+ height: 1rem;
5
+ position: absolute;
6
+ top: 0.5rem;
7
+ left: 0.5rem;
8
+ z-index: 400;
9
+ }
10
+
11
+ .baobao_musicbutton_wrapper.active a {
12
+ animation: rotate 3s infinite linear;
13
+ }
14
+
15
+ .baobao_musicbutton_wrapper.loading .loadinghint {
16
+ display: block;
17
+ }
18
+
19
+ .baobao_musicbutton_wrapper .loadinghint {
20
+ display: none;
21
+ width: 3rem;
22
+ font-size: 0.5rem;
23
+ color: #666;
24
+ }
25
+
26
+ .baobao_musicbutton_wrapper .test {
27
+ position: absolute;
28
+ top: 0;
29
+ left: 0;
30
+ }
31
+
32
+ .baobao_musicbutton_wrapper a {
33
+ display: inline-block;
34
+ width: 100%;
35
+ height: 100%;
36
+ background-image: url("../image/music_00000.png");
37
+ background-size: 100%;
38
+ animation-play-state: paused;
39
+ position: relative;
40
+ }