Skip to content

Commit dc05f20

Browse files
committed
optional mute on visibility hidden
1 parent a8c943d commit dc05f20

File tree

8 files changed

+150
-13
lines changed

8 files changed

+150
-13
lines changed

dist/index.js

Lines changed: 43 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.umd.js

Lines changed: 43 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.umd.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/library/core.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export declare class PlayerCore {
2727
protected _stopOnReset: boolean;
2828
protected _postMuteVolume: number;
2929
protected _isMuted: boolean;
30+
protected _visibilityAutoMute: boolean;
3031
readonly WHERE_IN_QUEUE_AT_END: string;
3132
readonly WHERE_IN_QUEUE_AT_START: string;
3233
readonly WHERE_IN_QUEUE_AFTER_CURRENT: string;
@@ -76,4 +77,9 @@ export declare class PlayerCore {
7677
getAudioGraph(): Promise<IAudioGraph>;
7778
setAudioContext(customAudioContext: AudioContext): void;
7879
getAudioContext(): Promise<AudioContext>;
80+
setAutoCreateContextOnFirstTouch(autoCreate: boolean): void;
81+
getAutoCreateContextOnFirstTouch(): boolean;
82+
setVisibilityAutoMute(visibilityAutoMute: boolean): void;
83+
getVisibilityAutoMute(): boolean;
84+
protected _handleVisibilityChange(): void;
7985
}

examples/simple-player/client/src/bootstrap.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ let secondSound = player.addSoundToQueue(secondSoundAttributes);
9090

9191
console.log(secondSound);
9292

93+
// turn un feature: if page becomes invisible auto mute, unmute when page becomes visible again
94+
player.setVisibilityAutoMute(true);
95+
9396
// halt the audio hardware access temporarily to reduce CPU and battery usage
9497
/*player.getAudioContext().then((audioContext) => {
9598
audioContext.suspend();
@@ -104,4 +107,4 @@ console.log(secondSound);
104107
//player.play();
105108

106109
// play next song
107-
//player.play(player.PLAY_SOUND_NEXT);
110+
//player.play(player.PLAY_SOUND_NEXT);

src/library/audio.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,8 @@ class PlayerAudio {
302302
// https://developer.mozilla.org/en-US/docs/Web/API/AudioDestinationNode
303303
this._audioGraph.gainNode.connect(audioContext.destination);
304304

305-
// update volume (gainValue)
306-
const gainValue = this._volume / 100;
307-
308-
this._changeGainValue(gainValue);
305+
// update volume
306+
this.changeVolume(this._volume);
309307

310308
// resolve
311309
resolve(this._audioGraph);
@@ -400,6 +398,11 @@ class PlayerAudio {
400398

401399
this._volume = volume;
402400

401+
// update volume (gainValue)
402+
const gainValue = this._volume / 100;
403+
404+
this._changeGainValue(gainValue);
405+
403406
}
404407

405408
protected _changeGainValue(gainValue: number): void {

src/library/core.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ export class PlayerCore {
4646
protected _postMuteVolume: number = null;
4747
// is muted?
4848
protected _isMuted: boolean = false;
49+
// automatically mute if visibility changes to invisible
50+
protected _visibilityAutoMute: boolean = false;
4951

5052
// constants
5153
readonly WHERE_IN_QUEUE_AT_END: string = 'append';
@@ -878,4 +880,47 @@ export class PlayerCore {
878880

879881
}
880882

883+
public setAutoCreateContextOnFirstTouch(autoCreate: boolean): void {
884+
this._playerAudio.setAutoCreateContextOnFirstTouch(autoCreate);
885+
}
886+
887+
public getAutoCreateContextOnFirstTouch(): boolean {
888+
return this._playerAudio.getAutoCreateContextOnFirstTouch();
889+
}
890+
891+
public setVisibilityAutoMute(visibilityAutoMute: boolean): void {
892+
893+
this._visibilityAutoMute = visibilityAutoMute;
894+
895+
if (visibilityAutoMute) {
896+
document.addEventListener('visibilitychange', this._handleVisibilityChange.bind(this), false);
897+
} else {
898+
document.removeEventListener('visibilitychange', this._handleVisibilityChange.bind(this), false);
899+
}
900+
901+
}
902+
903+
public getVisibilityAutoMute(): boolean {
904+
return this._visibilityAutoMute;
905+
}
906+
907+
protected _handleVisibilityChange() {
908+
909+
let hiddenKeyword: string;
910+
911+
if (typeof document.hidden !== 'undefined') { // Opera 12.10 and Firefox 18 and later support
912+
hiddenKeyword = 'hidden';
913+
} else if (typeof (document as any).msHidden !== 'undefined') {
914+
hiddenKeyword = 'msHidden';
915+
} else if (typeof (document as any).webkitHidden !== 'undefined') {
916+
hiddenKeyword = 'webkitHidden';
917+
}
918+
919+
if ((document as any)[hiddenKeyword]) {
920+
this.mute();
921+
} else {
922+
this.unMute();
923+
}
924+
}
925+
881926
}

0 commit comments

Comments
 (0)