-
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathAudioPlayer.ts
More file actions
163 lines (137 loc) · 4.05 KB
/
AudioPlayer.ts
File metadata and controls
163 lines (137 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import type {
AudioBuffer,
AudioBufferSourceNode,
} from 'react-native-audio-api';
import {
AudioContext,
PlaybackNotificationManager,
GainNode,
} from 'react-native-audio-api';
class AudioPlayer {
private readonly audioContext: AudioContext;
private sourceNode: AudioBufferSourceNode | null = null;
private audioBuffer: AudioBuffer | null = null;
private volumeNode: GainNode | null = null;
private isPlaying: boolean = false;
private currentElapsedTime: number = 0;
private playbackRate: number = 1;
private volume: number = 1;
private onPositionChanged: ((offset: number) => void) | null = null;
constructor() {
this.audioContext = new AudioContext();
}
play = async () => {
if (this.isPlaying) {
console.warn('Audio is already playing');
return;
}
if (!this.audioBuffer) {
console.warn('Audio buffer is not loaded');
return;
}
this.isPlaying = true;
PlaybackNotificationManager.show({
state: 'playing',
});
if (this.audioContext.state === 'suspended') {
await this.audioContext.resume();
}
this.sourceNode = this.audioContext.createBufferSource({
pitchCorrection: true,
});
this.sourceNode.buffer = this.audioBuffer;
this.sourceNode.playbackRate.value = this.playbackRate;
const volume1 = this.audioContext.createGain();
const volume2 = this.audioContext.createGain();
volume1.gain.value = 0.5;
volume2.gain.value = 0.5;
this.volumeNode = this.audioContext.createGain();
this.volumeNode.gain.value = this.volume;
this.sourceNode
.connect(this.volumeNode)
.connect(this.audioContext.destination);
this.sourceNode.onPositionChangedInterval = 1000;
this.sourceNode.onPositionChanged = (event) => {
PlaybackNotificationManager.show({
elapsedTime: this.currentElapsedTime,
});
this.currentElapsedTime = event.value;
if (this.onPositionChanged) {
this.onPositionChanged(
this.currentElapsedTime / this.audioBuffer!.duration
);
}
};
this.sourceNode.start(
this.audioContext.currentTime,
this.currentElapsedTime
);
};
pause = async () => {
if (!this.isPlaying) {
console.warn('Audio is not playing');
return;
}
this.sourceNode?.stop(this.audioContext.currentTime);
await this.audioContext.suspend();
PlaybackNotificationManager.show({
state: 'paused',
elapsedTime: this.currentElapsedTime,
});
this.isPlaying = false;
};
seekBy = (seconds: number) => {
this.sourceNode?.stop(this.audioContext.currentTime);
this.currentElapsedTime += seconds;
if (this.currentElapsedTime < 0) {
this.currentElapsedTime = 0;
} else if (this.currentElapsedTime > this.getDuration()) {
this.currentElapsedTime = this.getDuration();
}
PlaybackNotificationManager.show({
elapsedTime: this.currentElapsedTime,
});
if (this.isPlaying) {
this.isPlaying = false;
this.play();
}
};
loadBuffer = async (asset: string | number) => {
const buffer = await this.audioContext.decodeAudioData(asset);
if (buffer) {
this.audioBuffer = buffer;
this.playbackRate = 1;
}
};
reset = async () => {
if (this.sourceNode) {
this.sourceNode.onEnded = null;
this.sourceNode.onPositionChanged = null;
this.sourceNode.stop(this.audioContext.currentTime);
}
this.audioBuffer = null;
this.sourceNode = null;
this.currentElapsedTime = 0;
this.playbackRate = 1;
this.isPlaying = false;
await this.audioContext.suspend();
};
setOnPositionChanged = (
callback: null | ((offset: number) => void) = null
) => {
this.onPositionChanged = callback;
};
getDuration = (): number => {
return this.audioBuffer?.duration ?? 0;
};
getElapsedTime = (): number => {
return this.currentElapsedTime;
};
setVolume = (volume: number) => {
this.volume = volume;
if (this.volumeNode) {
this.volumeNode.gain.value = volume;
}
};
}
export default new AudioPlayer();