Play sound clip when php / mysql statement is true?

Hi, I have a warning window for a message that appears when the user receives a new message, what I want to do is add sound to this square when it appears, I use the php if statement to check when the user receives a new message, and I tried to add sound by doing the following, but not working, please someone show me how to do this. thank.

<?php
$check_new_chats = check_new_chats();
while ($chat = mysql_fetch_array($check_new_chats)) 
if (isset($_SESSION['user_id'])) { 
        if ($chat['to_user_id'] == $_SESSION['user_id']){ 
        echo( "<embed name='sound_file' src='/assets/music/sound_file.mp3' loop='true' hidden='true' autostart='true'/>"); ?>
+5
source share
2 answers
<?php
$check_new_chats = check_new_chats();
while ($chat = mysql_fetch_array($check_new_chats)) 
if (isset($_SESSION['user_id'])) { 
    if($chat['to_user_id'] == $_SESSION['user_id']){ 
        echo '<script type="text/javascript">play_sound();</script>';
    }
}
?>

Here's the Javascript function:

<script type="text/javascript">
    function play_sound() {
        var audioElement = document.createElement('audio');
        audioElement.setAttribute('src', '/assets/music/sound_file.mp3');
        audioElement.setAttribute('autoplay', 'autoplay');
        audioElement.load();
        audioElement.play();
    }
</script>
+3
source

According to the question:

hi , , , , , .

javascript, . , .

<audio id="soundHandle" style="display: none;"></audio>
<script>
  soundHandle = document.getElementById('soundHandle');
  soundHandle.src = '/assets/music/sound_file.mp3';
</script>



//With your message alert function....
alert("Message box");
soundHandle.play();
+1

All Articles