首页 > 解决方案 > 播放/暂停 HTML 音频在 iOS 上不起作用

问题描述

我已经通过 JS 在我的网站中实现了一个带有播放/暂停按钮的 html 音频。适用于除 iOS 之外的所有桌面和移动设备。

这不是自动播放,所以我认为它不会破坏任何 iOS 政策?

这是我正在使用的代码。

PS:尝试在stackoverflow上搜索,但没有发现任何有用的东西。

<style>
 #demo {
    width: 60px;
    height: 60px;
    outline: none;
       }
</style>

<button onclick="Play()" id="demo"></button>

<audio id="player" preload="auto">
 <source src="https://s3.eu-central-1.amazonaws.com/radmitry/Yoshio_Chino-We_Pray_For_Japan.mp3">
</audio>

<script>
    var audio = document.getElementById("player");
    audio.volume = 0.5;
    var butn = document.getElementById("demo");
    butn.style.background = "url(https://uploads-ssl.webflow.com/5acdf398c6f3e7252e9a31b9/5ad9f901d9a651ff58dad03b_play.svg) no-repeat";

    function Play() {
       if(audio.paused) {
               audio.play();
               butn.style.background = "url(https://uploads-ssl.webflow.com/5acdf398c6f3e7252e9a31b9/5ad9f8b88fb8e56097aa04ca_pause.svg) no-repeat";
    }

    else {
              audio.pause();
               butn.style.background = "url(https://uploads-ssl.webflow.com/5acdf398c6f3e7252e9a31b9/5ad9f901d9a651ff58dad03b_play.svg) no-repeat";
              }
      }

</script>

标签: javascriptiosiphonehtml5-audiobackground-music

解决方案


推荐阅读