mirror of
https://github.com/Tonejs/Tone.js
synced 2025-01-14 12:53:59 +00:00
Use addEventListener for start and stop
This commit is contained in:
parent
15d5584f97
commit
c7aed6199f
1 changed files with 19 additions and 3 deletions
|
@ -102,16 +102,32 @@ export class Recorder extends ToneAudioNode<RecorderOptions> {
|
|||
}
|
||||
}
|
||||
|
||||
start(): this {
|
||||
async start() {
|
||||
assert(this.state !== "started", "Recorder is already started");
|
||||
const startPromise = new Promise(done => {
|
||||
const handleStart = () => {
|
||||
this._recorder.removeEventListener("start", handleStart, false);
|
||||
|
||||
done();
|
||||
};
|
||||
|
||||
this._recorder.addEventListener("start", handleStart, false);
|
||||
});
|
||||
|
||||
this._recorder.start();
|
||||
return this;
|
||||
return await startPromise;
|
||||
}
|
||||
|
||||
async stop(): Promise<Blob> {
|
||||
assert(this.state !== "stopped", "Recorder is not started");
|
||||
const dataPromise: Promise<Blob> = new Promise(done => {
|
||||
this._recorder.ondataavailable = (e: BlobEvent) => done(e.data);
|
||||
const handleData = (e: BlobEvent) => {
|
||||
this._recorder.removeEventListener("dataavailable", handleData, false);
|
||||
|
||||
done(e.data);
|
||||
};
|
||||
|
||||
this._recorder.addEventListener("dataavailable", handleData, false);
|
||||
});
|
||||
this._recorder.stop();
|
||||
return await dataPromise;
|
||||
|
|
Loading…
Reference in a new issue