Solve the problem that html cannot automatically play sound and video under the Apple Safari browser - real-time voice call function [only customer service]...

When implementing the real-time voice call function in my customer service system, if I want to automatically play audio and video streams, I encountered problems on Apple devices.

Safari does not allow sounds to play automatically in the background by default. This is for user experience and privacy reasons, to avoid automatically playing sounds without the user realizing it.

The solution is

Safari browser for iOS 11 and above. When the user has had at least one interaction, the following two properties can be set, mutedand playsinlinethe property . Then set the dynamic js to play the sound automatically

<audio id="myAudio" src="audio.mp3" muted playsinline></audio>

Then dynamically set the properties in js

     var myAudio = document.getElementById('myAudio');
      myAudio.muted = false;  // 将 muted 属性设置为 false
      myAudio.autoplay = true;  // 将 autoplay 属性设置为 true
      myAudio.play();  // 播放音频

so

After the page has at least one interaction, you can make the sound play automatically on the Apple device

Guess you like

Origin blog.csdn.net/taoshihan/article/details/129435037