one
Version:
One is a React framework for web, iOS, and Android apps.
305 lines (302 loc) • 7.96 kB
JavaScript
import { rethrowNativeError } from "../nativeError.native.js";
import { createPlayerEvents } from "./events.native.js";
import { audioNative } from "./hybrid.native.js";
import { assertCrossOrigin, assertFlag, assertPitchQuality, assertRate, assertSeconds, assertTolerance, assertVolume, normalizeSource, resolveUpdateInterval } from "./validate.native.js";
function _class_call_check(instance, Constructor) {
if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function");
}
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _create_class(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
function _define_property(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value,
enumerable: true,
configurable: true,
writable: true
});
} else obj[key] = value;
return obj;
}
function native() {
return audioNative();
}
function sync(work) {
try {
return work();
} catch (error) {
return rethrowNativeError(error);
}
}
var AudioPlayer = /* @__PURE__ */ (function() {
"use strict";
function AudioPlayer2(id, updateIntervalMs) {
var _this = this;
_class_call_check(this, AudioPlayer2);
_define_property(this, "playerId", void 0);
_define_property(this, "removed", false);
_define_property(this, "events", void 0);
this.playerId = id;
this.events = createPlayerEvents(function() {
return _this.getStatus();
}, updateIntervalMs);
}
_create_class(AudioPlayer2, [
{
key: "id",
get: function get() {
return this.playerId;
}
},
{
key: "currentTime",
get: function get() {
return this.status().currentTime;
}
},
{
key: "duration",
get: function get() {
return this.status().duration;
}
},
{
key: "playing",
get: function get() {
return this.status().playing;
}
},
{
key: "paused",
get: function get() {
return this.status().paused;
}
},
{
key: "isLoaded",
get: function get() {
return this.status().isLoaded;
}
},
{
key: "isBuffering",
get: function get() {
return this.status().isBuffering;
}
},
{
key: "isAudioSamplingSupported",
get: function get() {
return false;
}
},
{
key: "loop",
get: function get() {
return this.status().loop;
},
set: function set(value) {
var _this = this;
this.assertUsable("loop");
assertFlag(value, "AudioPlayer.loop");
sync(function() {
return native().setPlayerLoop(_this.playerId, value);
});
}
},
{
key: "muted",
get: function get() {
return this.status().muted;
},
set: function set(value) {
var _this = this;
this.assertUsable("muted");
assertFlag(value, "AudioPlayer.muted");
sync(function() {
return native().setPlayerMuted(_this.playerId, value);
});
}
},
{
key: "volume",
get: function get() {
return this.status().volume;
},
set: function set(value) {
var _this = this;
this.assertUsable("volume");
assertVolume(value, "AudioPlayer.volume");
sync(function() {
return native().setPlayerVolume(_this.playerId, value);
});
}
},
{
key: "playbackRate",
get: function get() {
return this.status().playbackRate;
}
},
{
key: "shouldCorrectPitch",
get: function get() {
return this.status().shouldCorrectPitch;
},
set: function set(value) {
var _this = this;
this.assertUsable("shouldCorrectPitch");
assertFlag(value, "AudioPlayer.shouldCorrectPitch");
sync(function() {
return native().setPlayerPitchCorrection(_this.playerId, value);
});
}
},
{
key: "play",
value: function play() {
var _this = this;
this.assertUsable("play");
sync(function() {
return native().playerPlay(_this.playerId);
});
}
},
{
key: "pause",
value: function pause() {
var _this = this;
this.assertUsable("pause");
sync(function() {
return native().playerPause(_this.playerId);
});
}
},
{
key: "seekTo",
value: function seekTo(seconds, toleranceMillisBefore, toleranceMillisAfter) {
this.assertUsable("seekTo");
assertSeconds(seconds, "AudioPlayer.seekTo");
if (toleranceMillisBefore !== void 0) {
assertTolerance(toleranceMillisBefore, "AudioPlayer.seekTo");
}
if (toleranceMillisAfter !== void 0) {
assertTolerance(toleranceMillisAfter, "AudioPlayer.seekTo");
}
return native().playerSeek(this.playerId, seconds, toleranceMillisBefore, toleranceMillisAfter).catch(rethrowNativeError);
}
},
{
key: "replace",
value: function replace(source) {
var _this = this;
this.assertUsable("replace");
var normalized = normalizeSource(source, "AudioPlayer.replace");
sync(function() {
return native().playerReplace(_this.playerId, normalized.uri, normalized.headers);
});
}
},
{
key: "remove",
value: function remove() {
var _this = this;
if (this.removed) return;
this.removed = true;
this.events.removeAll();
sync(function() {
return native().removePlayer(_this.playerId);
});
}
},
{
key: "getStatus",
value: function getStatus() {
var _status_error;
var status = this.status();
return {
currentTime: status.currentTime,
didJustFinish: status.didJustFinish,
duration: status.duration,
error: (_status_error = status.error) !== null && _status_error !== void 0 ? _status_error : null,
id: this.playerId,
isBuffering: status.isBuffering,
isLoaded: status.isLoaded,
loop: status.loop,
mute: status.muted,
playbackRate: status.playbackRate,
playing: status.playing,
shouldCorrectPitch: status.shouldCorrectPitch
};
}
},
{
key: "setPlaybackRate",
value: function setPlaybackRate(rate, pitchCorrectionQuality) {
var _this = this;
this.assertUsable("setPlaybackRate");
assertRate(rate, "AudioPlayer.setPlaybackRate");
if (pitchCorrectionQuality !== void 0) {
assertPitchQuality(pitchCorrectionQuality, "AudioPlayer.setPlaybackRate");
}
sync(function() {
return native().setPlayerRate(_this.playerId, rate, pitchCorrectionQuality);
});
}
},
{
key: "addListener",
value: function addListener(event, listener) {
this.assertUsable("addListener");
return this.events.add(event, listener);
}
},
{
key: "removeAllListeners",
value: function removeAllListeners() {
this.events.removeAll();
}
},
{
key: "status",
value: function status() {
var _this = this;
this.assertUsable("status");
return sync(function() {
return native().getPlayerStatus(_this.playerId);
});
}
},
{
key: "assertUsable",
value: function assertUsable(verb) {
if (this.removed) throw new Error(`AudioPlayer.${verb}: the player was removed`);
}
}
]);
return AudioPlayer2;
})();
function createPlayer() {
var source = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : null, options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
var normalized = normalizeSource(source, "Audio.createPlayer");
var updateIntervalMs = resolveUpdateInterval(options, "Audio.createPlayer");
assertCrossOrigin(options.crossOrigin, "Audio.createPlayer");
var id = sync(function() {
return native().createPlayer(normalized.uri, normalized.headers);
});
return new AudioPlayer(id, updateIntervalMs);
}
export { AudioPlayer, createPlayer };
//# sourceMappingURL=player.native.js.map