I can't play a sound file

Joined
Aug 19, 2022
Messages
7
Reaction score
0
I am trying to play an mp3 file that's in the same location as the following .htm file, but no sound is playing:

<!DOCTYPE html>
<html>
<body>
<script>
const audioElement = new Audio('cat.mp3');
audioElement.play();
</script>
</body>
</html>

Thanks for any help.
 
Joined
Jul 3, 2022
Messages
93
Reaction score
23
User interaction needed first

HTML:
<!DOCTYPE html>
<html>
<body>
<button>Play</button>
<script>
const audioElement = new Audio('cat.mp3');
document.querySelector('button').addEventListener('click', function(){
audioElement.play();
});
</script>
</body>
</html>
 
Joined
Aug 19, 2022
Messages
7
Reaction score
0
The above code works OK when I use the Windows Chrome browser on my laptop, but when I use the Android Chrome browser on my mobile phone, it doesn't make any sound.

The .mp3 file and .htm code are both in the same folder on my phone, and I have also tried using a full path to the .mp3 file.

I have made sure that Settings -> Site settings -> Sound is set to Allowed.

Thanks.
 
Joined
Mar 11, 2022
Messages
227
Reaction score
32
That code should work on every device with every browser with default settings. Your problem is somewhere else. Maybe your android settings?
 
Joined
Aug 19, 2022
Messages
7
Reaction score
0
I can't find any other sound settings to change in Android that affect the Chrome browser.

Interestingly when I go to the Google Translate website in Chrome on my phone, when I type a word and click on the speaker icon, no sound is made. I tried this with the Edge browser too, and I get the same issue, so I'm wondering whether the problem is sound setting in the Android operating system rather than a browser setting?
 
Joined
Mar 11, 2022
Messages
227
Reaction score
32
What you may try is this:
Set your Android to developer mode (Google for it)
Install Developer tools for your android chrome (google for it)

Run your project. You should be able to see the console of chrome. Is there any errors?

The only thing comes to my mind is a "permission denied" error.

Try this on your site for testing
Code:
<audio src="cat.mp3" type="audio/mp3" id="testaudio">
<script>

const audioElement =document.getElementById('testaudio');


document.querySelector('button').addEventListener('click', function(){
audioElement.play();
});


</script>

If that works, than it has to do with the order your page is loading your elements. For security reasons chrome and safari may block audio and video elements that aren't available within the body tag. So not available on body.onload.

But: If so, there's a work arround. Don't put your javascript into the body of your site, but in the head section and run it by the onload Event.
 
Joined
Aug 19, 2022
Messages
7
Reaction score
0
Hello. Sorry for the delay on replying.

I've been able to set my phone to developer mode, but I can't find anywhere about how to install the developer tools for Android.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top