Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

jquery and audio problem

Featured Replies

Hi guys, I am building a site for music producers where they could download free sound files and read articles and tutorials on sound production. At the moment I am trying to create a simple music player with the html5 audio tag and jquery controls which would be used to preview the sound before download.

 

I have most of the player finished, play/pause, progress bar, display current time, duration etc. I only have 2 things left to do, one is preloading, but I'll leave that for later, the current problem is navigation via the progress bar click, when the user clicks on the bar it should change the currentTime of the audio.

 

 

jquery

 

$('.progressBar').click(function(e) {
    var elOffset = $(this).offset();
	var innerOffset = (e.clientX - elOffset.left)
	var totalWidth = $('.progressBar').width();
	var perc =  innerOffset / totalWidth;
	var $audio = $(this).parent().find("audio");
	alert($audio.currentTime);
    });

html

<article class="sound_short">
            	<h1>sound1</h1>
            	<p class="pubdate"><strong>Duration:</strong> x</p>
            	<p class="category"><strong>Category:</strong> Percusions</p>
        		
                <a class="play" id="song2" href="javascript:void(0);">
                     <img src="images/design/play_pause.png">
                </a>
                <audio id="audiotag2" src="audio/sound2.mp3" preload="auto"></audio>
                <div class="progressBar">
                	<div class="progress"></div>
                </div>
                <span class="current_time"></span>
                <span class="duration_time"></span>
                    <div class="clear"></div>
                <p>
                   some lorem ipsum text
                    <a href="#">read more</a>
       		</p>
        	<a class="button" href="#">Download</a>
</article>

 

 

this is my current code, I am going one step at the time, trying to figure out how to do this. I need to change the current time on click, but it seems that I can't even acces that proparty in this way, cause I get the code returning "undefined" when I click on the progressBar. I am not sure what is going on, cause I am getting the current time and duration in a same way inside the play function and it works fine there, also I can trigger play normally in this funciton, and it detects the right audio, depending on which progress bar I click.

 

I've seen there are some differences between targeting jquery elemnts and DOM nodes, and that seem like it might be a clue here, but I don't even know what does that even mean, so I need your help. If you need more code, just ask

Edited by DigitalSoulArts

  • Author

I am one of those guys that likes to reinvent the wheel, so I prefer to write my own code, I can practise programing and jquery in that way. Also I am not sure that I could find the exact code for this particular type of a player, most of these only have 1 set od audio controls and options to change the track from the playlist, in my case every sound has its on player which activates on first play.

 

I've got it to work anyway , here is the code

 

  $('.progressBar').click(function(e) {
    var elOffset = $(this).offset();
	var innerOffset = (e.clientX - elOffset.left)
	var totalWidth = $('.progressBar').width();
	var perc =  innerOffset / totalWidth;
	var curTime = $(this).parent().find("audio").get(0).currentTime;
	var durTime = $(this).parent().find("audio").get(0).duration;
	var newTime = perc * durTime ;
	$(this).parent().find("audio").get(0).currentTime = newTime;
	
	});

this .get(0) part did the trick, but I am still not completely sure why

 

 

post-56871-0-77777300-1386501376_thumb.jpg

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.