@earjar/audio-engine-ts 0.0.1

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 ADDED
@@ -0,0 +1,42 @@
1
+ # Audio Engine (Typescript)
2
+
3
+ This is a media handling React hook.
4
+
5
+ ## Installation and Requirements
6
+ > Requires Node 24 and higher
7
+
8
+ Install by running
9
+ ```
10
+ npm install @earjar/audio-engine-ts
11
+ ```
12
+ ___
13
+
14
+ ## Usage
15
+ ```tsx
16
+ import { useAudioEngine } from "../hooks/AudioEngine";
17
+
18
+ const MyComponent = () => {
19
+ const { load, play, pause, isPlaying } = useAudioEngine();
20
+
21
+ const handlePlayClick = async () => {
22
+ load("https://example.com/track.mp3");
23
+ await play();
24
+ };
25
+
26
+ return (
27
+ <button onClick={isPlaying ? pause : handlePlayClick}>
28
+ {isPlaying ? "Pause" : "Play"}
29
+ </button>
30
+ );
31
+ };
32
+ ```
33
+ ___
34
+
35
+ ## Author
36
+
37
+ Kamvelihle Gwijana
38
+ - [Linkedin](https://www.linkedin.com/in/kamvelihle-gwijana)
39
+
40
+ ## License
41
+
42
+ ISC
@@ -0,0 +1,9 @@
1
+ declare const useAudioEngine: () => {
2
+ load: (src: string) => void;
3
+ play: () => Promise<void>;
4
+ pause: () => void;
5
+ isPlaying: boolean;
6
+ };
7
+ declare const playOneShot: (src: string) => void;
8
+
9
+ export { playOneShot, useAudioEngine };
@@ -0,0 +1,9 @@
1
+ declare const useAudioEngine: () => {
2
+ load: (src: string) => void;
3
+ play: () => Promise<void>;
4
+ pause: () => void;
5
+ isPlaying: boolean;
6
+ };
7
+ declare const playOneShot: (src: string) => void;
8
+
9
+ export { playOneShot, useAudioEngine };
package/dist/index.js ADDED
@@ -0,0 +1,78 @@
1
+ "use client";
2
+ "use strict";
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/index.ts
22
+ var index_exports = {};
23
+ __export(index_exports, {
24
+ playOneShot: () => playOneShot,
25
+ useAudioEngine: () => useAudioEngine
26
+ });
27
+ module.exports = __toCommonJS(index_exports);
28
+
29
+ // src/hooks/AudioEngine.ts
30
+ var import_react = require("react");
31
+ var useAudioEngine = () => {
32
+ const audioRef = (0, import_react.useRef)(null);
33
+ const [isPlaying, setIsPlaying] = (0, import_react.useState)(false);
34
+ if (!audioRef.current) {
35
+ audioRef.current = new Audio();
36
+ }
37
+ (0, import_react.useEffect)(() => {
38
+ const audio = audioRef.current;
39
+ const handlePlay = () => setIsPlaying(true);
40
+ const handlePause = () => setIsPlaying(false);
41
+ const handleEnded = () => setIsPlaying(false);
42
+ audio.addEventListener("play", handlePlay);
43
+ audio.addEventListener("pause", handlePause);
44
+ audio.addEventListener("ended", handleEnded);
45
+ return () => {
46
+ audio.removeEventListener("play", handlePlay);
47
+ audio.removeEventListener("pause", handlePause);
48
+ audio.removeEventListener("ended", handleEnded);
49
+ audio.pause();
50
+ };
51
+ }, []);
52
+ const load = (0, import_react.useCallback)((src) => {
53
+ const audio = audioRef.current;
54
+ audio.src = src;
55
+ audio.load();
56
+ }, []);
57
+ const play = (0, import_react.useCallback)(async () => {
58
+ try {
59
+ await audioRef.current?.play();
60
+ } catch {
61
+ setIsPlaying(false);
62
+ }
63
+ }, []);
64
+ const pause = (0, import_react.useCallback)(() => {
65
+ audioRef.current?.pause();
66
+ }, []);
67
+ return { load, play, pause, isPlaying };
68
+ };
69
+ var playOneShot = (src) => {
70
+ const audio = new Audio(src);
71
+ audio.play().catch(() => {
72
+ });
73
+ };
74
+ // Annotate the CommonJS export names for ESM import in node:
75
+ 0 && (module.exports = {
76
+ playOneShot,
77
+ useAudioEngine
78
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,51 @@
1
+ "use client";
2
+
3
+ // src/hooks/AudioEngine.ts
4
+ import { useCallback, useEffect, useRef, useState } from "react";
5
+ var useAudioEngine = () => {
6
+ const audioRef = useRef(null);
7
+ const [isPlaying, setIsPlaying] = useState(false);
8
+ if (!audioRef.current) {
9
+ audioRef.current = new Audio();
10
+ }
11
+ useEffect(() => {
12
+ const audio = audioRef.current;
13
+ const handlePlay = () => setIsPlaying(true);
14
+ const handlePause = () => setIsPlaying(false);
15
+ const handleEnded = () => setIsPlaying(false);
16
+ audio.addEventListener("play", handlePlay);
17
+ audio.addEventListener("pause", handlePause);
18
+ audio.addEventListener("ended", handleEnded);
19
+ return () => {
20
+ audio.removeEventListener("play", handlePlay);
21
+ audio.removeEventListener("pause", handlePause);
22
+ audio.removeEventListener("ended", handleEnded);
23
+ audio.pause();
24
+ };
25
+ }, []);
26
+ const load = useCallback((src) => {
27
+ const audio = audioRef.current;
28
+ audio.src = src;
29
+ audio.load();
30
+ }, []);
31
+ const play = useCallback(async () => {
32
+ try {
33
+ await audioRef.current?.play();
34
+ } catch {
35
+ setIsPlaying(false);
36
+ }
37
+ }, []);
38
+ const pause = useCallback(() => {
39
+ audioRef.current?.pause();
40
+ }, []);
41
+ return { load, play, pause, isPlaying };
42
+ };
43
+ var playOneShot = (src) => {
44
+ const audio = new Audio(src);
45
+ audio.play().catch(() => {
46
+ });
47
+ };
48
+ export {
49
+ playOneShot,
50
+ useAudioEngine
51
+ };
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@earjar/audio-engine-ts",
3
+ "version": "0.0.1",
4
+ "description": "This is a media handling React hook.",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
+ },
14
+ "./dist/index.css": "./dist/index.css"
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "sideEffects": false,
20
+ "scripts": {
21
+ "build": "tsup"
22
+ },
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "git+https://github.com/Earjar/audio-engine-ts.git"
26
+ },
27
+ "keywords": [
28
+ "typescript",
29
+ "audio",
30
+ "react"
31
+ ],
32
+ "author": "kamvedev",
33
+ "license": "ISC",
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "type": "commonjs",
38
+ "bugs": {
39
+ "url": "https://github.com/Earjar/audio-engine-ts/issues"
40
+ },
41
+ "homepage": "https://github.com/Earjar/audio-engine-ts#readme",
42
+ "devDependencies": {
43
+ "@types/react": "^16.8.0",
44
+ "tsup": "^8.5.1",
45
+ "typescript": "^5.7.3"
46
+ },
47
+ "peerDependencies": {
48
+ "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
49
+ }
50
+ }