मैं अपनी साइट पर एक ऑडियो बार लागू करने की कोशिश कर रहा हूं जो गाने की सूची से गाने को बेतरतीब ढंग से चलाने के लिए चुनता है। संगीत शुरू होना चाहिए, लेकिन केवल तभी जब प्ले बटन दबाया गया हो, और जब कोई गाना खत्म हो जाए, तो स्वचालित रूप से अगला गाना बजाएं।
जैसा कि पहले उल्लेख किया गया है, नीचे दिया गया कोड ठीक काम करता है, प्ले बटन दबाए बिना संगीत अपने आप बजता है। क्या किसी के पास कोई विचार है कि इसे कैसे ठीक किया जाए?
मैंने player.autoplay=true;
को false
पर सेट करने की कोशिश की, लेकिन फिर अगला गाना अपने आप नहीं चलेगा।
<audio id="audioplayer" controls> <!-- Remove the "Controls" Attribute if you don't want the visual controls -->
</audio>
<script>
var lastSong = null;
var selection = null;
var playlist = ["sounds/0.mp3", "sounds/1.mp3", "sounds/2.mp3"]; // List of Songs
var player = document.getElementById("audioplayer"); // Get Audio Element
player.autoplay=true;
player.addEventListener("ended", selectRandom); // Run function when song ends
function selectRandom(){
while(selection == lastSong){ // Repeat until different song is selected
selection = Math.floor(Math.random() * playlist.length);
}
lastSong = selection; // Remember last song
player.src = playlist[selection]; // Tell HTML the location of the new Song
}
selectRandom(); // Select initial song
player.play(); // Start Song
</script>
1
forRnB
13 अक्टूबर 2019, 14:52
2 जवाब
सबसे बढ़िया उत्तर
क्या आपने खिलाड़ी को हटाने का प्रयास किया है। प्ले ();
मदीना
-1
Maddin
13 अक्टूबर 2019, 12:25
एक बटन बनाएं और एक onclick
विशेषता जोड़ें, selectRandom()
को स्थानांतरित करें एक बटन के लिए onclick
इस तरह कार्य करें:
var player = document.getElementById("audioplayer");
var lastSong = null;
var selection = null;
var playlist = ["https://www.soundjay.com/button/sounds/beep-07.mp3", "https://www.soundjay.com/button/sounds/button-2.mp3", "https://www.soundjay.com/button/sounds/button-3.mp3"]; // List of Songs
function start() {
player.play();
player.addEventListener("ended", selectRandom);
function selectRandom(){
while(selection == lastSong){ // Repeat until different song is selected
selection = Math.floor(Math.random() * playlist.length);
}
lastSong = selection; // Remember last song
player.src = playlist[selection]; // Tell HTML the location of the new Song
}
player.autoplay=true;
selectRandom();
}
<audio id="audioplayer" controls ></audio>
<button onclick="start()"> Play </button>
-1
Ömürcan Cengiz
13 अक्टूबर 2019, 13:09