在 iOS 浏览器上将商品添加到购物车时播放声音

Play sound when item added to cart on iOS browser

我有一个使用 WooCommerce 的网站。 每次将商品添加到购物车时,我都想播放一声短促的声音。 我已经准备好 mp3 文件了。

我决定使用这个https://github.com/admsev/jquery-play-sound 它在 PC 上运行,但我在 iOS 浏览器上听不到声音。

我的脚本如下

<script>
window.onload = function() {
  var anchors = document.getElementsByTagName('a');
  for(var i = 0; i < anchors.length; i++) {
      var anchor = anchors[i];
      anchor.onclick = function() {
          $.playSound("<?php bloginfo('stylesheet_directory');?>/my_sound.mp3");
      }
  }
}</script>

我应该怎么做才能在 iOS 浏览器上播放声音? 有谁能帮帮我吗?

首先,我会将其从该操作中删除:

add_action( 'wp_enqueue_scripts', 'tap_sound' );

然后将其硬编码(用于检查目的)添加到页脚。

其次,我会使用 jQuery,它可以更干净。例子: https://webdev-il.blogspot.com/2017/10/trigger-audio-and-playing-sounds-with.html

最好改写脚本只用jQuery,更扎实:)

<script>
jQuery(document).ready(function(){
    jQuery('a').on('click', function(){
        jQuery.playSound("<?php echo get_bloginfo('stylesheet_directory');?>/my_sound.mp3");
    });
});
</script>

如果 iOS 仍有问题,请反馈。