audio-channel-queue 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/LICENSE +21 -0
- package/README.md +59 -0
- package/dist/audio.d.ts +9 -0
- package/dist/audio.js +65 -0
- package/package.json +31 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 TONY CARPENTER
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# Audio Queue Package
|
|
2
|
+
The purpose of this package is to help queue audio files so they do not play on top of each other.
|
|
3
|
+
|
|
4
|
+
You can also enqueue audio files to different queues. This allows you to play sounds concurrently, but not have them overlap in their given audio queue.
|
|
5
|
+
|
|
6
|
+
To preview this package and see how it works with visualized code examples, check out the demo that can be found here: _____.
|
|
7
|
+
|
|
8
|
+
## How To Install This Package:
|
|
9
|
+
Install this package by running either of these commands:
|
|
10
|
+
- For npm run `npm install _____ --save?` (for typescript run `npm install ____ ____--save`)
|
|
11
|
+
- For yarn run `yarn add _____` (for typescript run `yarn add ____ ____`)
|
|
12
|
+
|
|
13
|
+
### How To Use This Package:
|
|
14
|
+
```queueAudio(audioFileGoesHere);```
|
|
15
|
+
Use the `queueAudio()` function to add a file to the queue and start playing it automatically. It takes two arguments:
|
|
16
|
+
- The first argument is an imported sound file.
|
|
17
|
+
- The second argument is optional and it allows you to choose a different queue channel.
|
|
18
|
+
|
|
19
|
+
```stopCurrentAudioInChannel(queueChannelNumberGoesHere);```
|
|
20
|
+
Use the `stopCurrentAudioInChannel()` function to stop the current playback of a file in a queue and start playing the next one automatically. It takes one argument:
|
|
21
|
+
- The first argument is optional and it allows you to choose a different queue channel. If you are only using the default channel, just use `stopCurrentAudioInChannel()`.
|
|
22
|
+
|
|
23
|
+
```stopAllAudioInChannel(queueChannelNumberGoesHere);```
|
|
24
|
+
Use the `stopAllAudioInChannel()` function to stop the current playback of all files in a queue and removes all enqueued files. It takes one argument:
|
|
25
|
+
- The first argument is optional and it allows you to choose a different queue channel. If you are only using the default channel, just use `stopAllAudioInChannel()`.
|
|
26
|
+
|
|
27
|
+
```stopAllAudio(queueChannelNumberGoesHere);```
|
|
28
|
+
Use the `stopAllAudio()` function to stop the current playback of all files in all queues. It takes no arguments.
|
|
29
|
+
|
|
30
|
+
If you need to expose the queue array for logging or other purposes, it is available to you as well: `audioChannels`.
|
|
31
|
+
|
|
32
|
+
### Example Usage in React:
|
|
33
|
+
|
|
34
|
+
`App.tsx`
|
|
35
|
+
```
|
|
36
|
+
import redTeamWins from './audio/red_team_wins.mp3';
|
|
37
|
+
import { queueAudio } from './queue-audio-package ____';
|
|
38
|
+
|
|
39
|
+
function App(): JSX.Element {
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
<div>
|
|
43
|
+
<button onClick={()=> queueAudio(redTeamWins)}>Play sound</button>
|
|
44
|
+
</div>
|
|
45
|
+
)
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export default App;
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
If you cannot import audio files into your app, you may need a `custom.d.ts` file in the root directory. An example of one is shown here:
|
|
52
|
+
|
|
53
|
+
`custom.d.ts`
|
|
54
|
+
```
|
|
55
|
+
declare module '*.mp3' {
|
|
56
|
+
const src: string;
|
|
57
|
+
export default src;
|
|
58
|
+
}
|
|
59
|
+
```
|
package/dist/audio.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type AudioQueue = HTMLAudioElement[];
|
|
2
|
+
export type AudioQueueChannel = {
|
|
3
|
+
queue: AudioQueue;
|
|
4
|
+
};
|
|
5
|
+
export declare const audioChannels: AudioQueueChannel[];
|
|
6
|
+
export declare const queueAudio: (audioUrl: string, channelNumber?: number) => Promise<void>;
|
|
7
|
+
export declare const stopCurrentAudioInChannel: (channelNumber?: number) => void;
|
|
8
|
+
export declare const stopAllAudioInChannel: (channelNumber?: number) => void;
|
|
9
|
+
export declare const stopAllAudio: () => void;
|
package/dist/audio.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.stopAllAudio = exports.stopAllAudioInChannel = exports.stopCurrentAudioInChannel = exports.queueAudio = exports.audioChannels = void 0;
|
|
13
|
+
exports.audioChannels = [];
|
|
14
|
+
const queueAudio = (audioUrl_1, ...args_1) => __awaiter(void 0, [audioUrl_1, ...args_1], void 0, function* (audioUrl, channelNumber = 0) {
|
|
15
|
+
if (!exports.audioChannels[channelNumber]) {
|
|
16
|
+
exports.audioChannels[channelNumber] = { queue: [] };
|
|
17
|
+
}
|
|
18
|
+
const audio = new Audio(audioUrl);
|
|
19
|
+
exports.audioChannels[channelNumber].queue.push(audio);
|
|
20
|
+
if (exports.audioChannels[channelNumber].queue.length === 1) {
|
|
21
|
+
return playAudioQueue(channelNumber);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
exports.queueAudio = queueAudio;
|
|
25
|
+
const playAudioQueue = (channelNumber) => __awaiter(void 0, void 0, void 0, function* () {
|
|
26
|
+
const channel = exports.audioChannels[channelNumber];
|
|
27
|
+
if (channel.queue.length === 0)
|
|
28
|
+
return;
|
|
29
|
+
const currentAudio = channel.queue[0];
|
|
30
|
+
return new Promise((resolve) => {
|
|
31
|
+
currentAudio.addEventListener('ended', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
32
|
+
channel.queue.shift();
|
|
33
|
+
yield playAudioQueue(channelNumber);
|
|
34
|
+
resolve();
|
|
35
|
+
}));
|
|
36
|
+
currentAudio.play();
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
const stopCurrentAudioInChannel = (channelNumber = 0) => {
|
|
40
|
+
const channel = exports.audioChannels[channelNumber];
|
|
41
|
+
if (channel && channel.queue.length > 0) {
|
|
42
|
+
const currentAudio = channel.queue[0];
|
|
43
|
+
currentAudio.pause();
|
|
44
|
+
channel.queue.shift();
|
|
45
|
+
playAudioQueue(channelNumber);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
exports.stopCurrentAudioInChannel = stopCurrentAudioInChannel;
|
|
49
|
+
const stopAllAudioInChannel = (channelNumber = 0) => {
|
|
50
|
+
const channel = exports.audioChannels[channelNumber];
|
|
51
|
+
if (channel) {
|
|
52
|
+
if (channel.queue.length > 0) {
|
|
53
|
+
const currentAudio = channel.queue[0];
|
|
54
|
+
currentAudio.pause();
|
|
55
|
+
}
|
|
56
|
+
channel.queue = [];
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
exports.stopAllAudioInChannel = stopAllAudioInChannel;
|
|
60
|
+
const stopAllAudio = () => {
|
|
61
|
+
exports.audioChannels.forEach((_channel, index) => {
|
|
62
|
+
(0, exports.stopAllAudioInChannel)(index);
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
exports.stopAllAudio = stopAllAudio;
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "audio-channel-queue",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Allows you to queue audio files to different playback channels.",
|
|
5
|
+
"main": "dist/audio.js",
|
|
6
|
+
"types": "dist/audio.d.ts",
|
|
7
|
+
"files": ["dist", "src"],
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"prepare": "npm run build"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/tonycarpenter21/audio-queue-package.git"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"audio",
|
|
18
|
+
"queue",
|
|
19
|
+
"channel",
|
|
20
|
+
"no dependencies"
|
|
21
|
+
],
|
|
22
|
+
"author": "Tony Carpenter",
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/tonycarpenter21/audio-queue-package/issues"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/tonycarpenter21/audio-queue-package#readme",
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"typescript": "^5.6.2"
|
|
30
|
+
}
|
|
31
|
+
}
|