Updated on 2024-11-15 GMT+08:00

Listening on Autoplay Errors

Sample code of listening on autoplay errors:

<!DOCTYPE html>
<html>
  <head>
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"
    />
    <title>Audio playback blocking demo</title>
    <style>
      html, body {
        width: 100%;
        height: 100%;
        padding: 0;
        margin: 0;
      }
      .preview_player {
        width: 1280px;
        height: 720px;
        margin: 20px auto;
        border: 1px solid #ddd;
        position: relative;
      }
      .preview_player_el {
        width: 100%;
        height: 100%;
      }
      .unmute_mask {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.5);
        text-align: center;
        display: none;
      }
      .unmute_btn {
        display: inline-block;
        padding: 10px;
        color: rgb(50, 250, 50);
        border: 1px solid rgb(50, 250, 50);
        background: rgba(50, 250, 50, 0.3);
        transform: translateY(400px);
        cursor: pointer;
      }
    </style>
    <script type="text/javascript" src="./sdk/HWLLSPlayer.js"></script>
  </head>
  <body>
    <div class="preview_player">
      <div id="preview" class="preview_player_el"></div>
      <div class="unmute_mask" id="unmute-mask">
        <div class="unmute_btn" onclick="replayAction()">Click to unmute</div>
      </div>
    </div>
    <script type="text/javascript">
      const playClient = HWLLSPlayer.createClient()
      const unmuteMask = document.getElementById('unmute-mask')
      const replayAction = function () {
        playClient.replay()
        unmuteMask.style.display = 'none'
      }
      // Handling registered audio playback blocking
      const initEvent = function () {
        playClient.on('Error', (resp) => {
          // Error message on audio autoplay blocking
          if (resp.errCode === 51000000) {
            // Add the button unmute on the page.
            unmuteMask.style.display = 'block'
          }
        })
      }
      initEvent()
      // Video playback
      const streamUrl = 'your stream url'
      const options = {
        elementId: 'preview',
        autoPlay: true,
      }
      playClient.startPlay(streamUrl, options)
    </script>
  </body>
</html>