所以我发现只有一个类似于我的问题,答案并没有真正帮助我,所以我希望用我自己的代码来帮我找到我想要的答案。
我想在发布新帖子时为我的shoutbox添加声音。我相信播放声音功能的代码片段就在这里
function shouts() {
clearTimeout(getshout);
var xmlHttp = (window.XMLHttpRequest) ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.open("GET", "shoutbox/shouts.php?i=" + Math.random());
xmlHttp.onreadystatechange = function() {
if (this.readyState == 4) {
if (parseInt(this.responseText) > current_shouts) {
getshouts();
current_shouts = parseInt(this.responseText);
}
getshout = setTimeout("shouts()", 1000);
}
}
xmlHttp.send(null);
}
但我不是百分百肯定,我知道这是检查数据库中新条目的地方,这个函数调用的相应php代码是
<?php
require_once("../config.php");
echo implode(mysql_fetch_assoc(mysql_query("SELECT COUNT(id) FROM shout")));
?>
我很确定为了做到这一点,不必触及此代码。我已经在Google上进行了搜索,并检查了多个论坛,但我还没有找到一种实际可行的方法,我不是要求你们为我编写代码,只是给我一个正确的推动方向!
如果还有其他需要请告诉我,我会编辑我的帖子。
谢谢,凯西
答案 0 :(得分:1)
您网页上的某处
<audio id="myaudio" src="shout.mp3"></audio>
在获得新的呼喊时(可能在致电getshouts()
之前)
document.getElementById('myaudio').play();
答案 1 :(得分:1)
我会在audio.play()
内拨打getshouts()
。
请注意,只有最新的浏览器才支持音频标记。请查看此页面,了解音频代码的后备机制:http://code.coneybeare.net/getting-html5-audio-tag-and-flash-fallback-to
HTML
<audio id="audioplayer" preload>
<source src="audio.mp3">
</audio>
的Javascript
var audioTag = document.createElement('audio');
if (!(!!(audioTag.canPlayType) && ("no" != audioTag.canPlayType("audio/mpeg")) && ("" != audioTag.canPlayType("audio/mpeg")))) {
AudioPlayer.embed("audioplayer", {soundFile: "audio.mp3"});
}
function getshouts(){
... // your code
if(success){
audioTag.play();
}
}
此代码未经过测试,但会给您一个基本想法。
更多资源:
干杯!
答案 2 :(得分:0)
继续Eugene的帖子,编辑你的php文件,将回复添加到mysql数据库并播放音乐。
答案 3 :(得分:0)
我的设计是从单行MySQL表中显示声音文件名,并为用户提供播放它们的选项。列名表示在最终应用程序中促使播放声音的事件。这是PHP代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Event Sounds</title>
<link rel="stylesheet" type="text/css" href="common.css" />
</head>
<body>
<h1><b>AZ Science Bowl</b> -- Back Room Operations</h1>
<h2>Event Sounds Selected -- and Test Play</h2>
<?php
$dsn = "mysql:dbname=SciBwl_Operation_TestI";
$username = "********";
$password = "********";
try {
$conn = new PDO( $dsn, $username, $password );
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch ( PDOException $e ) {
echo "Connection failed: " . $e->getMessage();
}
$sql = "SELECT * FROM SoundsSelect";
echo "<ul>";
try {
$rows = $conn->query( $sql );
foreach ( $rows as $row ) {
echo "<li>For <b>Program Start:</b> " . $row["ProgStart"] . "\t<audio controls><source src=\"./sounds/" . $row["ProgStart"] . "\" type=\"audio/wav\"></audio></li>";
echo "<li>For <b>Break & Round Start:</b> " . $row["BrkRndStart"] . "\t<audio controls><source src=\"./sounds/" . $row["BrkRndStart"] . "\" type=\"audio/wav\"></audio></li>";
echo "<li>For <b>Toss & Bonus Start:</b> " . $row["TossBonusStart"] . "\t<audio controls><source src=\"./sounds/" . $row["TossBonusStart"] . "\" type=\"audio/wav\"></audio></li>";
echo "<li>For <b>5 Second Warning:</b> " . $row["5SecWarn"] . "\t<audio controls><source src=\"./sounds/" . $row["5SecWarn"] . "\" type=\"audio/wav\"></audio></li>";
echo "<li>For <b>Toss & Bonus End:</b> " . $row["TossBonusEnd"] . "\t<audio controls><source src=\"./sounds/" . $row["TossBonusEnd"] . "\" type=\"audio/wav\"></audio></li>";
echo "<li>For <b>30 Second End of Break Warning:</b> " . $row["30SecWarn"] . "\t<audio controls><source src=\"./sounds/" . $row["30SecWarn"] . "\" type=\"audio/wav\"></audio></li>";
echo "<li>For <b>Round End:</b> " . $row["RoundEnd"] . "\t<audio controls><source src=\"./sounds/" . $row["RoundEnd"] . "\" type=\"audio/wav\"></audio></li>";
}
} catch ( PDOException $e ) {
echo "Query failed: " . $e->getMessage();
}
echo "</ul>";
$conn = null;
?>
<br><br>
</body>
</html>