0
votes

I had posted some questions to try and get a music player finished and came to the conclusion I needed to re-write my code. With this fresh code its a lot smoother and I belive my bugs now to be a simple fix, I am just not sure what they are. I'll provide a short video along with the explaination and code below. Thanks for any input and explainations to where I went wrong!

So, upon page load / refresh the title of track1 is 'Send Me On My Way' which can be seen in the HTML. However this will not play a song. Track 1 (or others) will play following (nextSong / previousSong) functions. It will then display track1.mp3 (not the title) I tried to edit the name of the songs in the file but then they didnt play.

This is my main issue to get the player up and running as intended. Finally, I was hoping to add some functionality to the progress bar by allowing the user to skip the song, not totally needed but if its a simple edit that would be appreciated.

A visual below https://imgur.com/a/oFuBKLY


</audio>

<header id="about">
    <div class="menu">
        <span class="bar"></span>
    </div>
    <nav class="nav">
        <div class="overlay">
            <ul>
                <li><a href="index.html">Home</a> </li>
                <li><a class=active href="Album.html">Album</a> </li>
                <li><a href="Gallery.html">Gallery</a> </li>
                <li><a href="Book.html">Book Us</a> </li>
            </ul>
        </div>
    </nav>

<div id="main">
    <div id="image">
        <img src="/images/J&G Logo.png"/>
    </div>
    <div id="player">
        <div output id ="songTitle"  >Im On My Way</div>
            <div id="buttons">
                <button id="pre" onclick="pre()"><img src="/images/backwards.png"></button>
                <button id="play" onclick="playOrPauseSong()"><img src="/images/play.png"></button>
                <button id="next" onclick="next()"><img src="/images/forwards.png"></button>
        </div>
    </div>


    <div id="seek-bar">
        <div id="fill"></div>
        <div id="handle"></div>
    </div>
</div>

and the script

var songs = ["/music/Track1.mp3", "/music/Track2.mp3", "/music/Track3.mp3", "/music/Track4.mp3", "/music/Track5.mp3", "/music/Track6.mp3", "/music/Track7.mp3"];
var poster = ["/images/J&G Logo.png", "/images/J&G Logo.png", "/images/J&G Logo.png", "/images/J&G Logo.png", "/images/J&G Logo.png", "/images/J&G Logo.png", "/images/J&G Logo.png"];

var songTitle = document.getElementById("songTitle");
var fillBar = document.getElementById("fill");

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


function playSong(){

   song.src = songs[currentSong];

   songTitle.textContent = songs[currentSong];

   song.play();
}
       function playOrPauseSong(){

           if(song.paused){
               song.play();
               $("#play img").attr("src","/images/Pause.png");
           }
           else{
               song.pause();
               $("#play img").attr("src","/Images/Play.png");
           }
       }

       song.addEventListener('timeupdate',function(){

           var position = song.currentTime / song.duration;

           fillBar.style.width = position * 100 +'%';
       });


       function next(){

           currentSong++;
           if(currentSong > 6){
               currentSong = 0;
           }
           playSong();
           $("#play img").attr("src","/images/Pause.png");
           $("#image img").attr("src",poster[currentSong]);
           $("#bg img").attr("src",poster[currentSong]);
       }

       function pre(){

           currentSong--;
           if(currentSong < 0){
               currentSong = 6;
           }
           playSong();
           $("#play img").attr("src","/images/Pause.png");
           $("#image img").attr("src",poster[currentSong]);
           $("#bg img").attr("src",poster[currentSong]);
       }
1
When the page is loaded the function playSong() is not called. Instead on play button click the function playOrPauseSong() is called and it just run song.play() or song.pause(), but the song object doesn't have src yet. After next() or pre() it is set through playSong() function and everything starts to work. Regarding track names I don't see how it should be set, the only title I see is the <div output id ="songTitle" >Im On My Way</div>. You should map songs array to some titles array or use a map structure (or something else) for storing both files and titles. - user1987
Ah thank you for your advice mate! I will change it in the morning and get back to you on the update. Any idea the best way to add functionality to the progress bar? - Decisive Development
Ok so I have added the following titles in order of the tracks 1-7 var titles = ["I'm On My Way", "Rolling In The Deep", "Mad World", "Too Close", "Feelin' Good", "I Will Wait", "All These Things That I've Done"]; and changed the HTML to '<div output id ='title'></div>' However I'm not sure what code I now need to integrate that into my current lines, my JavaSctript is very fresh. Is ' title.value = titles[index]; ' the line I need to add? In regards to the song not playing on load, I don't want it to auto play but I do want the initial option to be play track1 - Decisive Development

1 Answers

1
votes

I have created a small example based on your code with progress bar and very basic title selection logic. I fixed the first play action and first title issues as well. You can refer to w3schools where you can find more examples of events and attributes for tag.

var songs = ["https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3", "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3", "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-3.mp3", "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-4.mp3", "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-5.mp3", "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-6.mp3", "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-7.mp3"];
var poster = ["/images/J&G Logo.png", "/images/J&G Logo.png", "/images/J&G Logo.png", "/images/J&G Logo.png", "/images/J&G Logo.png", "/images/J&G Logo.png", "/images/J&G Logo.png"];
var titles = ["I'm On My Way", "Rolling In The Deep", "Mad World", "Too Close", "Feelin' Good", "I Will Wait", "All These Things That I've Done"]

var songTitle = document.getElementById("songTitle");
var fillBar = document.getElementById("fill");

var song = new Audio();
var currentSong = 0;
songTitle.textContent = titles[currentSong];

function playSong(){

   song.src = songs[currentSong];

   songTitle.textContent = titles[currentSong];

   song.play();
}
       function playOrPauseSong(){
           if(song.paused){
               playSong();
               $("#play img").attr("src","/images/Pause.png");
           }
           else{
               song.pause();
               $("#play img").attr("src","/Images/Play.png");
           }
       }

       song.addEventListener('timeupdate',function(){
          $('#seekbar').attr("value", this.currentTime / this.duration);
       });


       function next(){
           currentSong++;
           if(currentSong > songs.length - 1){
               currentSong = 0;
           }
           playSong();
           $("#play img").attr("src","/images/Pause.png");
           $("#image img").attr("src",poster[currentSong]);
           $("#bg img").attr("src",poster[currentSong]);
       }

       function pre(){
           currentSong--;
           if(currentSong < 0){
               currentSong = songs.length - 1;
           }
           playSong();
           $("#play img").attr("src","/images/Pause.png");
           $("#image img").attr("src",poster[currentSong]);
           $("#bg img").attr("src",poster[currentSong]);
       }
       
       function countProgress(event) {
          
       }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</audio>

<header id="about">
    <div class="menu">
        <span class="bar"></span>
    </div>
    <nav class="nav">
        <div class="overlay">
            <ul>
                <li><a href="index.html">Home</a> </li>
                <li><a class=active href="Album.html">Album</a> </li>
                <li><a href="Gallery.html">Gallery</a> </li>
                <li><a href="Book.html">Book Us</a> </li>
            </ul>
        </div>
    </nav>

<div id="main">
    <div id="image">
        <img src="/images/J&G Logo.png"/>
    </div>
    <div id="player">
        <div output id="songTitle"></div>
            <div id="buttons">
                <button id="pre" onclick="pre()"><img src="/images/backwards.png"></button>
                <button id="play" onclick="playOrPauseSong()"><img src="/images/play.png"></button>
                <button id="next" onclick="next()"><img src="/images/forwards.png"></button>
        </div>
    </div>

<progress id="seekbar" value="0" max="1" style="width:200px;"></progress>
</div>