mirror of
https://github.com/zulip/zulip.git
synced 2025-11-17 20:41:46 +00:00
notifications: Handle exception when trying to play audio.
Safari denies user from playing audio without an interactive trigger like a button by default. So, when user received a notification in Zulip via Safari, it triggers an error when trying to play the notification sound. Our goal for this commit is to simply handle the error.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import $ from "jquery";
|
||||
|
||||
import * as blueslip from "./blueslip";
|
||||
import * as keydown_util from "./keydown_util";
|
||||
|
||||
// Add functions to this that have no non-trivial
|
||||
@@ -75,3 +76,21 @@ export function parse_html(html: string): DocumentFragment {
|
||||
template.innerHTML = html;
|
||||
return template.content;
|
||||
}
|
||||
|
||||
/*
|
||||
* Handle permission denied to play audio by the browser.
|
||||
* This can happen due to two reasons: user denied permission to play audio
|
||||
* unconditionally and browser denying permission to play audio without
|
||||
* any interactive trigger like a button. See
|
||||
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/play for more details.
|
||||
*/
|
||||
export async function play_audio(elem: HTMLVideoElement): Promise<void> {
|
||||
try {
|
||||
await elem.play();
|
||||
} catch (error) {
|
||||
if (!(error instanceof DOMException)) {
|
||||
throw error;
|
||||
}
|
||||
blueslip.debug(`Unable to play audio. ${error.name}: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user