在问一个问题之前,我曾经尝试过认真研究这个问题,但看起来我身陷困境,想看看是否有人知道这个问题的明确答案。 我见过一两个类似的问题,但他们似乎没有指出我的问题,或者至少我不明白他们要做什么。
我正在研究一个 RPG 系统音频 PHP 功能, 该功能在被调用时播放音乐, 如果用户没有哑音的音乐选项, 它会使用音频标签 。 (在函数 $song 中, 歌曲被投到第一个参数中 。) 如果此函数被调回真实状态, 那么它会使用一些 Javascript 。 请原谅代码, 因为它有点乱( 对我来说), 但这里使用以下代码, 包括 javascrip 代码 :
function playmusic($song, $resume=false, $loop=true)
{
global $baseurl, $loggeduser;
if ($resume == true)
{
$buttons = "
<button onclick="resetplaytime()" type="button">Reset</button>
<button onclick="alertplaytime()" type="button">Alert Time</button>";
$resume = "
<script type="text/javascript">
var curmusic_$song = document.getElementById("music_$song");
var musictime = getcookie("musicplaytime_$song");
if (musictime != null && musictime != "")
{
alert(musictime);
curmusic_$song.currentTime = musictime;
}
window.onload=settimecookie();
function getcookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^s+|s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
}
function settimecookie()
{
var playtime = curmusic_$song.currentTime;
if (playtime != null && playtime != "")
{
document.cookie = musicplaytime_$song= + playtime;
}
setTimeout("settimecookie()", 50);
}
function alertplaytime()
{
alert(musictime);
}
function resetplaytime()
{
curmusic_$song.currentTime=0;
}
</script>";
}
if ($loop == true)
{
$songloop = " loop="true"";
}
if ($loggeduser[ mutemusic ] == 0)
{
return "
</table>
$buttons
<audio id="music_$song" autoplay="true"$songloop preload="true">
<source src="$baseurl/boardfiles/effects/$song.ogg" type="audio/ogg" />
<source src="$baseurl/boardfiles/effects/$song.mp3" type="audio/mp3" />
</audio>
$resume
<table class="brdr" cellpadding="0">";
}
}
现在,如果我把将音频标签播放时间设定为 cookie 值的循环的提醒从循环中移除,整个脚本就不管用,除非您使用按钮直接调用一个函数,然后该函数就起作用。
我尝试了几件事情来把这个函数变成一个函数,然后直接调用它(这似乎只是与点击事件有关,但如果你期望它会运行在加载中或自动事件之后,它就会失败。),然后在函数的后半部分移动它(加上设定时间cookie()的加载,因为它会将饼干设置到当前播放时间),而现在我完全没有想法了。
I am hoping to get an answer for this as I am sure this code could help others who ve been trying to get some sort of resume audio function that isn t bulky or confusing to use. :) I ve been trying to find a solution for quite awhile and never have been this close. Thank you for reading. If you d like the whole PHP function then I ll show that too but there s really nothing to see except two debug buttons (Reset and Alert cookie) and the audio tag with the id music_$song .
<强度 > 编辑 : 仅要明确, 在代码中, 在双引号之前有一些逃跑, 因为正返回 pHP 函数中的 Javascript 。 强度 >
编辑 2: 现在用粗体粗体显示这个编辑, 因为有人错过了它。 此外, 我添加了 PHP 部分 。
Last edit: I have now seemed to figure something out that got me closer to what I wish to achieve. I changed the above code to the following and it works flawless for me.
function playmusic($song, $resume=false, $loop=true)
{
global $baseurl, $loggeduser;
if ($resume == true)
{
$resume = "
<!--
<button onclick="resetplaytime();" type="button">Reset</button>
<button onclick="alertplaytime();" type="button">Alert Time</button>
<button onclick="settimecookie();" type="button">Start Cookie tracking</button>
-->
<script type="text/javascript">
function getcookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^s+|s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
}
function settimecookie()
{
var curmusic_$song = document.getElementById("music_$song");
var playtime = curmusic_$song.currentTime;
if (playtime != null && playtime != "")
{
document.cookie = "musicplaytime_$song=" + playtime;
}
setTimeout("settimecookie()", 50);
}
function resumeplaytime()
{
var curmusic_$song = document.getElementById("music_$song");
var musictime = getcookie("musicplaytime_$song");
if (musictime != null && musictime != "")
{
curmusic_$song.currentTime = musictime;
}
curmusic_$song.play();
settimecookie();
}
window.onload=resumeplaytime;
function alertplaytime()
{
var musictime = getcookie("musicplaytime_$song");
alert(musictime);
}
function resetplaytime()
{
var curmusic_$song = document.getElementById("music_$song");
curmusic_$song.currentTime=0;
}
</script>";
}
else
{
$autoplay = "autoplay="true"";
}
if ($loop == true)
{
$songloop = " loop="true"";
}
if ($loggeduser[ mutemusic ] == 0)
{
return "
</table>
$resume
<audio id="music_$song" $autoplay$songloop preload="true">
<source src="$baseurl/boardfiles/effects/$song.ogg" type="audio/ogg" />
<source src="$baseurl/boardfiles/effects/$song.mp3" type="audio/mp3" />
</audio>
<table class="brdr" cellpadding="0">";
}
}
It seems to work perfectly now. I changed a few things and now works as I want it to. Thanks for the help! :)