Определение текущего контекста в отношении команды audio / video play () - PullRequest
0 голосов
/ 04 августа 2020

Итак, когда мы пытаемся, например, myVideoElement.play(), мы получаем эту ошибку в консоли:

Uncaught (in promise) DOMException: The play method is not allowed by the user agent or the platform in the current context, possibly because the user denied permission.

Возможно ли это в JavaScript чтобы определить или проверить, в каком контексте мы находимся, чтобы, например, предотвратить вызов play ()? Я не хочу, чтобы в консоли отображалось предупреждение или сообщение об ошибке.

1 Ответ

0 голосов
/ 04 августа 2020

Если вас беспокоит ошибка в консоли, вы можете просто ее поймать.

async function yourFunction()
{
     try
     {
           await myVideoElement.play();
           // if the script reaches here, it succeeded
           console.log("success");
     }
     catch(error)
     {
           // if it failed it will come here, but will not put an error in the console,
           // so you can handle it however you want or just ignore it and nothing will happen
     }
}

yourFunction()

Подробнее: https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/play

...