-
-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2449 from GuoLei1990/feat/audio
feat: support background audio
- Loading branch information
Showing
21 changed files
with
564 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Engine } from "../Engine"; | ||
import { ReferResource } from "../asset/ReferResource"; | ||
|
||
/** | ||
* Audio Clip. | ||
*/ | ||
export class AudioClip extends ReferResource { | ||
private _audioBuffer: AudioBuffer | null = null; | ||
|
||
/** Name of clip. */ | ||
name: string; | ||
|
||
/** | ||
* Number of discrete audio channels. | ||
*/ | ||
get channels(): number { | ||
return this._audioBuffer.numberOfChannels; | ||
} | ||
|
||
/** | ||
* Sample rate, in samples per second. | ||
*/ | ||
get sampleRate(): number { | ||
return this._audioBuffer.sampleRate; | ||
} | ||
|
||
/** | ||
* Duration, in seconds. | ||
*/ | ||
get duration(): number { | ||
return this._audioBuffer.duration; | ||
} | ||
|
||
constructor(engine: Engine, name: string = "") { | ||
super(engine); | ||
this.name = name; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
_getAudioSource(): AudioBuffer { | ||
return this._audioBuffer; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
_setAudioSource(value: AudioBuffer): void { | ||
this._audioBuffer = value; | ||
} | ||
|
||
protected override _onDestroy(): void { | ||
super._onDestroy(); | ||
this._audioBuffer = null; | ||
this.name = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* @internal | ||
* Audio Manager. | ||
*/ | ||
export class AudioManager { | ||
private static _context: AudioContext; | ||
private static _gainNode: GainNode; | ||
private static _isResuming = false; | ||
|
||
static getContext(): AudioContext { | ||
let context = AudioManager._context; | ||
if (!context) { | ||
AudioManager._context = context = new window.AudioContext(); | ||
|
||
// Safari can't resume audio context without element interaction | ||
document.addEventListener("pointerdown", AudioManager._tryResume, true); | ||
document.addEventListener("touchend", AudioManager._tryResume, true); | ||
document.addEventListener("touchstart", AudioManager._tryResume, true); | ||
} | ||
return context; | ||
} | ||
|
||
static getGainNode(): GainNode { | ||
let gainNode = AudioManager._gainNode; | ||
if (!AudioManager._gainNode) { | ||
AudioManager._gainNode = gainNode = AudioManager.getContext().createGain(); | ||
gainNode.connect(AudioManager.getContext().destination); | ||
} | ||
return gainNode; | ||
} | ||
|
||
static isAudioContextRunning(): boolean { | ||
if (AudioManager.getContext().state !== "running") { | ||
console.warn("The AudioContext is not running and requires user interaction, such as a click or touch."); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
private static _tryResume(): void { | ||
if (AudioManager._context.state !== "running") { | ||
if (AudioManager._isResuming) { | ||
return; | ||
} | ||
|
||
AudioManager._isResuming = true; | ||
AudioManager._context.resume().then(() => { | ||
AudioManager._isResuming = false; | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.