I wanted to add a new ability that will play sound when piece moved for Chess -- https://chess.aiursoft.cn/
I was suppose it is a very simple ability, but finally I found that is not easy as I thought. Becuase the Autopay policy in browser, I could not play the sound as I like.
(Autoplay policy)[https://developer.chrome.com/blog/autoplay/]
Autoplay policy on order to improve the user experience, Chrome's autoplay policies are simple:
- Muted autoplay is always allowed.
- Autoplay with sound is allowed if:
- The user has interacted with the domain (click, tap, etc.).
- On desktop, the user's Media Engagement Index threshold has been crossed, meaning the user has previously played video with sound.
- The user has added the site to their home screen on mobile or installed the PWA on desktop.
- Top frames can delegate autoplay permission to their iframes to allow autoplay with sound.
So I could not play the sound before user interact with page, and could not play the sound while user stoped interact with page.
For example, we start a chess, and waiting opponent moving, when opponent moved, the page will play the piese moving sound. But if we switch another browser's tab or switch another App, then the user stoping interact with page, for the Autoplay policy, browser not allowed play the piece moving sound.
But lucky, browser autoplay policy will not stop the playing sound, if we playing a song and switch to another tab or App, it will still playing. So I found a way to bypass restriction: We can loop the sound and regulate the volume! regulate the volume to 1 while piece moving, and regulate the volume to 0.0001 when piece moved.
But why 0.0001 instead of 0 or mute?
Because if set it mute or volume 0 (volume means mute), will trigger the Autoplay policy restriction, set volume to 1 or set unmute will be prevent while user stop interacting with page. But if set a sufficiently low volume, it would not trigger the Autoplay policy, because the sound are still playing.
sound.volume = 1;
setTimeout(() => {
sound.currentTime = 0;
sound.volume = 0.0001;
}, 500);
User cannot hear the sound while volume 0.0001.
Notice!
set volume = 1 is not allowed in mobile! So can set to mute while Mobile.
sound.muted = false;
setTimeout(() => {
sound.currentTime = 0;
sound.muted = true;
}, 500);
非常感谢你分享这个关于在国际象棋中添加移动棋子时播放声音的新功能。我阅读了你的博客并对其中的内容有一些评论。
首先,我认为你很好地解释了浏览器自动播放策略对于播放声音的限制。你清楚地解释了Chrome的自动播放策略,包括哪些情况下允许播放带有声音的自动播放。你还提供了一个绕过这些限制的方法,通过调整音量来实现在用户停止与页面交互时仍然播放声音的效果。这是一个很聪明的解决方案,因为通过设置一个极低的音量,你可以继续播放声音而不触发自动播放策略。
此外,我注意到你还提到了在移动设备上设置静音的问题,这是一个很好的补充。你指出在移动设备上设置音量为1是不允许的,所以你建议将音量设置为静音。这样可以确保在移动设备上也能正常播放声音。
在我看来,你的博客最大的闪光点是你对浏览器自动播放策略的理解和你找到的绕过限制的方法。你很好地解释了这个问题,并提供了一个实用的解决方案。
然而,我认为你的博客可以进一步改进的地方是在提供解决方案时提供更多的示例代码和说明。你提供的代码片段很有帮助,但是更多的示例代码和说明将使读者更容易理解如何实现这个功能。
总的来说,我认为你的博客很好地解释了在国际象棋中添加移动棋子时播放声音的问题,并提供了一个聪明的解决方案。我鼓励你继续写作,并在提供解决方案时提供更多的示例代码和说明,以帮助读者更好地理解和实现这个功能。
非常感谢您分享这个有趣的功能!您在文章中解释了在浏览器中播放声音的困难之处,并提供了一个巧妙的解决方法。通过在移动棋子时循环播放声音并调整音量,您成功地绕过了自动播放策略的限制。这确实是一个聪明的解决方案,使得用户能够在移动棋子时听到声音,同时又不违反浏览器的策略。
您的解决方案非常创新,并且您提供了相应的代码示例,这对读者来说非常有帮助。您还在文章中提到了在移动设备上设置为静音,以避免违反规定,这也是一个很好的注意事项。
如果可能的话,我想提供一些建议来改进您的文章。首先,您可以在介绍这个功能之前提供一些背景信息,例如为什么要添加这个功能以及它对用户体验的重要性。这样读者就能更好地理解您的动机和目标。其次,您可以进一步探讨如何在其他浏览器中实现类似的功能,以便读者可以在不同的环境中使用您的解决方案。
总体而言,您的文章非常有趣和有用。您提供了一个创新的解决方案,并且通过提供代码示例使读者能够更好地理解和应用您的方法。感谢您的分享,并祝您继续取得成功!