Hi All,
How to Play Sound where record of mysql table is added?.
thank you.

Dani AI

Generated

As asked and noted, playing a sound when a database row is added requires client-side playback. The server can detect and record the insert, but it cannot make a browser play audio by itself. Modern browsers also block autoplay unless the user has interacted with the page, so plan for an "enable sound" gesture or a visible control.

If the same browser performs the insert, play sound on the client after the successful INSERT (for example, in your AJAX success handler). A minimal, modern pattern uses the HTML5 audio element and a user gesture to unlock autoplay:

<button id="enableSound">Enable sound</button>
<audio id="notif" preload="auto">
  <source src="ding.mp3" type="audio/mpeg">
  <source src="ding.ogg" type="audio/ogg">
</audio>

<script>
const audio = document.getElementById('notif');
document.getElementById('enableSound').addEventListener('click', () => {
  audio.play().catch(()=>{});
});
// call onInsertSuccess() after your AJAX insert succeeds
function onInsertSuccess() {
  audio.currentTime = 0;
  audio.play().catch(()=>{});
}
</script>

If other users/browsers must be notified when any client inserts a row, push the event to connected clients (WebSocket or Server-Sent Events). Example client using SSE:

const es = new EventSource('/events.php');
es.onmessage = e => {
  if (e.data === 'new_record') {
    document.getElementById('notif').currentTime = 0;
    document.getElementById('notif').play().catch(()=>{});
  }
};

Troubleshooting: serve MP3 and OGG for broad support, ensure correct MIME types and CORS if audio is hosted elsewhere, preload only after the user gesture to save mobile bandwidth, and debounce notifications so the sound isn't spammy. For production, prefer WebSocket or a pub/sub (Redis, etc.) to avoid inefficient DB-polling.

Recommended Answers

All 3 Replies

Member Avatar for Member #949455

You need to create a db and table and rows.

Try this code:

$sound = "<embed src='ding.wav' height=0 width=0 hidden=true autostart=true loop=false volume=100 />";

if(mysql_insert_worked) //some condition that knows if it was successful
    {
         echo $sound;
    }

or

You also can used ajax in <embed>:

http://www.scriptol.com/ajax/anaa/demos.php

Try this code:

<embed src="ding.wav" id="sound2" width="0" heigh="0" autostart="false" enablejavascript="true"/>
Member Avatar for Member #949455

If you feel the question is solve, on your bottom left corner there's button called
"Mark Question Solved". Click on that button. Clicking that button means that your question has been answer so when other members needs to find this answer, they come at your thread to see the correct answer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.