@euterpe.js/player
Advanced tools
Comparing version 1.0.23 to 2.0.0
{ | ||
"name": "@euterpe.js/player", | ||
"version": "1.0.23", | ||
"version": "2.0.0", | ||
"type": "module", | ||
@@ -33,5 +33,5 @@ "description": "A simple, safe AudioContext web music player", | ||
"peerDependencies": { | ||
"tslib": "2.6.0" | ||
"tslib": "2.6.1" | ||
}, | ||
"types": "./src/index.d.ts" | ||
} |
@@ -13,6 +13,6 @@ export declare enum SubscribeEvents { | ||
#private; | ||
private audio_context; | ||
audio_context: AudioContext; | ||
private audio_element; | ||
track: MediaElementAudioSourceNode; | ||
private gain; | ||
gain: GainNode; | ||
volume: number; | ||
@@ -29,28 +29,27 @@ private current_song_path?; | ||
/** | ||
* Safer seek_async. Normal seek will try to start the player even if the track hasn't started yet, or was previously suspended/closed | ||
* Safer seek_async. Normal seek will try to start the player even if the track hasn't started yet, or was previously suspended/closed. | ||
* Will also resume playback if player is paused (by finishing the song etc) | ||
* @throws if "Can't seek - Audiocontext is not running" | ||
*/ | ||
try_seek_async(new_time: number): Promise<unknown>; | ||
try_seek(new_time: number): Promise<void>; | ||
/** | ||
* Unsafe, throws error if failed. Use try_seek_async or seek_async unless you don't care about the result. | ||
*/ | ||
seek(new_time: number): void; | ||
/** | ||
* Safer play_toggle_async. Normal play_toggle will try to start the player even if the track hasn't started yet, or was previously suspended/closed | ||
* @throws Error if playback failed | ||
*/ | ||
try_play_toggle_async(): Promise<unknown>; | ||
try_play_toggle(): Promise<void>; | ||
/** | ||
* Can try to play even if the audio context was suspended or closed. Best to use try_play_toggle_async() | ||
*/ | ||
play_toggle_async(): Promise<unknown>; | ||
/** | ||
* Unsafe, throws error if failed. Use play_toggle_async or try_play_toggle_async unless you don't care about the result. | ||
* Unsafe, can just fail. Use try_play_toggle unless you don't care about the result. | ||
*/ | ||
play_toggle(): void; | ||
/** | ||
* Safer play_async. Normal play will try to start the player even if the track hasn't started yet, or was previously suspended/closed | ||
* Safer play. Normal play will try to start the player even if the track hasn't started yet, or was previously suspended/closed | ||
* @throws Error if playback failed | ||
*/ | ||
try_play_async(): Promise<unknown>; | ||
try_play(): Promise<void>; | ||
/** | ||
* Will try to play even if the audio context was suspended or closed. Best to use try_play_async() | ||
*/ | ||
play_async(): Promise<unknown>; | ||
/** | ||
* Unsafe, throws error if failed. Use play_async or try_play_async unless you don't care about the result. | ||
* Unsafe, can just fail. Use play_async or try_play_async unless you don't care about the result. | ||
*/ | ||
@@ -64,4 +63,5 @@ play(): void; | ||
* Will only load metadata of the upcoming song. Need to call try_play_async() afterwards to start the playback | ||
* @throws Error if adding element throwed Error or Stalled | ||
*/ | ||
try_new_song_async(path: string): Promise<unknown>; | ||
try_new_song(path: string): Promise<void>; | ||
/** | ||
@@ -68,0 +68,0 @@ * Won't tell if you if the song actually got loaded or if it failed. For a safer version use try_new_song_async() unless you don't care about the result |
161
src/index.js
var _MusicPlayer_instances, _MusicPlayer_volume_cache, _MusicPlayer_pub_sub, _MusicPlayer_emit_time, _MusicPlayer_emit_duration_fmt, _MusicPlayer_emit_time_fmt, _MusicPlayerBuilder_audio_context, _MusicPlayerBuilder_gain, _MusicPlayerBuilder_track, _MusicPlayerBuilder_volume, _MusicPlayerBuilder_prev_node, _MusicPlayerBuilder_is_gain_connected; | ||
import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib"; | ||
import { __awaiter, __classPrivateFieldGet, __classPrivateFieldSet } from "tslib"; | ||
export var SubscribeEvents; | ||
@@ -117,55 +117,41 @@ (function (SubscribeEvents) { | ||
/** | ||
* Safer seek_async. Normal seek will try to start the player even if the track hasn't started yet, or was previously suspended/closed | ||
* Safer seek_async. Normal seek will try to start the player even if the track hasn't started yet, or was previously suspended/closed. | ||
* Will also resume playback if player is paused (by finishing the song etc) | ||
* @throws if "Can't seek - Audiocontext is not running" | ||
*/ | ||
try_seek_async(new_time) { | ||
return new Promise((resolve, reject) => { | ||
if (this.track.context.state == "closed" || this.track.context.state == "suspended") { | ||
try_seek(new_time) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this.audio_context.state !== "running") { | ||
this.is_playing = false; | ||
reject("Can't seek - track not playing"); | ||
throw new Error("Can't seek - audioContext not running, audio_context.state : " + this.audio_context.state); | ||
} | ||
if (this.audio_element.paused) | ||
yield this.try_play(); | ||
this.audio_element.currentTime = new_time; | ||
resolve(null); | ||
/*audio_element.play().then((s) => resolve(s), (r) => { | ||
is_playing = false | ||
reject(r) | ||
})*/ | ||
}); | ||
} | ||
// THIS MIGHT BE UNNECESSARY? CUZ SEEKING DOESN'T REQUIRE PLAY | ||
// /** | ||
// * Can try to seek even if the audio context was suspended or closed. Best to use try_seek_async() | ||
// */ | ||
// seek_async(new_time: number) { | ||
// return new Promise((resolve, reject) => { | ||
// this.audio_element.currentTime = new_time | ||
// resolve(null) | ||
// /* audio_element.play().then((s) => resolve(s), (r) => { | ||
// is_playing = false | ||
// reject(r) | ||
// })*/ | ||
// }) | ||
// // } | ||
// /** | ||
// * Unsafe, throws error if failed. Use try_seek_async or seek_async unless you don't care about the result. | ||
// */ | ||
/** | ||
* Unsafe, throws error if failed. Use try_seek_async or seek_async unless you don't care about the result. | ||
*/ | ||
seek(new_time) { | ||
this.audio_element.currentTime = new_time; | ||
//this.audio_element.play().catch((e) => { throw e }) | ||
} | ||
/** | ||
* Safer play_toggle_async. Normal play_toggle will try to start the player even if the track hasn't started yet, or was previously suspended/closed | ||
* @throws Error if playback failed | ||
*/ | ||
try_play_toggle_async() { | ||
return new Promise((resolve, reject) => { | ||
if (this.audio_context.state === "suspended" || this.audio_context.state === "closed") { | ||
reject("Context closed or suspended"); | ||
try_play_toggle() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this.audio_context.state !== "running") { | ||
yield this.audio_context.resume(); | ||
} | ||
if (this.audio_element.paused) { | ||
this.audio_element.play().then((s) => { | ||
try { | ||
yield this.audio_element.play(); | ||
this.is_playing = true; | ||
resolve(s); | ||
}, (r) => { | ||
} | ||
catch (e) { | ||
this.is_playing = false; | ||
reject(r); | ||
}); | ||
throw e; | ||
} | ||
} | ||
@@ -175,3 +161,2 @@ else { | ||
this.is_playing = false; | ||
resolve(null); | ||
} | ||
@@ -181,27 +166,3 @@ }); | ||
/** | ||
* Can try to play even if the audio context was suspended or closed. Best to use try_play_toggle_async() | ||
*/ | ||
play_toggle_async() { | ||
return new Promise((resolve, reject) => { | ||
if (this.audio_context.state === "suspended" || this.audio_context.state === "closed") { | ||
this.audio_context.resume(); | ||
} | ||
if (this.audio_element.paused) { | ||
this.audio_element.play().then((s) => { | ||
this.is_playing = true; | ||
resolve(s); | ||
}, (r) => { | ||
this.is_playing = false; | ||
reject(r); | ||
}); | ||
} | ||
else { | ||
this.audio_element.pause(); | ||
this.is_playing = false; | ||
resolve(null); | ||
} | ||
}); | ||
} | ||
/** | ||
* Unsafe, throws error if failed. Use play_toggle_async or try_play_toggle_async unless you don't care about the result. | ||
* Unsafe, can just fail. Use try_play_toggle unless you don't care about the result. | ||
*/ | ||
@@ -222,41 +183,26 @@ play_toggle() { | ||
/** | ||
* Safer play_async. Normal play will try to start the player even if the track hasn't started yet, or was previously suspended/closed | ||
* Safer play. Normal play will try to start the player even if the track hasn't started yet, or was previously suspended/closed | ||
* @throws Error if playback failed | ||
*/ | ||
try_play_async() { | ||
return new Promise((resolve, reject) => { | ||
try_play() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this.is_playing) | ||
reject(Error("Already playing")); | ||
if (this.audio_context.state === "suspended" || this.audio_context.state === "closed") { | ||
reject("Context closed or suspended"); | ||
return; | ||
if (this.audio_context.state !== "running") { | ||
yield this.audio_context.resume(); | ||
} | ||
this.audio_element.play().then((s) => { | ||
this.is_playing = true; | ||
resolve(s); | ||
}, (r) => { | ||
this.is_playing = false; | ||
reject(r); | ||
}); | ||
}); | ||
} | ||
/** | ||
* Will try to play even if the audio context was suspended or closed. Best to use try_play_async() | ||
*/ | ||
play_async() { | ||
return new Promise((resolve, reject) => { | ||
if (this.is_playing) | ||
resolve(null); | ||
if (this.audio_context.state === "suspended" || this.audio_context.state === "closed") { | ||
this.audio_context.resume(); | ||
if (this.audio_element.paused) { | ||
try { | ||
yield this.audio_element.play(); | ||
this.is_playing = true; | ||
} | ||
catch (e) { | ||
this.is_playing = false; | ||
throw e; | ||
} | ||
} | ||
this.audio_element.play().then((s) => { | ||
this.is_playing = true; | ||
resolve(s); | ||
}, (r) => { | ||
this.is_playing = false; | ||
reject(r); | ||
}); | ||
}); | ||
} | ||
/** | ||
* Unsafe, throws error if failed. Use play_async or try_play_async unless you don't care about the result. | ||
* Unsafe, can just fail. Use play_async or try_play_async unless you don't care about the result. | ||
*/ | ||
@@ -266,5 +212,4 @@ play() { | ||
return; | ||
this.audio_element.play().catch((r) => { | ||
this.audio_element.play().catch(() => { | ||
this.is_playing = false; | ||
throw r; | ||
}); | ||
@@ -281,4 +226,5 @@ } | ||
* Will only load metadata of the upcoming song. Need to call try_play_async() afterwards to start the playback | ||
* @throws Error if adding element throwed Error or Stalled | ||
*/ | ||
try_new_song_async(path) { | ||
try_new_song(path) { | ||
return new Promise((resolve, reject) => { | ||
@@ -288,14 +234,18 @@ this.audio_element.src = this.current_song_path = path; | ||
const controller = new AbortController(); | ||
this.audio_element.addEventListener("canplaythrough", function canplay_listener(s) { | ||
this.audio_element.addEventListener("canplay", function canplay_listener(s) { | ||
controller.abort(); | ||
resolve(s); | ||
}, { signal: controller.signal }); | ||
this.audio_element.addEventListener("error", function error_listener(e) { | ||
controller.abort(); | ||
reject(e); | ||
controller.abort("new src error"); | ||
}, { signal: controller.signal }); | ||
this.audio_element.addEventListener("stalled", function stalled_listener(e) { | ||
controller.abort(); | ||
reject(e); | ||
controller.abort("new src stalled"); | ||
}, { signal: controller.signal }); | ||
//once aborted, try to set current_song_duration | ||
controller.signal.addEventListener("abort", (r) => { | ||
this.current_song_duration = this.audio_element.duration; | ||
if (typeof controller.signal.reason == "string") | ||
reject(new Error(controller.signal.reason)); | ||
resolve(); | ||
}); | ||
this.is_playing = false; | ||
@@ -483,3 +433,2 @@ }); | ||
__classPrivateFieldGet(this, _MusicPlayerBuilder_prev_node, "f").connect(__classPrivateFieldGet(this, _MusicPlayerBuilder_audio_context, "f").destination); | ||
this.audio_element.preload = "metadata"; | ||
return new MusicPlayer(__classPrivateFieldGet(this, _MusicPlayerBuilder_audio_context, "f"), this.audio_element, __classPrivateFieldGet(this, _MusicPlayerBuilder_track, "f"), __classPrivateFieldGet(this, _MusicPlayerBuilder_gain, "f"), __classPrivateFieldGet(this, _MusicPlayerBuilder_volume, "f")); | ||
@@ -486,0 +435,0 @@ } |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
41540
567