有没有办法在Web应用程序中以编程方式播放音频

有没有办法在Web应用程序中以编程方式播放音频

本文介绍了有没有办法在Web应用程序中以编程方式播放音频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个网络应用程序,它涉及在新订单进入应用程序时播放声音。在桌面上,一切正常。

I am building a web app which involves playing a sound when new orders have come into the app. On desktop this all works fine.

但是,在iOS上,音频根本不播放。在一些我发现iOS在播放音频方面有局限性。

However, on iOS, the audio does not play at all. After some investigation I have discovered that iOS has limitations on playing audio.


  • 仅限用户互动后

  • 仅在页面处于活动状态时才显示

  • 仅在电话屏幕打开时

  • 无法保留屏幕关闭,所以你的音频将在第一首歌后停止。

  • Only after user interaction
  • Only while the page is active and in the fore
  • Only while the phone screen is on
  • There’s no way to keep the screen from turning off, so your audio will stop after the first song.

有没有办法让用户允许音频以编程方式播放?

Is there a way to ask user permission to allow audio to be played programmatically?

推荐答案

是的,这是真的!用户请求之前没有音频缓冲区。
使用一些弹出窗口或任何适合眼睛的动画来吸引用户点击
一个是你初始播放,同时下一个代码行暂停,你将初始化缓冲,然后你可以编程播放音频。

Yes it is true ! No audio buffer before user request.Use some popup or any animation nice for eye to attract user for tap .One's you init play and in same time next code line pause you will initiale buffering after that you can play audio programatically.

在我的记忆中... android和iOS没有相同的行为,它是关于我们可以通过这个技巧(单击或触摸)启动缓冲区的最大音频数量。您需要对其进行测试。

in my memory ... android and iOS don't have a same behavior , it is about max number of audios that we can start buffer with this trick (one click or touch). You will need to test it.

如果您希望同一音频在同一时间播放更多次:
最好使用以下方法创建更多音频html标签相同的来源。我是js的粉丝,但这次最好有html格式的html音频标签。

If you want same audio to play more times in same moment :it is better to create more audio html tags with the same source . I 'm a fan of js but this time it is better to have html audio tags in html form.

    var EXE_JUST_ONE_TIME = false;

    document.addEventListener("touchstart" , function(e) {

    if (EXE_JUST_ONE_TIME == false){

     EXE_JUST_ONE_TIME = true;

     document.getElementById("LaserShot").play(); // need for play pause just for buffering start
     document.getElementById("LaserShot").pause();
     // now you can play programmability from js
     document.getElementById("LaserShot_CLONE").play();
     document.getElementById("LaserShot_CLONE").pause();

    }
    else if(EXE_JUST_ONE_TIME = true){

     document.getElementById("MakeReadyotherAudio1").play();
     document.getElementById("MakeReadyotherAudio1").pause();

     EXE_JUST_ONE_TIME = 'NOMORE'

   }


    }

这篇关于有没有办法在Web应用程序中以编程方式播放音频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 01:00