Why can't JavaScript .play() audio files on iPhone safari?

后端 未结 4 1860
慢半拍i
慢半拍i 2020-12-08 06:51

I\'ve got a JavaScript web app working that plays some audio periodically like this:

var SOUND_SUCCESS = new Audio(\'success.mp3\');
SOUND_SUCCESS.play();
         


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-08 07:26

    To add to xingliang cai's response, here's a code sample I got to work for me:

    const soundEffect = new Audio();
    
    // onClick of first interaction on page before I need the sounds
    soundEffect.play();
    
    // later on when you actually want to play a sound at any point without user interaction
    soundEffect.src = 'path/to/file.mp3';
    soundEffect.play();
    

提交回复
热议问题