php/mysql if 语句为真时播放声音片段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15671767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
play sound clip when php/mysql if statement is true?
提问by James Pale
hi i have a message alert box that appears when a user gets a new message, what i want to do is add sound to this box when it pops up, i am using a php if statement to check when a user gets a new message and i have tried adding sound by doing the following but its not working please can someone show me how to do this. thanks.
嗨,我有一个消息警报框,当用户收到一条新消息时,它会出现,我想要做的是在它弹出时向该框添加声音,我正在使用 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'/>"); ?>
回答by Raphael Caixeta
<?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:
这是 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>
回答by kmoney12
According to the question:
根据问题:
hi i havea message alert box that appears when a user gets a new message, what i want to do is add sound to this box when it pops up
嗨,我有一个消息警报框,当用户收到新消息时会出现该框,我想要做的是在该框弹出时为其添加声音
You already have some sort of javascript function that displays an alert box when needed. Use the info from this answerto play a sound with it.
您已经拥有某种 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();