Html html5 音频不在 chrome 中播放

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18906360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 13:36:00  来源:igfitidea点击:

html5 audio not playing in chrome

htmlaudiohtml5-audio

提问by user2636480

I have a very simple website with an html5 video and a html5 audio that is triggered on and off with two simple buttons. the audio all works gloriously fine in Safari, but will not work in Chrome.

我有一个非常简单的网站,其中包含一个 html5 视频和一个 html5 音频,通过两个简单的按钮触发和关闭。音频在 Safari 中运行良好,但在 Chrome 中不起作用。

my website is www.rossfraser.co (that is not .com)

我的网站是 www.rossfraser.co(不是 .com)

my code for the audio is this:

我的音频代码是这样的:

<div align="center">
<audio>
  <source src="audio/site-jingle.mp3" type="audio/mpeg">
</audio>
</div>

<audio id="player" src="audio/site-jingle.mp3" type="audio/mpeg" loop></audio>
<div style="text-align:center">
    <button onclick="document.getElementById('player').play()">JINGLE</button>
    <button onclick="document.getElementById('player').pause()">NO JINGLE</button>
</div>

Any help would be GREATLY appreciated. Thanks!

任何帮助将不胜感激。谢谢!

回答by hectormr85

I think I have the solution. MP3 with 192kbps doesn't work.

我想我有解决办法。192kbps 的 MP3 不起作用。

MP3 with 128 works fine!!!

128 的 MP3 工作正常!!!

回答by Astro

Firstly see: If there are no java script conflicts. In my case jquery.mini.js was conflicting.

首先看:如果没有java脚本冲突。在我的情况下 jquery.mini.js 是冲突的。

The best cross-browser support worked for me was buzz.js:

对我来说最好的跨浏览器支持是 buzz.js:

<script src="buzz.js"></script>
<script>
var sound;
$(function() {
sound = new buzz.sound( "media/yourfilename", {
    formats: ['ogg', 'mp3']
}).setVolume(100).play();

$('#pause').click(function() {
    sound.pause();
});
$('#play').click(function() {
    sound.play();
    });
});    
</script>

For buttons use this:

对于按钮使用这个:

<button id="pause">Pause</button>
<button id="play">Play</button>

or

或者

<audio controls id="a1" preload="auto" autoplay  src="media/page1"></audio>

回答by Nova Pixels Designs

I think Google Chrome has something against underscores. Taking out the underscores in my song file solved the problem in it's entirety

我认为谷歌浏览器有一些反对下划线的东西。去掉我的歌曲文件中的下划线完全解决了问题

First off, I have 2 separate HTML5 audio players that I wanted on my webpage for 2 songs. One song file had underscores, and one song file didn't.

首先,我有 2 个独立的 HTML5 音频播放器,我想要在我的网页上播放 2 首歌曲。一首歌曲文件有下划线,一首歌曲文件没有。

My first step was to take hectorme85's advice, and change my .mp3 and .ogg bitrates to 128kpbs. Had the same problem, except only for the song with the underscores. At this time, the no underscore solution didn't dawn on me yet. Then my second step was to switch the order of the .ogg and .mp3, making .ogg come first. Still, the underscored song didn't work- and the song without the underscores worked.

我的第一步是采纳 hectorme85 的建议,将我的 .mp3 和 .ogg 比特率更改为 128kpbs。有同样的问题,除了带有下划线的歌曲。此时,我还没有意识到无下划线的解决方案。然后我的第二步是切换 .ogg 和 .mp3 的顺序,使 .ogg 排在第一位。尽管如此,下划线的歌曲还是没有用——而没有下划线的歌曲也奏效了。

Lastly, I looked at the differences with both songs very carefully in my markup. That's when it hit me. "I may need to delete the underscores for the song file." So I did, and BAM.. My audio works for me in Chrome.

最后,我在标记中非常仔细地查看了两首歌曲的差异。就在那时它击中了我。“我可能需要删除歌曲文件的下划线。” 所以我做到了,BAM .. 我的音频在 Chrome 中对我有用。

回答by hevi

I had the same problem on Ubuntu 12.04 (couldn't try on mac/win)

我在 Ubuntu 12.04 上遇到了同样的问题(无法在 mac/win 上尝试)

I sort the problem out by switching to ogg, it works perfect on both firefox and chrome.

我通过切换到 ogg 来解决问题,它在 Firefox 和 chrome 上都可以完美运行。

I tried to play mp3s with different bitrates, mono-stereo, quality options but no chance :(

我尝试播放具有不同比特率、单声道立体声、质量选项的 mp3,但没有机会:(

in my opinion problem is caused by codec issues. for me it also explains why firefox has been insisting not to use mp3.

在我看来问题是由编解码器问题引起的。对我来说,这也解释了为什么 Firefox 一直坚持不使用 mp3。