简体   繁体   English

ios 上的音频不能正常工作,只能在第一次工作

[英]Audio on ios does not work properly, it only works the first time

I have a problem with playing audio on IOS devices... the code I use is this to activate/deactivate the sound:我在 IOS 设备上播放音频时遇到问题...我使用的代码是用于激活/停用声音:

 <audio id="audio">
    <source src="sounds/fart.mp3" type="audio/mpeg" />
    Your browser does not support the audio element.
 </audio>
 <button id="audio-btn" class="audio-btn" onclick="toggleAudio()">
    <img id="audio-icon" src="img/sound-off.webp" alt="audio_play/pause" />
 </button>



let audioIconPlay = false;

 function toggleAudio() {
   const audioIcon = document.getElementById('audio-icon');
   audio.autoplay = true;
   audioIconPlay = !audioIconPlay;
   if (audioIconPlay) {
     audioIcon.src = 'img/sound-on.webp';
   } else {
     audioIcon.src = 'img/sound-off.webp';
   }
 }

And this when I click the button to play the audio:当我单击按钮播放音频时:

 <button
  onmousedown="playMyAudio()"
  onmouseup="playMyAudio()"
 >
  Fart
 </button>

 function playMyAudio() {
   if (audioIconPlay && audio.paused) {
     audio.play();
   } else {
     audio.pause();
     audio.currentTime = 0;
   }
 }

Ps.: I did it all on onTouch as well Ps.:我也是在onTouch上完成的

Now, on android devices it works, every click plays the audio, while on iOS it works only on the first click?现在,在 android 设备上它可以工作,每次点击都会播放音频,而在 iOS 上它只能在第一次点击时工作? There is a solution: I tried this: Allow audio.play() on safari for chat app but it does not work.有一个解决方案:我试过这个: Allow audio.play() on safari for chat app但它不起作用。

When you click the button, the function is executed and the sound is played.单击按钮时,执行 function 并播放声音。 But at the mouseup event, the function is executed again and the sound is then stopped.但是在 mouseup 事件中,再次执行 function 并停止声音。 Have you tried it with an onclick, so the function will run only once?您是否尝试过使用 onclick,所以 function 只能运行一次? Or should the sound only be played while pressing the button?还是应该只在按下按钮时播放声音?

By the way, the equivalent for onmousedown on touchscreen devices is ontouchstart, and the equivalent of onmouseup is ontouchend.顺便说一句,在触摸屏设备上,onmousedown 的等价物是 ontouchstart,onmouseup 的等价物是 ontouchend。 But I suggest to use onclick.但我建议使用 onclick。

I did some tests, in practice it works but not immediate as with android.... My problem is that the audio I use is too short, it lasts a second.我做了一些测试,在实践中它可以工作,但不像 android 那样立即生效......我的问题是我使用的音频太短,持续一秒钟。 maybe even less.., So on the iphone it doesn't have the time to play such a short sound and it seems that it plays it only once but in reality it always does it only that the other times it is not heard as it does not have the time necessary to stop and play it all over again, I tried to insert a longer audio and it works.也许甚至更少..,所以在 iphone 上它没有时间播放这么短的声音,而且它似乎只播放一次,但实际上它总是只在其他时间听不到它没有时间停下来重新播放,我尝试插入更长的音频,它可以工作。 the problem is that if I click quickly several times on the button I do not get the same result as I have on android.问题是,如果我在按钮上快速单击几次,我得到的结果与在 android 上的结果不同。 i,e, stop the audio, reset the time and play it again.即,停止音频,重置时间并再次播放。 I have no idea of the because but it seems that iphone is delayed and unable to do it, while on android devices yes.我不知道,因为但似乎 iphone 被延迟并且无法做到,而在 android 设备上是的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM