【问题标题】:How to make an image that plays audio when clicked如何制作单击时播放音频的图像
【发布时间】:2016-01-17 03:46:10
【问题描述】:

我正在尝试实现一个播放音频的按钮。我不想有滑块。

有没有办法去掉滑块,或者改用按钮的图像?

当前代码:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Music</title>
<style type="text/css">
body {
        color:transparent;
}
</style>
</head>

<body onLoad="RandomSong();">
<audio id="content" src="background1.mp3" controls></audio>

<script>
function RandomSong() {
    var Music = ["background1.mp3","background2.mp3","background3.mp3","background4.mp3","background5.mp3"];
    var randomCode = Math.floor(Math.random()*Music.length);
    document.getElementById("content").value = "mp3=" + Music[randomCode] + "&amp;showslider=0&amp;width=5&amp;loop=1&amp;autoplay=0&amp;volume=50";
}
</script>
</body>
</html>

【问题讨论】:

  • 到目前为止你有什么代码?
  • 我已经尝试了很多东西,我什至已经让图像显示出来了。我只需要它循环播放声音
  • pastebin.com/2c4wSuqZ 我的音频工作正常,无论如何我可以用图像替换播放器吗?更好的是,无论如何我可以隐藏滑块,只有播放按钮可见?
  • edit您的问题包含您的代码。
  • @Ajean 完成,添加代码

标签: html image button audio


【解决方案1】:

有一个简单的 JQuery 解决方案:

<script type="text/javascript" src="jquery-1.7.1.js"></script>
<script type="text/javascript">
$(document).ready( function() {
$('#track1').click(function(){
   $('#wrap').append('<embed id="embed_player" src="audio.wav" autostart="true" hidden="true"></embed>');
});
});
</script>
<img name="track1" src="images/track1.png" width="180" height="180" border="0" id="track1" alt="" />
<div id="wrap">&nbsp;</div>

Src:查看这篇类似的帖子,上面有@blasteralfred 的回答:Play sound file when image is clicked

【讨论】:

    【解决方案2】:

    我正在为孩子们制作文字游戏,我一直在玩如何制作一个在点击时播放音频的图像的代码。我希望这能帮到您 我正在使用 Notepad++ 这个代码在 Google Chome 中工作

    <!DOCTYPE html>
    <html>
    <script>
    var audio1 = new Audio("/Sound 1/Samples 1Sounds/CAT.wav ");
    audio.oncanplaythrough = function(){}
    audio.onended = function(){}
    </script>
    
    <input type="image"     id="myimage"style="height:200px;width:200px;"src="/Picture 1/Samples     Pictures/cat.jpg" 
    onclick="audio1.play()">
    
    <script>
    var audio2 = new Audio("/Sound 1/Samples 1Sounds/dog.wav ");
    audio.oncanplaythrough = function(){}
    audio.onended = function(){}
    </script>
    
    <input type="image" id="myimage"     style="height:200px;width:200px;"src="/Picture 1/Samples Pictures/dog.jpg" 
    onclick="audio2.play()">
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-25
      • 2017-08-08
      相关资源
      最近更新 更多