从 url、php 下载 mp3
我正在尝试在 html5 音频元素中播放来自谷歌翻译的音频文件。我的代码是:
<script>
$("#play_button").click(function(){
$("#memory")[0].play();
});
</script>
<audio id="memory" preload="auto" style="display: none; ">
<source src="http://translate.google.com/translate_tts?tl=en&q=hello">
</audio>
通常会播放一个声音说“你好”。这在 safari 中有效,但在我想要的 google chrome 中无效。
如果我“预加载”声音一次(在另一个选项卡中加载网址),然后再次在我的页面上运行此代码,它就会起作用。可能是因为音频被加载到某个缓存中。 (我对此不太了解......)
是否有办法跟踪音频是否已加载并让浏览器在尝试播放声音之前等待。或者是将文件下载到服务器然后通过新文件引用我的音频的解决方案?
I'm trying to play an audiofile from google translate in an html5 audio element. My code for this is:
<script>
$("#play_button").click(function(){
$("#memory")[0].play();
});
</script>
<audio id="memory" preload="auto" style="display: none; ">
<source src="http://translate.google.com/translate_tts?tl=en&q=hello">
</audio>
which normally would play a voice saying "hello". This works i safari but not in google chrome, where I want it to.
If I "preload" the sound once (loading the url in another tab) and then run this code on my page again, it works. Probably because the audio gets loaded into some cache. (I don't know that much about that...)
Is there anyway to track if the audio is loaded and get the browser to wait for that before trying to play the sound. Or is the solution to download the file to the server and then reference my audio by the new file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
浏览器按照 html 文件中出现的顺序解释 html。如果将脚本标签移到音频标签下方,它应该在运行脚本之前加载音频。
The browser interprets html in the same order as it appears in the html file. If you move your script tag below your audio tag, it should load the audio before running the script.