Jump to content
ajoo

need some sound advice.

Recommended Posts

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. 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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 !

 

 

 

Share this post


Link to post
Share on other sites

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 !

Share this post


Link to post
Share on other sites

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>

 

  • Thanks 1

Share this post


Link to post
Share on other sites

Hi Guru Barand !

?? $(doucment).ready(...)

Sir you have the Midas touch !

Thanks loads !

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 !

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.