Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Prevent Element appearing in system media controls #10995

Merged
merged 27 commits into from
Jul 4, 2024
Merged
Changes from 5 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
5834398
Use WebAudio API to play notification sound
SuperKenVery May 26, 2023
249cfb7
Run prettier
SuperKenVery May 26, 2023
e19a8bf
Chosse from mp3 and ogg
SuperKenVery May 30, 2023
9e588e5
Run prettier
SuperKenVery Jun 17, 2023
17e0a9e
Merge branch 'develop' of github.com:matrix-org/matrix-react-sdk into…
SuperKenVery Jun 17, 2023
1f2ba5f
Merge branch 'develop' of github.com:matrix-org/matrix-react-sdk into…
SuperKenVery Jun 26, 2023
495dd5f
Use WebAudioAPI everywhere
SuperKenVery Jun 27, 2023
0819b11
Run prettier
SuperKenVery Jun 27, 2023
17cd18b
Eliminate a stupid error
SuperKenVery Jun 27, 2023
a37d710
Merge branch 'develop' into superkenvery/webaudioapi
richvdh Jun 20, 2024
52dc8df
Merge branch 'develop' into superkenvery/webaudioapi
t3chguy Jun 20, 2024
fd73da5
Merge branch 'develop' of github.com:matrix-org/matrix-react-sdk into…
t3chguy Jul 4, 2024
273d3de
Iterate
t3chguy Jul 4, 2024
13ba04b
Merge branch 'develop' into superkenvery/webaudioapi
t3chguy Jul 4, 2024
d214a6d
Update setupManualMocks.ts
t3chguy Jul 4, 2024
2ca3667
Merge branch 'superkenvery/webaudioapi' of github.com:SuperKenVery/ma…
t3chguy Jul 4, 2024
6357690
Iterate
t3chguy Jul 4, 2024
6a8e677
delint
t3chguy Jul 4, 2024
f0884ea
Iterate
t3chguy Jul 4, 2024
08c4fe1
Iterate
t3chguy Jul 4, 2024
32257a4
Merge remote-tracking branch 'SuperKenVery/superkenvery/webaudioapi' …
t3chguy Jul 4, 2024
099c1c5
mocks
t3chguy Jul 4, 2024
a9971aa
mocks
t3chguy Jul 4, 2024
559afe3
Iterate
t3chguy Jul 4, 2024
8a3ea49
Iterate
t3chguy Jul 4, 2024
5872591
Simplify
t3chguy Jul 4, 2024
226e3d4
covg
t3chguy Jul 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 42 additions & 23 deletions src/Notifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ class NotifierClass {
private toolbarHidden?: boolean;
private isSyncing?: boolean;

// Cache the sounds loaded
private audioContext: AudioContext = new AudioContext();
private sounds: Record<string, AudioBuffer> = {};

public notificationMessageForEvent(ev: MatrixEvent): string | null {
const msgType = ev.getContent().msgtype;
if (msgType && msgTypeHandlers.hasOwnProperty(msgType)) {
Expand Down Expand Up @@ -213,36 +217,51 @@ class NotifierClass {
};
}

private async createAudioSourceForRoom(roomId: string): Promise<AudioBufferSourceNode> {
const audio = await this.getSoundBufferForRoom(roomId);
const source = this.audioContext.createBufferSource();
source.buffer = audio;
source.connect(this.audioContext.destination);
return source;
}

private async getSoundBufferForRoom(roomId: string): Promise<AudioBuffer> {
const sound = this.getSoundForRoom(roomId);

const audioElement = document.createElement("audio");
let format = "";
if (audioElement.canPlayType("audio/mpeg")) {
format = "mp3";
} else if (audioElement.canPlayType("audio/ogg")) {
format = "ogg";
} else {
console.log("Browser doens't support mp3 or ogg");
// Will probably never happen. If happened, format="" and will fail to load audio. Who cares...
}

const url = sound ? sound["url"] : "media/message." + format;

if (this.sounds.hasOwnProperty(url)) {
return this.sounds[url];
}

const response = await fetch(url);
const buffer = await response.arrayBuffer();
const audio = await this.audioContext.decodeAudioData(buffer);
this.sounds[url] = audio;
return audio;
}

// XXX: Exported for tests
public async playAudioNotification(ev: MatrixEvent, room: Room): Promise<void> {
const cli = MatrixClientPeg.get();
if (localNotificationsAreSilenced(cli)) {
return;
}

const sound = this.getSoundForRoom(room.roomId);
logger.log(`Got sound ${(sound && sound.name) || "default"} for ${room.roomId}`);

try {
const selector = document.querySelector<HTMLAudioElement>(
sound ? `audio[src='${sound.url}']` : "#messageAudio",
);
let audioElement = selector;
if (!audioElement) {
if (!sound) {
logger.error("No audio element or sound to play for notification");
return;
}
audioElement = new Audio(sound.url);
if (sound.type) {
audioElement.type = sound.type;
}
document.body.appendChild(audioElement);
}
await audioElement.play();
} catch (ex) {
logger.warn("Caught error when trying to fetch room notification sound:", ex);
}
// Play notification sound here
const source = await this.createAudioSourceForRoom(room.roomId);
source.start();
}

public start(): void {
Expand Down