Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript JQUERY Media Player Duration Issues
    primarykey
    data
    text
    <p>So, I've been working on an mp3 player that doesn't use Flash, just JS, HTML, and CSS. I took some open source code and have been playing with it, however I had to edit it when I started using JSONP to get the "link" of the MP3 file from another website. I had issues getting any of the JS function calls in the HTML to work, so I used JQUERY bind and that fixed everything but duration function and clientx issues. link = foo.mp3</p> <pre><code>function writePlayer(link){ $('&lt;div id=\"main\"&gt;&lt;h2&gt;Episode&lt;/h2&gt;&lt;div id=\"player\"&gt;&lt;input id=\"playButton\" class=\'player_control\' type=\"button\" onClick=\"playClicked(this);\" value=\"&gt;\"&gt;&lt;div id=\"duration\" class=\'player_control\' &gt;&lt;div id=\"duration_background\" onClick=\"durationClicked(event);\"&gt;&lt;div id=\"duration_bar\" class=\'duration_bar\'&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div id=\"volume_control\" class=\'player_control\' onClick=\"volumeChangeClicked(event);\"&gt;&lt;div id=\"volume_background\"&gt;&lt;div id=\"volume_bar\"&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;input type=\"button\" class=\'player_control\' id=\"volume_button\" onClick=\"volumeClicked();\" value=\"Vol\"&gt;&lt;/div&gt;&lt;audio id=\"aplayer\" src='+ link +' type=\"audio/mp3\" onTimeUpdate=\"update();\" onEnded=\"trackEnded();\"&gt;&lt;b&gt;Your browser does not support the &lt;code&gt;audio&lt;/code&gt; element. &lt;/b&gt;&lt;/audio&gt;&lt;div id=\"msg\" class=\'output\'&gt;&lt;/div&gt;&lt;div id=\"content\"&gt;&lt;p&gt;&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;').appendTo('body'); $(document).ready(pageLoaded) $("#playButton").bind('click',playClicked(this)) $("#duration_background").bind('click',durationClicked(event)) $("#volume_control").bind('click',volumeChangeClicked(event)) $("#volume_button").bind('click',volumeClicked(event)) $("#aplayer").bind('timeUpdate',update()) $("#aplayer").bind('ended',trackEnded()) }; var audio_duration; var audio_player; var volume_button; var volume_control; function pageLoaded() { audio_player = document.getElementById("aplayer"); alert(audio_player); volume_button = document.getElementById('volume_button'); volume_control = document.getElementById('volume_control'); //get the duration audio_duration = audio_player.duration; //set the volume set_volume(0.7); } function set_volume(new_volume) { audio_player.volume = new_volume; update_volume_bar(); volume_button.value = "Volume: "+parseInt(new_volume*100); } function update_volume_bar() { new_top = volume_button.offsetHeight - volume_control.offsetHeight; volume_control.style.top = new_top+"px"; volume = audio_player.volume; //change the size of the volume bar wrapper = document.getElementById("volume_background"); wrapper_height = wrapper.offsetHeight; wrapper_top = wrapper.offsetTop; new_height= wrapper_height*volume; volume_bar = document.getElementById("volume_bar"); volume_bar.style.height=new_height+"px"; new_top = wrapper_top + ( wrapper_height - new_height ); volume_bar.style.top=new_top+"px"; } function update(audio_player) { //get the duration of the player dur = audio_player.duration; time = audio_player.currentTime; fraction = time/dur; percent = (fraction*100); wrapper = document.getElementById("duration_background"); new_width = wrapper.offsetWidth*fraction; document.getElementById("duration_bar").style.width=new_width+"px"; } function playClicked(element) { //get the state of the player if(audio_player.paused) { audio_player.play(); newdisplay = "| |"; }else{ audio_player.pause(); newdisplay = "&gt;"; } element.value=newdisplay; } function trackEnded() { //reset the playControl to 'play' document.getElementById("playButton").value="&gt;"; } function volumeClicked(event) { control = document.getElementById('volume_control'); if(control.style.display=="block") { control.style.display="None"; }else{ control.style.display="Block"; update_volume_bar(); } } function volumeChangeClicked(event) { //get the position of the event clientY = event.clientY; offset = event.currentTarget.offsetTop + event.currentTarget.offsetHeight - clientY; volume = offset/event.currentTarget.offsetHeight; set_volume(volume); update_volume_bar(); } function durationClicked(event) { //get the position of the event clientX = event.clientX; left = event.currentTarget.offsetLeft; clickoffset = clientX - left; percent = clickoffset/event.currentTarget.offsetWidth; duration_seek = percent*audio_duration; document.getElementById("aplayer").currentTime=duration_seek; }; &lt;/script&gt; </code></pre> <p>Thank you very much for your help!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload