react-native-audio-api
Version:
react-native-audio-api provides system for controlling audio in React Native environment compatible with Web Audio API specification
77 lines (76 loc) • 2.02 kB
JavaScript
;
import { AudioEventEmitter } from "../events/index.js";
import AudioScheduledSourceNode from "../core/AudioScheduledSourceNode.js";
const MAX_PLAYBACK_RATE = 4;
export class AudioFileSourceNode extends AudioScheduledSourceNode {
emitter = new AudioEventEmitter(globalThis.AudioEventEmitter);
attach(options) {
this.resetNodeAndSubscriptions();
const sub = this.emitter.addAudioEventListener('ended', _event => {
options.onEnded();
});
this.node.onEnded = sub.subscriptionId;
return {
duration: this.node.duration
};
}
dispose() {
this.resetNodeAndSubscriptions();
}
play() {
this.node.start(this.context.currentTime);
}
pause() {
this.node.pause();
}
seekToTime(seconds) {
this.node.seekToTime(seconds);
}
setVolume(value) {
this.node.volume = value;
}
setLoop(value) {
this.node.loop = value;
}
setPlaybackRate(value) {
if (!Number.isFinite(value) || value < 0 || value > MAX_PLAYBACK_RATE) {
throw new Error(`AudioFileSourceNode: playbackRate must be a non-negative number, less than or equal to ${MAX_PLAYBACK_RATE}.`);
}
this.node.playbackRate = value;
}
setPreservesPitch(value) {
this.node.preservesPitch = value;
}
getFileSourceNode() {
return this.node;
}
getDuration() {
return this.node.duration;
}
getCurrentTime() {
return this.node.currentTime;
}
startPositionTracking(onTime) {
if (!this.node) {
return;
}
this.stopPositionTracking();
const sub = this.emitter.addAudioEventListener('positionChanged', event => {
onTime(event.value);
});
this.node.onPositionChanged = sub.subscriptionId;
}
stopPositionTracking() {
if (this.node) {
this.node.onPositionChanged = '0';
}
}
resetNodeAndSubscriptions() {
if (this.node) {
this.node.onPositionChanged = '0';
this.node.onEnded = '0';
this.node.disconnect(undefined);
}
}
}
//# sourceMappingURL=AudioFileSourceNode.js.map