从我的播放列表onclick图像播放随机音乐

时间:2015-11-03 21:19:04

标签: javascript audio onclick dreamweaver

我有这个代码,当我点击图片时(音频不在下面的代码上工作,但它在我的网站上有效)可以播放一首音乐,并在我再次点击它时暂停音乐。

我的疑问是,每当有人进入我的网站并点击图片时,如何从我的var sounds播放列表中加载随机音乐?

提前致谢。

<script>
    var sounds = [
	"http://www.vtxfactory.com/sounds/royksopp.mp3",
	"http://www.vtxfactory.com/sounds/9thwonder.mp3",
	"http://www.vtxfactory.com/sounds/thisbeat.mp3",
	"http://www.vtxfactory.com/sounds/mosdef.mp3",
	"http://www.vtxfactory.com/sounds/oizo.mp3",
	"http://www.vtxfactory.com/sounds/dilla.mp3",];
	
	function StartOrStop(audioFile) {
        
		var audie = document.getElementById("myAudio");
        if (!audie.src || audie.src !== audioFile) audie.src = audioFile;
        console.log(audie.paused);
        if (audie.paused == false) {
            console.log('pause');
            audie.pause();
        } else {
            console.log('play');
            audie.play();
        }
    }
</script>
<img src="http://vtxfactory.com/images/vtxfactorylogobw.png" onclick="StartOrStop('http://www.vtxfactory.com/sounds/oizo.mp3')" class="spin" align="left" onmouseover="this.src='http://vtxfactory.com/images/vtxfactorylogocolor.png'" onmouseout="this.src='http://vtxfactory.com/images/vtxfactorylogobw.png'" onmousedown="this.src='http://vtxfactory.com/images/vtxfactorylogocolor.png'" onmouseup="this.src='http://vtxfactory.com/images/vtxfactorylogobw.png'" width="165px" height="190px" >
<audio id="myAudio"></audio>

3 个答案:

答案 0 :(得分:0)

您可以创建一个getter函数来实现这一目标。

Dim dt As DataTable = New DataTable()
dt.Columns.Add(New DataColumn("ID", GetType(String)))

'YOUR DATACOUMN NAME IN DATATABLE  AND  DATAFIELD OF BOUNDFIELD IN GRIDVIEW MUST MATCH
dt.Columns.Add(New DataColumn("company", GetType(String)))
dt.Columns.Add(New DataColumn("email", GetType(String)))

For i As Integer = 0 To email_list.Length - 2' SHOULD IT NOT BE email_list.Length - 1, IF LAST ONE IS NOT BLANK
    Dim y As Integer = 0
    Dim strarray2() As String = email_list(i).Split("|"c)

    'AS Namrehs POINTED IN COMMENTS, NO YOU REALLY DON'T NEED THAT "For Each str2 In strarray2" LOOP

    ID = strarray2(0)
    company = strarray2(1)
    email = strarray2(2)

    'ADD ONE ROW AT A TIME [my logic of what I want]
    Dim dr As DataRow = dt.NewRow()
    dr("ID") = Convert.ToString(ID)
    dr("company") = Convert.ToString(company)
    dr("email") = Convert.ToString(email)
    dt.Rows.Add(dr)
Next

'HERE YOU ALSO HAVE TO ASSIGN AND BIND IT TO THE GRIDVIEW
DisplaySup.DataSource = dt
DisplaySup.DataBind()

答案 1 :(得分:0)

从StartOrStop函数中删除参数,并将其添加为音频源

audie.src = sounds[Math.floor(Math.random()*sounds.length)];

答案 2 :(得分:0)

这有效

<script>
    var sounds = [
        "http://www.vtxfactory.com/sounds/royksopp.mp3",
        "http://www.vtxfactory.com/sounds/9thwonder.mp3",
        "http://www.vtxfactory.com/sounds/thisbeat.mp3",
        "http://www.vtxfactory.com/sounds/mosdef.mp3",
        "http://www.vtxfactory.com/sounds/oizo.mp3",
        "http://www.vtxfactory.com/sounds/dilla.mp3"
    ];

    function StartOrStop(audioFile) {
        var audie = document.getElementById("myAudio");
        if (audie.paused) {
            audie.src = sounds[Math.floor(Math.random() * sounds.length)];
            audie.play();
        } else {
            audie.pause();
        }
    }
</script>

<img src="http://vtxfactory.com/images/vtxfactorylogobw.png" onclick="StartOrStop()" class="spin" align="left" onmouseover="this.src = 'http://vtxfactory.com/images/vtxfactorylogocolor.png'" onmouseout="this.src = 'http://vtxfactory.com/images/vtxfactorylogobw.png'" onmousedown="this.src = 'http://vtxfactory.com/images/vtxfactorylogocolor.png'" onmouseup="this.src = 'http://vtxfactory.com/images/vtxfactorylogobw.png'" width="165px" height="190px" >
<audio id="myAudio"></audio>