{"id":1345,"date":"2019-02-10T21:23:03","date_gmt":"2019-02-10T21:23:03","guid":{"rendered":"http:\/\/bradharrington.us\/dev\/?p=1345"},"modified":"2019-02-14T08:09:04","modified_gmt":"2019-02-14T08:09:04","slug":"rebuilding-some-old-audio-settings-on-a-website","status":"publish","type":"post","link":"https:\/\/bradharrington.us\/dev\/rebuilding-some-old-audio-settings-on-a-website\/","title":{"rendered":"Rebuilding Audio Settings"},"content":{"rendered":"<p>I was recently looking at an older website I had built for a customer and saw that the audio buttons I had built for his site were no longer functioning correctly.\u00a0 Somewhere along the line, the code became deprecated and now instead of playing each singularly as the button was clicked, all the sounds were playing at once, as the page was loaded.\u00a0 Completely unacceptable and he never had bothered to notify me, so I had no clue.<\/p>\n<p>This was a site built right at the time the new HTML5 was just coming out, so I built most of the site with older code styles.\u00a0 I used inline event handling that was hard-coded into the HTML.\u00a0 This is not the best way to handle JavaScript anymore, so I decided to rewrite the code with a slightly better method.\u00a0 It ended out being pretty simple but did have a trick that needed to be solved.<\/p>\n<p>Writing the functions to play each audio clip was easy, but figuring out how to stop the previous clip from being played was the tricky part.\u00a0 Here is the code I used to play each clip and stop the previous clips.<\/p>\n<p>First off, here is the html for the audio files and 5 of the buttons, there is a 6th button that I am not going to show you because it is part of another section and it&#8217;s too much code to put here, but it works exactly the same as these ones.\u00a0 For the 6th button, I wanted it to work as a stop all audio button, but I kept running into problems with calling the stop.\u00a0 For some reason, every time I added a function to stop audio, it would stop all audio and the buttons would quit working.\u00a0 It seemed like it was a scope problem, but then I thought about just adding an empty sound file that only lasted a split second and it effectively did what I wanted it to do.\u00a0 Not the most elegant solution but it did what I wanted and used the same code that was already working.<\/p>\n<blockquote><p>&lt;audio id=&#8221;sound1&#8243; src=&#8221;sound\/clientIntheShop_f.mp3&#8243; preload=&#8221;auto&#8221; autobuffer&gt;&lt;\/audio&gt;<br \/>\n&lt;audio id=&#8221;sound2&#8243; src=&#8221;sound\/generations_sb_f.mp3&#8243; preload=&#8221;auto&#8221; autobuffer&gt;&lt;\/audio&gt;<br \/>\n&lt;audio id=&#8221;sound3&#8243; src=&#8221;sound\/lifeToProject.mp3&#8243; preload=&#8221;auto&#8221; autobuffer&gt;&lt;\/audio&gt;<br \/>\n&lt;audio id=&#8221;sound4&#8243; src=&#8221;sound\/listenToWood_f.mp3&#8243; preload=&#8221;auto&#8221; autobuffer&gt;&lt;\/audio&gt;<br \/>\n&lt;audio id=&#8221;sound5&#8243; src=&#8221;sound\/makingCustomPiece_f.mp3&#8243; preload=&#8221;auto&#8221; autobuffer&gt;&lt;\/audio&gt;<br \/>\n&lt;audio id=&#8221;sound6&#8243; src=&#8221;sound\/silence.mp3&#8243; preload=&#8221;auto&#8221; autobuffer&gt;&lt;\/audio&gt;<\/p>\n<p>&lt;div id=&#8221;sounds&#8221;&gt;<br \/>\n&lt;form width=&#8221;50px&#8221;&gt;<br \/>\n&lt;input type=&#8221;button&#8221; id=&#8221;soundbtn1&#8243; value=&#8221; &#8220;&gt;<br \/>\n&lt;input type=&#8221;button&#8221; id=&#8221;soundbtn2&#8243; value=&#8221; &#8220;&gt;<br \/>\n&lt;input type=&#8221;button&#8221; id=&#8221;soundbtn3&#8243; value=&#8221; &#8220;&gt;<br \/>\n&lt;input type=&#8221;button&#8221; id=&#8221;soundbtn4&#8243; value=&#8221; &#8220;&gt;<br \/>\n&lt;input type=&#8221;button&#8221; id=&#8221;soundbtn5&#8243; value=&#8221; &#8220;&gt;<br \/>\n&lt;\/form&gt;<br \/>\n&lt;\/div&gt;<\/p>\n<p>var audio1;<br \/>\nvar audio2;<br \/>\nvar audio3;<br \/>\nvar audio4;<br \/>\nvar audio5;<br \/>\nvar audio6;<\/p>\n<p>document.getElementById(&#8220;soundbtn1&#8221;).onclick = function() {playAudio1()};<br \/>\ndocument.getElementById(&#8220;soundbtn2&#8221;).onclick = function() {playAudio2()};<br \/>\ndocument.getElementById(&#8220;soundbtn3&#8221;).onclick = function() {playAudio3()};<br \/>\ndocument.getElementById(&#8220;soundbtn4&#8221;).onclick = function() {playAudio4()};<br \/>\ndocument.getElementById(&#8220;soundbtn5&#8221;).onclick = function() {playAudio5()};<br \/>\ndocument.getElementById(&#8220;soundbtn6&#8221;).onclick = function() {playAudio6()};<\/p>\n<p>\/* The following code stops all audio clips from being played by pausing them in their tracks except anything that you physically target by clicking on it. *\/<\/p>\n<p>document.addEventListener(&#8216;play&#8217;, function(e){<br \/>\nvar audios = document.getElementsByTagName(&#8216;audio&#8217;);<br \/>\nfor(var i = 0, len = audios.length; i &lt; len;i++){<br \/>\nif(audios[i] != e.target){<br \/>\naudios[i].pause();<br \/>\naudios[i].currentTime = 0;<br \/>\n}<br \/>\n}<br \/>\n}, true);<\/p>\n<p>\/* These functions play the associated audio clips that are called by the buttons identified with their unique ID&#8217;s\u00a0 *\/<\/p>\n<p>function playAudio1(){<br \/>\naudio1 = document.getElementById(&#8216;sound1&#8217;);<br \/>\naudio1.play();<br \/>\n}<br \/>\nfunction playAudio2(){<br \/>\naudio2 = document.getElementById(&#8216;sound2&#8217;);<br \/>\naudio2.play();<br \/>\n}<br \/>\nfunction playAudio3(){<br \/>\naudio3 = document.getElementById(&#8216;sound3&#8217;);<br \/>\naudio3.play();<br \/>\n}<br \/>\nfunction playAudio4(){<br \/>\naudio4 = document.getElementById(&#8216;sound4&#8217;);<br \/>\naudio4.play();<br \/>\n}<br \/>\nfunction playAudio5(){<br \/>\naudio5 = document.getElementById(&#8216;sound5&#8217;);<br \/>\naudio5.play();<br \/>\n}<br \/>\nfunction playAudio6(){<br \/>\naudio6 = document.getElementById(&#8216;sound6&#8217;);<br \/>\naudio6.play();<br \/>\n}<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>I was recently looking at an older website I had built for a customer and saw that the audio buttons I had built for his site were no longer functioning correctly.\u00a0 Somewhere along the line, the code became deprecated and now instead of playing each singularly as the button was clicked, all the sounds were &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/bradharrington.us\/dev\/rebuilding-some-old-audio-settings-on-a-website\/\" class=\"more-link\">Read more<span class=\"screen-reader-text\"> &#8220;Rebuilding Audio Settings&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[55],"tags":[],"_links":{"self":[{"href":"https:\/\/bradharrington.us\/dev\/wp-json\/wp\/v2\/posts\/1345"}],"collection":[{"href":"https:\/\/bradharrington.us\/dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bradharrington.us\/dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bradharrington.us\/dev\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bradharrington.us\/dev\/wp-json\/wp\/v2\/comments?post=1345"}],"version-history":[{"count":6,"href":"https:\/\/bradharrington.us\/dev\/wp-json\/wp\/v2\/posts\/1345\/revisions"}],"predecessor-version":[{"id":1393,"href":"https:\/\/bradharrington.us\/dev\/wp-json\/wp\/v2\/posts\/1345\/revisions\/1393"}],"wp:attachment":[{"href":"https:\/\/bradharrington.us\/dev\/wp-json\/wp\/v2\/media?parent=1345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bradharrington.us\/dev\/wp-json\/wp\/v2\/categories?post=1345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bradharrington.us\/dev\/wp-json\/wp\/v2\/tags?post=1345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}