Creating a music player with JavaScript












0















I am trying to create a music player .
my HTML code is :



<div id="main">
<div id="list" draggable="true">

</div>

<div id="player">
<div id="buttons">

<button id="pre" onclick="pre()"><img src="images/pre.png" height="90%" width="90%"></button>
<button id="play" onclick="playAudio()"><img src="images/play.png" height="90%" width="90%"></button>
<button id="next" onclick=" next()"><img src="images/next.png" height="90%" width="90%"></button>
<input type="file" id="file" name="file" multiple ="multiple" style=" display : none ;">
<button id="browse"><img src="images/browse.jpg" height="90%" width="90%"></button>
<button id="unmute"><img src="images/unmute.png" height="90%" width="90%"></button>

</div>
<div id="seekbar">
<div id="fill"></div>
<div id="handle"></div>
</div>
</div>

</div>


And my JavaScrpit is :



// Browse button
$("#browse").on("click", function() {
$("input").trigger("click");
});

// Append the music
$("#file").change(function() {
var result = $(this)[0].files;
for(var i = 0 ; i< result.length ; i++){
var file = result[i];
// here are the files
$("#list").append("<p id='first'>" + file.name + " (TYPE: " + file.type + ", SIZE: " + file.size + " ) </p>");

}
});

// play the music

$("#list").on( "click" , "#first" , function(){
console.log(song );

});


var songs = document.getElementById("list") ;

var song = new Audio();
var currentSong = 0 ;

$("#list").on( "click" , "#first" , function(){
playSong();
});

window.onload = playSong ;

function playSong(){
song.src = songs[currentSong];

song.play();
};


I want to append music to the (div with id = list) and then when i click on the music, play it but its not working and give me this error :: Uncaught (in promise) DOMException: Failed to load because no supported source was found.



can anyone help me!!!????










share|improve this question

























  • I don't see anywhere that actually loads any media files. Which is reflected by the error you're getting.

    – Rick Calder
    Nov 15 '18 at 23:52






  • 1





    Song.src is pointing at HTMLELEMENT object. The src is supposed to be filename. Unless the text content of div is filename

    – Edwin Dijas Chiwona
    Nov 15 '18 at 23:53






  • 1





    Possible duplicate of Play audio local file with html

    – Chris G
    Nov 15 '18 at 23:55











  • songs[currentSong] is undefined, given that songs isn't an array holding local files but a <div> element.

    – Chris G
    Nov 15 '18 at 23:57











  • first i have this error : index.html:94 Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first. and when i load music and click on it have this error : Uncaught (in promise) DOMException: Failed to load because no supported source was found. @RickCalder

    – hossein asghari
    Nov 16 '18 at 0:01


















0















I am trying to create a music player .
my HTML code is :



<div id="main">
<div id="list" draggable="true">

</div>

<div id="player">
<div id="buttons">

<button id="pre" onclick="pre()"><img src="images/pre.png" height="90%" width="90%"></button>
<button id="play" onclick="playAudio()"><img src="images/play.png" height="90%" width="90%"></button>
<button id="next" onclick=" next()"><img src="images/next.png" height="90%" width="90%"></button>
<input type="file" id="file" name="file" multiple ="multiple" style=" display : none ;">
<button id="browse"><img src="images/browse.jpg" height="90%" width="90%"></button>
<button id="unmute"><img src="images/unmute.png" height="90%" width="90%"></button>

</div>
<div id="seekbar">
<div id="fill"></div>
<div id="handle"></div>
</div>
</div>

</div>


And my JavaScrpit is :



// Browse button
$("#browse").on("click", function() {
$("input").trigger("click");
});

// Append the music
$("#file").change(function() {
var result = $(this)[0].files;
for(var i = 0 ; i< result.length ; i++){
var file = result[i];
// here are the files
$("#list").append("<p id='first'>" + file.name + " (TYPE: " + file.type + ", SIZE: " + file.size + " ) </p>");

}
});

// play the music

$("#list").on( "click" , "#first" , function(){
console.log(song );

});


var songs = document.getElementById("list") ;

var song = new Audio();
var currentSong = 0 ;

$("#list").on( "click" , "#first" , function(){
playSong();
});

window.onload = playSong ;

function playSong(){
song.src = songs[currentSong];

song.play();
};


I want to append music to the (div with id = list) and then when i click on the music, play it but its not working and give me this error :: Uncaught (in promise) DOMException: Failed to load because no supported source was found.



can anyone help me!!!????










share|improve this question

























  • I don't see anywhere that actually loads any media files. Which is reflected by the error you're getting.

    – Rick Calder
    Nov 15 '18 at 23:52






  • 1





    Song.src is pointing at HTMLELEMENT object. The src is supposed to be filename. Unless the text content of div is filename

    – Edwin Dijas Chiwona
    Nov 15 '18 at 23:53






  • 1





    Possible duplicate of Play audio local file with html

    – Chris G
    Nov 15 '18 at 23:55











  • songs[currentSong] is undefined, given that songs isn't an array holding local files but a <div> element.

    – Chris G
    Nov 15 '18 at 23:57











  • first i have this error : index.html:94 Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first. and when i load music and click on it have this error : Uncaught (in promise) DOMException: Failed to load because no supported source was found. @RickCalder

    – hossein asghari
    Nov 16 '18 at 0:01
















0












0








0








I am trying to create a music player .
my HTML code is :



<div id="main">
<div id="list" draggable="true">

</div>

<div id="player">
<div id="buttons">

<button id="pre" onclick="pre()"><img src="images/pre.png" height="90%" width="90%"></button>
<button id="play" onclick="playAudio()"><img src="images/play.png" height="90%" width="90%"></button>
<button id="next" onclick=" next()"><img src="images/next.png" height="90%" width="90%"></button>
<input type="file" id="file" name="file" multiple ="multiple" style=" display : none ;">
<button id="browse"><img src="images/browse.jpg" height="90%" width="90%"></button>
<button id="unmute"><img src="images/unmute.png" height="90%" width="90%"></button>

</div>
<div id="seekbar">
<div id="fill"></div>
<div id="handle"></div>
</div>
</div>

</div>


And my JavaScrpit is :



// Browse button
$("#browse").on("click", function() {
$("input").trigger("click");
});

// Append the music
$("#file").change(function() {
var result = $(this)[0].files;
for(var i = 0 ; i< result.length ; i++){
var file = result[i];
// here are the files
$("#list").append("<p id='first'>" + file.name + " (TYPE: " + file.type + ", SIZE: " + file.size + " ) </p>");

}
});

// play the music

$("#list").on( "click" , "#first" , function(){
console.log(song );

});


var songs = document.getElementById("list") ;

var song = new Audio();
var currentSong = 0 ;

$("#list").on( "click" , "#first" , function(){
playSong();
});

window.onload = playSong ;

function playSong(){
song.src = songs[currentSong];

song.play();
};


I want to append music to the (div with id = list) and then when i click on the music, play it but its not working and give me this error :: Uncaught (in promise) DOMException: Failed to load because no supported source was found.



can anyone help me!!!????










share|improve this question
















I am trying to create a music player .
my HTML code is :



<div id="main">
<div id="list" draggable="true">

</div>

<div id="player">
<div id="buttons">

<button id="pre" onclick="pre()"><img src="images/pre.png" height="90%" width="90%"></button>
<button id="play" onclick="playAudio()"><img src="images/play.png" height="90%" width="90%"></button>
<button id="next" onclick=" next()"><img src="images/next.png" height="90%" width="90%"></button>
<input type="file" id="file" name="file" multiple ="multiple" style=" display : none ;">
<button id="browse"><img src="images/browse.jpg" height="90%" width="90%"></button>
<button id="unmute"><img src="images/unmute.png" height="90%" width="90%"></button>

</div>
<div id="seekbar">
<div id="fill"></div>
<div id="handle"></div>
</div>
</div>

</div>


And my JavaScrpit is :



// Browse button
$("#browse").on("click", function() {
$("input").trigger("click");
});

// Append the music
$("#file").change(function() {
var result = $(this)[0].files;
for(var i = 0 ; i< result.length ; i++){
var file = result[i];
// here are the files
$("#list").append("<p id='first'>" + file.name + " (TYPE: " + file.type + ", SIZE: " + file.size + " ) </p>");

}
});

// play the music

$("#list").on( "click" , "#first" , function(){
console.log(song );

});


var songs = document.getElementById("list") ;

var song = new Audio();
var currentSong = 0 ;

$("#list").on( "click" , "#first" , function(){
playSong();
});

window.onload = playSong ;

function playSong(){
song.src = songs[currentSong];

song.play();
};


I want to append music to the (div with id = list) and then when i click on the music, play it but its not working and give me this error :: Uncaught (in promise) DOMException: Failed to load because no supported source was found.



can anyone help me!!!????







javascript jquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 1 at 1:20









Andy Hoffman

9,38031839




9,38031839










asked Nov 15 '18 at 23:49









hossein asgharihossein asghari

75




75













  • I don't see anywhere that actually loads any media files. Which is reflected by the error you're getting.

    – Rick Calder
    Nov 15 '18 at 23:52






  • 1





    Song.src is pointing at HTMLELEMENT object. The src is supposed to be filename. Unless the text content of div is filename

    – Edwin Dijas Chiwona
    Nov 15 '18 at 23:53






  • 1





    Possible duplicate of Play audio local file with html

    – Chris G
    Nov 15 '18 at 23:55











  • songs[currentSong] is undefined, given that songs isn't an array holding local files but a <div> element.

    – Chris G
    Nov 15 '18 at 23:57











  • first i have this error : index.html:94 Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first. and when i load music and click on it have this error : Uncaught (in promise) DOMException: Failed to load because no supported source was found. @RickCalder

    – hossein asghari
    Nov 16 '18 at 0:01





















  • I don't see anywhere that actually loads any media files. Which is reflected by the error you're getting.

    – Rick Calder
    Nov 15 '18 at 23:52






  • 1





    Song.src is pointing at HTMLELEMENT object. The src is supposed to be filename. Unless the text content of div is filename

    – Edwin Dijas Chiwona
    Nov 15 '18 at 23:53






  • 1





    Possible duplicate of Play audio local file with html

    – Chris G
    Nov 15 '18 at 23:55











  • songs[currentSong] is undefined, given that songs isn't an array holding local files but a <div> element.

    – Chris G
    Nov 15 '18 at 23:57











  • first i have this error : index.html:94 Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first. and when i load music and click on it have this error : Uncaught (in promise) DOMException: Failed to load because no supported source was found. @RickCalder

    – hossein asghari
    Nov 16 '18 at 0:01



















I don't see anywhere that actually loads any media files. Which is reflected by the error you're getting.

– Rick Calder
Nov 15 '18 at 23:52





I don't see anywhere that actually loads any media files. Which is reflected by the error you're getting.

– Rick Calder
Nov 15 '18 at 23:52




1




1





Song.src is pointing at HTMLELEMENT object. The src is supposed to be filename. Unless the text content of div is filename

– Edwin Dijas Chiwona
Nov 15 '18 at 23:53





Song.src is pointing at HTMLELEMENT object. The src is supposed to be filename. Unless the text content of div is filename

– Edwin Dijas Chiwona
Nov 15 '18 at 23:53




1




1





Possible duplicate of Play audio local file with html

– Chris G
Nov 15 '18 at 23:55





Possible duplicate of Play audio local file with html

– Chris G
Nov 15 '18 at 23:55













songs[currentSong] is undefined, given that songs isn't an array holding local files but a <div> element.

– Chris G
Nov 15 '18 at 23:57





songs[currentSong] is undefined, given that songs isn't an array holding local files but a <div> element.

– Chris G
Nov 15 '18 at 23:57













first i have this error : index.html:94 Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first. and when i load music and click on it have this error : Uncaught (in promise) DOMException: Failed to load because no supported source was found. @RickCalder

– hossein asghari
Nov 16 '18 at 0:01







first i have this error : index.html:94 Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first. and when i load music and click on it have this error : Uncaught (in promise) DOMException: Failed to load because no supported source was found. @RickCalder

– hossein asghari
Nov 16 '18 at 0:01














1 Answer
1






active

oldest

votes


















0














The best way to archive what you actually describe is to put your audio url's in an array. Like in the following piece of code:



let song_list = new Array(); // Contains Audio URLs 
let current_song = 0;
let player = new Audio();
player.src = song_list[current_song];

function playSong(){
player.play();
}

function pauseSong(){
player.pause();
}

function nextSong(){
player.pause();
player.src = song_list[++current_song];
player.onload = ()=>{
player.play();
};
}


And keep in mind that autoplay won't work. You need at least an interaction with the website to actually hear the audio(at least on Google Chrome or to be more clear on all V8 running browsers).






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329479%2fcreating-a-music-player-with-javascript%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The best way to archive what you actually describe is to put your audio url's in an array. Like in the following piece of code:



    let song_list = new Array(); // Contains Audio URLs 
    let current_song = 0;
    let player = new Audio();
    player.src = song_list[current_song];

    function playSong(){
    player.play();
    }

    function pauseSong(){
    player.pause();
    }

    function nextSong(){
    player.pause();
    player.src = song_list[++current_song];
    player.onload = ()=>{
    player.play();
    };
    }


    And keep in mind that autoplay won't work. You need at least an interaction with the website to actually hear the audio(at least on Google Chrome or to be more clear on all V8 running browsers).






    share|improve this answer






























      0














      The best way to archive what you actually describe is to put your audio url's in an array. Like in the following piece of code:



      let song_list = new Array(); // Contains Audio URLs 
      let current_song = 0;
      let player = new Audio();
      player.src = song_list[current_song];

      function playSong(){
      player.play();
      }

      function pauseSong(){
      player.pause();
      }

      function nextSong(){
      player.pause();
      player.src = song_list[++current_song];
      player.onload = ()=>{
      player.play();
      };
      }


      And keep in mind that autoplay won't work. You need at least an interaction with the website to actually hear the audio(at least on Google Chrome or to be more clear on all V8 running browsers).






      share|improve this answer




























        0












        0








        0







        The best way to archive what you actually describe is to put your audio url's in an array. Like in the following piece of code:



        let song_list = new Array(); // Contains Audio URLs 
        let current_song = 0;
        let player = new Audio();
        player.src = song_list[current_song];

        function playSong(){
        player.play();
        }

        function pauseSong(){
        player.pause();
        }

        function nextSong(){
        player.pause();
        player.src = song_list[++current_song];
        player.onload = ()=>{
        player.play();
        };
        }


        And keep in mind that autoplay won't work. You need at least an interaction with the website to actually hear the audio(at least on Google Chrome or to be more clear on all V8 running browsers).






        share|improve this answer















        The best way to archive what you actually describe is to put your audio url's in an array. Like in the following piece of code:



        let song_list = new Array(); // Contains Audio URLs 
        let current_song = 0;
        let player = new Audio();
        player.src = song_list[current_song];

        function playSong(){
        player.play();
        }

        function pauseSong(){
        player.pause();
        }

        function nextSong(){
        player.pause();
        player.src = song_list[++current_song];
        player.onload = ()=>{
        player.play();
        };
        }


        And keep in mind that autoplay won't work. You need at least an interaction with the website to actually hear the audio(at least on Google Chrome or to be more clear on all V8 running browsers).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 1 at 1:05

























        answered Feb 1 at 0:59









        DOCTYPE HTMLDOCTYPE HTML

        94541523




        94541523
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329479%2fcreating-a-music-player-with-javascript%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values