简体   繁体   中英

Play sound when an alarm notification is sent

I have a php file in which I check for the alarms I have to send to the user.

This file is launched using a cron in Ubuntu or in task manager in Windows. This part works well.

Now, I need to play a sound each time an alarm is sent.

If I launch the php file manually in the browser, it works. The sound is playing. But, when I launch it in the command line (like if it was launched in the cron), the alarms are sent but the sound doesn't play.

I use a plugin (jquery.playsound.js) and it comes from : https://github.com/admsev/jquery-play-sound

It only contains this part of code :

(function($){
    $.extend({
        playSound: function(){
          return $(
            '<audio autoplay="autoplay" style="display:none;">'
              + '<source src="' + arguments[0] + '.mp3" />'
              + '<source src="' + arguments[0] + '.ogg" />'
              + '<embed src="' + arguments[0] + '.mp3" hidden="true" autostart="true" loop="false" class="playSound" />'
              + '</audio>'
          ).appendTo('body');
        }
    });
})(jQuery);

Here's the code I use.

<script src='assets/js/jquery-2.1.0.min.js'></script>
<script src='assets/js/jquery.playsound.js'></script>

$resultAlarm = getAlarmToSend();
if( $resultAlarm ){
    while ($row = getRowElement($resultAlarm)){
        $to = $row['Email'];
        $subject = '***** ALARM *****';
        $message = 'THIS IS AN ALARM!!';

        include 'sendmail.php';

        ?>

        <script type="text/javascript">
            $.playSound("assets/sounds/alarm");
        </script>

        <?php 

        resetAlarm();
    }   
}

Someone can help me please?

JavaScript needs a browser to run (normally), something you don't have on shell/console.

You can still play the sound on Ubuntu using shell_exec and aplay , something like:

shell_exec("aplay /home/user/path/to/alarm.wav")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM