2017-01-13 1 views
0

Ich möchte mein Javascript machen, um die Quelle meiner HTML-Audio zu ändern. Ich suchte auf Stackoverflow, aber ich konnte nicht finden, was ich wollte.Javascript ändern Quelle der Musik

Dies ist mein HTML-Code. Ich benutze CSS in diesem Moment nicht.

<!DOCTYPE html> 
<html lang="en"> 

<head> 
    <meta charset="UTF-8"> 
    <title>HigherLower</title> 
</head> 

<body> 

    <h1> welcome to my game. This game is about selecting if a number is getting higher or lower than the previous number. 
    Click this button to get started.</h1> 

    <audio id="currentsong" loop autoplay controls src="trol.mp3"></audio> 

    <button id="songbutton" onclick="nextsong()">next song</button> 


    <script src="javascript.js"></script> 
</body> 

</html> 

Hier ist mein Javascript-Code:

/** 
    * Created by Jasper on 12-1-2017. 
    */ 
var currentsong = document.getElementById('currentsong'); 
var nextbutton = document.getElementById('songbutton'); 

currentsong = 1; 
currentsong = 2; 
currentsong = 3; 

function nextsong() { 
    if (currentsong = 1) { 

    innerHTML = currentsong.src = 'trol.mp3'; 
    } else { 
    currentsong.innerHTML = currentsong.src = 'paradise.mp3'; 
    } 
} 

ich die Musik Änderung auf einem Button machen wollen. Vielen Dank im Voraus

Antwort

0

ändern

currentsong.innerHTML=currentsong.src='paradise.mp3';

Um

currentsong.src = 'paradise.mp3'

+0

danke für die kommentierung. es funktioniert immer noch nicht ... ich sehe, dass ich auch einen fehler in meinem post gemacht habe. innerHTML = streamsong.src = 'trol.mp3'; muss streamsong.src = trol.mp3 sein, ohne das innereHTML –

0

Hier ist i etwas, das Sie Sie Ihren Code müssen beheben try..however können, wie Sie die gleiche Variable verwenden als Counter var, auf dem Sie die Referenz zu Ihrem Element 'streamsong' abrufen

var currentsong = document.getElementById('currentsong'); 
var nextbutton = document.getElementById('songbutton'); 

var currentsong_counter =1; //use a different counter here 
currentsong_counter = 1;// not sure why you are doing consecutive assignments as it will get last value ie. 3 
currentsong_counter = 2; 
currentsong_counter = 3; 

function nextsong() { 
    if (currentsong_counter === 1) { 
    // if you want both html and source to changed then use below else currentsong.src = 'trol.mp3' will do the work 
    currentsong.innerHTML = currentsong.src = 'trol.mp3'; 
    // 
    } 
    else { 
    currentsong.innerHTML = currentsong.src = 'paradise.mp3'; 
    } 
}