【问题标题】:Play sound file in a web-page in the background在后台播放网页中的声音文件
【发布时间】:2012-11-15 16:50:48
【问题描述】:

我想在后台播放我的网页上的声音文件(不希望出现媒体播放器 UI),我的网站将在 Fire-Fox 上运行,我使用了 Embed 元素并设置隐藏属性为真<embed name="myMusic" src="Masgon.mp3" type="audio/midi" autostart="false" Hidden="true" loop="true"></embed> 问题是没有声音播放,除非我在这种情况下删除了隐藏属性,播放声音文件并出现媒体播放器 UI,但我不希望那样。

【问题讨论】:

  • 文件类型为audio/mp3,而type属性的值为“audio/midi”。 midi 文件具有 .mid 或 .midi 扩展名。

标签: html firefox


【解决方案1】:
<audio src="/music/good_enough.mp3">
<p>If you are reading this, it is because your browser does not support the audio element.     </p>
</audio>

如果你想要控制

<audio src="/music/good_enough.mp3" controls>
<p>If you are reading this, it is because your browser does not support the audio element.</p>
</audio>

也使用嵌入

<embed src="/music/good_enough.mp3" width="180" height="90" loop="false" autostart="false" hidden="true" />

【讨论】:

  • 有关 Firefox 问题 stackoverflow.com/questions/6529645/… 的信息。嵌入应该可以工作,但如果不使用 Object
  • 这并没有回答“除非我删除了隐藏属性”的问题,解决方案是使用 width=0 和 height=0
【解决方案2】:
<audio src="/music/good_enough.mp3" autoplay>
<p>If you are reading this, it is because your browser does not support the audio element.     </p>
<embed src="/music/good_enough.mp3" width="180" height="90" hidden="true" />
</audio>

对我来说很好用。

【讨论】:

    【解决方案3】:

    虽然现在评论可能为时已晚,但这是解决诸如您的问题的工作代码。

    <div id="player">
        <audio autoplay hidden>
         <source src="link/to/file/file.mp3" type="audio/mpeg">
                    If you're reading this, audio isn't supported. 
        </audio>
    </div>
    

    【讨论】:

    • 只需从您的代码中删除 controls 即可完美
    • @IvijanStefanStipić 真的!如果音频是 hidden (并且在 autoplay 上),则设置 controls 是没有意义的。
    【解决方案4】:
    <audio controls autoplay loop>
      <source src="path/your_song.mp3" type="audio/ogg">
      <embed src="path/your_song.mp3" autostart="true" loop="true" hidden="true"> 
    </audio>
    

    [ps。用文件夹和歌曲名称替换“path/your_song.mp3”,例如。 “music/samplemusic.mp3”或“media/bgmusic.mp3”等

    【讨论】:

    • 这个最适合自动播放,其他方法会导致错误 "play() failed because the user didn't interact with the document "
    【解决方案5】:

    我通过删除type 属性解决了这个问题:

    <embed name="myMusic" loop="true" hidden="true" src="Music.mp3"></embed>
    

    当然不是最干净的方式。

    如果您使用的是 HTML5: Firefox 不支持 MP3。 Wav 和 Ogg 是。 在这里,您可以找到哪些浏览器支持哪种类型的音频的概述: http://www.w3schools.com/html/html5_audio.asp

    【讨论】:

    • 我用 .wave 文件试了一下,还是不行,和上面的场景一样
    【解决方案6】:

    如果您不想显示控件,请尝试此代码

    <audio  autoplay>
     <source src="song.ogg"  type="audio/ogg">
    Your browser does not support the audio element.
    </audio>
    

    【讨论】:

      【解决方案7】:

      如果你想要它只是正常的:

      <audio src="sus.mp3">
          <script>alert("Your browser doesn't support the Audio tag!")</script>
      </audio>`
      

      如果您希望它在加载时自动启动:(chrome 不支持)

      <audio src="sus.mp3" autostart="true">
          <script>alert("Your browser doesn't support the Audio tag!")</script>
      </audio>
      

      如果你想要控制

      <audio src="sus.mp3" autostart="true" controls>
          <script>alert("Your browser doesn't support the Audio tag!")</script>
      </audio>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-05
        • 1970-01-01
        • 2016-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-01
        相关资源
        最近更新 更多