嗨我有一个消息提醒框,当用户收到新消息时出现,我想要做的是当它弹出时添加声音到这个框,我使用php if语句来检查用户何时获得新消息,我尝试通过执行以下操作添加声音,但它不起作用请有人告诉我如何做到这一点。感谢。
<?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'/>"); ?>
答案 0 :(得分:3)
<?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>';
}
}
?>
这是Javascript函数:
<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>
答案 1 :(得分:1)
根据问题:
嗨我有消息提示框,当用户获得新消息时会显示该消息提示框 消息,我想要做的是弹出时将声音添加到此框
您已经拥有某种javascript函数,可在需要时显示警告框。使用this answer中的信息播放声音。
<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();