Why using a source tag inside an audio tag prevents the loadeddata event from firing?

纵饮孤独 提交于 2019-12-04 06:13:17

This is a new answer based on the changing requirements and due to a bug that I missed causing the audio to auto load on page load.

https://jsfiddle.net/3aof2jnL/

This fiddle I have rebuilt from the ground up for your requirements.

This uses one time only functions as event binding that are then unbound once the system is set up.

it also does not have the <audio> tags in the HTML to prevent preloading before it's needed. and create the audio when the play button is triggered.

To understand it all the code in this fiddle has been commented so for more details on how I did it please read the comments in the fiddle.

As for problem 2 you are better off using server-side code E.G use PHP

<?php
$page_name = $_SERVER['REQUEST_URI'];
str_replace($page_name, ".php", ".mp3");
if(file_exists("audio/{$pageName}")){
 // page has an mp3 audio
}
?>

Oh and one last thing before I go you need to get all your audio's and converted them to OGG and have both an OGG Vorbis format and an MP3 format, not all browser support mp3 and the ones that don't do support OGG. then with my javascript, you will see

audioSources["audio/mpeg"] = "http://www.hochmuth.com/mp3/Bloch_Prayer.mp3";

you should add an OGG path for example.

audioSources["audio/ogg"] = "http://www.hochmuth.com/ogg/Bloch_Prayer.ogg";

------ old answer fixed the original problem only -----

Here you go working, https://jsfiddle.net/y61bjk5e/1/

The reason for this is your we're binding events after they had fired because they were inside the playpause function

Also, your play variable was not set so that is now set to a collection that it loops through.

If I understand correctly, you use JSLint to lint your code. The reason why it says the for is unexpected is because of the configuration of JSLint which by default doesn't accept a for-loop. On the JSLint website, you can take a look at the bottom of the page to change that:

Basically, some consider that every loop should be done in functional programming style (with a .forEach for instance). To know why, I suggest you take a look online to get multiple opinions but you can have one here.

As for your "bonus question", I can conclude that the problem lies with using the source tag to specify your audio source. Try using the src attribute of the audio tag and it should work. I could not find why, so we'll have to wait for someone else to answer that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!