ajoo Posted May 23, 2019 Share Posted May 23, 2019 Hi ! I have been trying to play sounds using this tiny snippet which gives no errors or warnings but the sounds won't play. var url = "https://hpr.dogphilosophy.net/test/wav.wav"; var audio = new Audio(url); audio.load(); audio.addEventListener("load", function() { audio.play(); }, true); I have checked the sound link and it works directly in the browser. Can someone please help. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/308745-need-some-sound-advice/ Share on other sites More sharing options...
denno020 Posted May 23, 2019 Share Posted May 23, 2019 I have never used Audio previously, but looking at the docs, audio.load() will reload the file, which means you should be able to omit it here. Also, the event to listen for is "loadeddata", rather than "load".. See how those updates go Quote Link to comment https://forums.phpfreaks.com/topic/308745-need-some-sound-advice/#findComment-1567028 Share on other sites More sharing options...
ajoo Posted May 23, 2019 Author Share Posted May 23, 2019 Hi denno, You are correct about the audio.load. So i modified as follows and was quite hopeful that it would work but it still doesn't var url = "music/beep-07.wav"; var audio = new Audio(url); audio.addEventListener("loadeddata", function() { audio.play(); }, true); It gives Quote Uncaught (in promise) DOMException this error message though I wonder why because this is the error given by the play() function if you play() the audio directly while it is still loading. Here it is inside the "loadeddata" event handler so the data should have loaded before play() was called. Still at it. Thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/308745-need-some-sound-advice/#findComment-1567035 Share on other sites More sharing options...
ajoo Posted May 24, 2019 Author Share Posted May 24, 2019 Hi all ! This following code works but HTML: <button class="" id="listen">Play Audio</button> JS: $(document).ready(function(){ $('#listen').click(function(e){ var url = "https://hpr.dogphilosophy.net/test/wav.wav"; var audio = new Audio(url); audio.type = 'audio/wav'; audio.play(); }); }); but only if there is some interaction like the button click on the document first. I would like to get the sounds to play without any interacting controls. Isn't that possible ? Any help appreciated, Thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/308745-need-some-sound-advice/#findComment-1567051 Share on other sites More sharing options...
Barand Posted May 24, 2019 Share Posted May 24, 2019 This works for me <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Lang" content="en"> <title>Sample</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type='text/javascript'> $().ready( function() { var url = "https://hpr.dogphilosophy.net/test/wav.wav"; var audio = new Audio(url); audio.type = 'audio/wav'; audio.play(); }) </script> </head> <body> Sound should be playing </body> </html> 1 Quote Link to comment https://forums.phpfreaks.com/topic/308745-need-some-sound-advice/#findComment-1567057 Share on other sites More sharing options...
ajoo Posted May 24, 2019 Author Share Posted May 24, 2019 Hi Guru Barand ! ?? $(doucment).ready(...) Sir you have the Midas touch ! Thanks loads ! Quote Link to comment https://forums.phpfreaks.com/topic/308745-need-some-sound-advice/#findComment-1567058 Share on other sites More sharing options...
ajoo Posted October 7, 2019 Author Share Posted October 7, 2019 Hi all ! Just like to continue this old thread since the problem is related. I hope that is ok. I have my sounds going great. However, I am trying to get my sound files a bit safe by placing them above the document root and serving them off the server. The code below passes the filename of the audio to invoke the server to serve the same. <html> <head> <title>Test Sound</title> </head> <body> <a onclick="playSound()"> Play</a> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> function playSound() { alert("Play Sound"); var testsound = "beep07.wav"; var data; alert(testsound); $.post("playsound.php", {sf: testsound}, function(data,status){ alert("CP1"); alert("Data : "+data+"\nStatus : "+status); var audio = new Audio(data); audio.type = 'audio/wav'; audio.play(); alert("Thanks"); }); } </script> </body> </html> Everything works fine except that it ends up with a Quote Uncaught (in promise) DOMException I believe that this is happening because it needs an event to fire the sound which is exactly how it was triggered on a button press. But because the server was involved and the file was not lifted directly (by a path and filename) I think, that this error occurred. So how can this be resolved ? Another thing is that this is just a trial code to get a sound going. However what i really would need is a a stream of sound files that I need to invoke on a button press. For e.g. i can have an array like ['a.mp3','b.mp3','c.mp3','d.mp3','e.mp3'] to sound a,b,c,d,e one after the other once the start button is clicked. Thanks all for any help on this. Quote Link to comment https://forums.phpfreaks.com/topic/308745-need-some-sound-advice/#findComment-1570431 Share on other sites More sharing options...
ajoo Posted October 9, 2019 Author Share Posted October 9, 2019 hI all ! I just updated this thread with a question yesterday and I am not sure if this pops "in the new threads to be answered" section where ever that maybe. Earlier it was on the home page as an aside window. So i am trying this once again and noting the views thus far to see if its being read. I Kindly request the Gurus / experts to inspect the code for their views / suggestions to hammer out a solution if one is possible in this manner. Thanks loads ! Quote Link to comment https://forums.phpfreaks.com/topic/308745-need-some-sound-advice/#findComment-1570473 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.