自用的MP3播放网页,后台php,前台js,bootstrap
<!--
mp3测试网站
1、自动搜索网站中mp3目录下的文件夹,生成文件夹分类(php)
2、再从各个目录下搜索文件,添加至播放按钮(php+javascript)
3、audio元素添加监听事件,暂停或退出时记忆播放记录(localStorage永久保存)
退出利用onbeforeunload事件,效果不是很好
4、利用post上传历史记录,服务器端用文件保存,如果是post请求则只返回历史记录,如果是get请求则不读取文件
5、将floder由序号替换成文件夹名,这样增加文件夹时不会出错
6、设定跳过片头片尾,用timeupdate事件比较简单,但会频繁刷currenttim,用定时器则操作比较繁琐
-->
<?php
ob_clean();
$_path='./record/mp3record';
if($_SERVER['REQUEST_METHOD']==='POST' ){
if($_POST['em']=="em"){
$_file=fopen($_path, 'r');
$_content=fread($_file, filesize($_path));
echo $_content;
}
if(empty($_POST)){
$content=file_get_contents('php://input');
if(file_exists($_path)==false||filesize($_path)==0){
$_file=fopen($_path, 'w');
$newcontent=array();
array_push($newcontent, $content);
$newcontent=json_encode($newcontent);
fwrite($_file,$newcontent);
fclose($_file);
}else{
$_file=fopen($_path, 'r');
$old_content=fread($_file,filesize($_path));
fclose($_file);
$old_content=json_decode($old_content,true);
if(count($old_content)==10){
array_shift($old_content);
}
array_push($old_content, $content);
$old_content=json_encode($old_content);
$_file=fopen($_path, 'w');
fwrite($_file,$old_content);
fclose($_file);
}
}
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=devic-width, initial-scale=1">
<script src="js/jquery-3.5.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="css\bootstrap.min.css">
<title>我的mp3网站(测试)</title>
<style type="text/css">
a{
text-decoration-line: none;
color: black;
font-family:"STKaiti";
font-weight: bolder;
}
li{
border: 1px solid
background-color: white;
border-radius: 10px;
text-align: center;
width: 10%;
display: inline-block;
margin-bottom: 10px;
padding: 0px;
cursor: pointer;
}
input{
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="container">
<div class="row" id="next">
<!-- 下一首按钮 -->
<div class="col-xs-6">
<button class="btn btn-primary btn-block">上一首</button>
</div>
<div class="col-xs-6">
<button class="btn btn-primary btn-block">下一首</button>
</div>
</div>
<!-- 播放器 -->
<div class='row'>
<audio id="vi" class='col-xs-12 col-md-10 col-md-offset-1' src="" controls="" autoplay="autoplay">你的浏览器不支持</audio>
</div>
<!-- 快进按钮 -->
<div class="row" id="jump">
<div class="col-xs-6">
<button class="btn btn-primary btn-block">快退十秒</button>
</div>
<div class="col-xs-6">
<button class="btn btn-primary btn-block">快进十秒</button>
</div>
</div>
<br>
<!-- 下面是文件夹名称 -->
<div class="row">
<?php $dir=scandir("mp3"); ?> <!-- 扫描根目录 -->
<div style="background:#fc5531 ;color:#FFF;cursor:pointer ">
<h3 id="title">播放内容</h3>
</div>
<div id="index" class="row" >
<?php foreach ($dir as $keya ) { ?>
<?php $path="mp3/".$keya ?>
<?php if(is_dir($path) && $keya!="." && $keya!=".."){ ?>
<li class='col-xs-4'>
<a href="#"><?php echo $keya ?></a>
</li>
<?php }} ?>
<?php ?>
</div>
</div>
<!-- 历史记录 -->
<div class="row" id="record">
<button class="col-xs-5" >上传历史记录</button>
<span class="col-xs-2 glyphicon glyphicon-chevron-down" style="text-Align:center"></span>
<button class="col-xs-5" >下载历史记录</button>
</div>
<!-- 这里是历史记录 先隐藏,点击后显示-->
<div class="row" >
<div id='relist' style="display: none" >
<li class='col-xs-12' style="text-Align:left" rec=""></li>
<li class='col-xs-12' style="text-Align:left" rec=""></li>
<li class='col-xs-12' style="text-Align:left" rec=""></li>
<li class='col-xs-12' style="text-Align:left" rec=""></li>
<li class='col-xs-12' style="text-Align:left" rec=""></li>
<li class='col-xs-12' style="text-Align:left" rec=""></li>
<li class='col-xs-12' style="text-Align:left" rec=""></li>
<li class='col-xs-12' style="text-Align:left" rec=""></li>
<li class='col-xs-12' style="text-Align:left" rec=""></li>
<li class='col-xs-12' style="text-Align:left" rec=""></li>
</div>
</div>
<br>
<!-- 下面开始是具体文件 -->
<div id="itmeall" class='row'>
<?php foreach ($dir as $keya ) { ?><!-- 遍历子目录 -->
<?php $path="mp3/".$keya ?><!-- 加上路径 -->
<?php if(is_dir($path) && $keya!="." && $keya!=".."){ ?><!-- 判断是否是文件夹 -->
<div style="display: inline-block; vertical-align: top " id=""><!-- 将文件夹设置到DIV中 -->
<?php $file=scandir("$path");
$numb=0 ;
foreach ($file as $key ) {
if(strpos($key,".")&&!strpos($key,"db")){ ?><!--
<!-- <a href="#"><?php echo $key?></a> -->
<?php $numb++ ;
$k=$numb<99?"col-xs-3":"col-xs-4"
?>
<li type="button" class="<?php echo $k ?> col-md-1" value="<?php echo $key?>">第 <?php echo $numb ?> 集 <!--
</li>
<?php }} ?>
<?php ?>
</div>
<?php }; ?>
<?php } ;?>
</div>
<!-- 跳过片头片尾 -->
<div class="row" id='jumphead'>
<div class="col-xs-6">
<div class="input-group ">
<span class="input-group-addon">-</span>
<input type="text" class="form-control" style="text-align: center" value=0 >
<span class="input-group-addon">+</span>
</div>
</div>
<div class="col-xs-6">
<div class="input-group ">
<span class="input-group-addon">-</span>
<input type="text" class="form-control" style="text-align: center" value=0 >
<span class="input-group-addon">+</span>
</div>
</div>
<div class="col-xs-6" style="text-align: center">跳过片头</div>
<div class="col-xs-6" style="text-align: center">跳过片尾</div>
</div>
<script type="text/javascript">
var aObj=document.getElementById('index').children;
var itemallObj=document.getElementById("itmeall").children;
var titleObj=document.getElementById("title")
for (var j = itemallObj.length - 1; j >= 0; j--) {
itemallObj[j].id="";
itemallObj[j].style.display='none';
pathNext=this.innerText;
}
for (var i = aObj.length - 1; i >= 0; i--) {
aObj[i].index=i
var pathNext="mp3"
aObj[i].onclick=function (i) {
localStorage.folder=this.innerText
for (var j = itemallObj.length - 1; j >= 0; j--) {
itemallObj[j].id=""
itemallObj[j].style.display='none'
pathNext=this.innerText;
}
for (var j=0 ;j<aObj.length;j++){
aObj[j].style.backgroundColor="white";
aObj[j].children[0].style.color="black"
}
var s=this.index
this.style.backgroundColor="#FF9900";
this.children[0].style.color="white"
itemallObj[s].id="itme";
itemallObj[s].style.display='block'
$("#itme").show(500);
var iObj=document.getElementById('itme').children;
var vObj=document.getElementById('vi');
var playToPause=function(){
if(vObj.paused){
vObj.play();
}else{
vObj.pause();
}
}
titleObj.onclick=playToPause
for (var i = iObj.length - 1; i >= 0; i--) {
iObj[i].index=i
iObj[i].onclick=function () {
if(parseInt(localStorage.file)!=i){
localStorage.file=this.index ;
}
vObj.src="mp3/"+pathNext+"/"+this.getAttribute("value");
titleObj.innerText="播放内容:"+this.getAttribute("value");
vObj.play();
for (var j = 0; j < iObj.length; j++) {
iObj[j].style.backgroundColor="";
}
this.style.backgroundColor="#FF9900";
}
}
}
}
</script>
<script type="text/javascript">
var vObj = document.getElementById('vi');
var auto_play=function(){
for (var i=0; i<=aObj.length; i++){
if (aObj[i].innerText==localStorage['folder']){
aObj[i].click();
break;
}
}
iObj=document.querySelector('#itme').children;
iObj[parseInt(localStorage.file)].click();
vObj.currentTime=parseInt(localStorage.record)
}
try{
auto_play();
}
catch{
console.log("localStorage无文件信息")
aObj[1].click();
}
var iObj = document.getElementById('itme').children;
var nextClick = function (s) {
for (var i = 0; i < iObj.length; i++) {
if (iObj[i].style.backgroundColor == "") {
continue;
}
b = i + s;
iObj[b].click();
localStorage.record=0
break;
}
}
vObj.addEventListener('ended', function () {
nextClick(1);
}, false);
vObj.addEventListener('pause', function () {
localStorage.record=vObj.currentTime;
}, false);
vObj.addEventListener('play', function () {
if (this.currentTime<parseInt(localStorage.jumphead)){
this.currentTime=parseInt(localStorage.jumphead)
}
}, false);
vObj.addEventListener('timeupdate', function () {
if(this.duration-this.currentTime-localStorage.jumpend<=0){
nextClick(1)
}
}, false);
var nextObj = document.querySelector("#next");
nextObj.children[0].firstElementChild.onclick = function () {
nextClick(-1);
}
nextObj.children[1].firstElementChild.onclick = function () {
nextClick(1);
}
var jumpObj = document.querySelector("#jump");
jumpObj.children[0].firstElementChild.onclick = function () {
vObj.currentTime -= 10;
}
jumpObj.children[1].firstElementChild.onclick = function () {
vObj.currentTime += 10;
}
</script>
<!-- 历史记录的js-->
<script type="text/javascript">
var record=document.querySelector("#record");
var historyObj=document.querySelector('#relist')
for (var i = historyObj.children.length - 1; i >= 0; i--) {
historyObj.children[i].onclick=function(){
rec=this.getAttribute('rec');
rec=JSON.parse(rec);
localStorage.record=rec.record
localStorage.folder=rec.folder
localStorage.file=rec.file
auto_play();
}
}
record.children[0].onclick=function(){
localStorage.record=vObj.currentTime;
historyMessage={folder:localStorage.folder,file:localStorage.file,record:localStorage.record};
historyMessage=JSON.stringify(historyMessage);
var xml=new XMLHttpRequest();
xml.open("POST",location.href,true);
xml.setRequestHeader("Content-type", "application/json");
xml.setRequestHeader("kbn-version", "5.3.0");
xml.send(historyMessage);
}
record.children[1].onclick=function(){
var showfile=document.querySelector('#itme')
if(showfile.style.display=='none'){
showfile.style.display="block"
}else{
showfile.style.display="none"
};
}
record.children[2].onclick=function(){
if(historyObj.style.display==""){
historyObj.style.display='none';
return;
}
historyMessage='em=em'
var xml=new XMLHttpRequest();
xml.open("POST",location.href);
xml.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xml.send(historyMessage);
xml.onreadystatechange = function () {
if (xml.readyState == 4 && xml.status == 200) {
var rec = xml.responseText;
rec=JSON.parse(rec);
for(var i=0;i<rec.length;i++){
ii=rec.length-i-1
historyObj.children[ii].setAttribute('rec',rec[i]);
rec[i]=JSON.parse(rec[i]);
historyObj.children[ii].innerText=rec[i].folder+'----第'+(parseInt(rec[i].file)+1)+'集'+'---时间:'+rec[ii].record;
}
historyObj.style.display="";
}
};
}
var jumphead=document.querySelector('#jumphead').children[0].children[0].children;
if(localStorage.jumphead){
jumphead[1].value=localStorage.jumphead;
}else{
localStorage.jumphead=0
}
jumphead[2].onclick=function(){
jumphead[1].value=parseInt(jumphead[1].value)+1;
localStorage.jumphead=jumphead[1].value
}
jumphead[2].touchstart=function(){
time2=window.setInterval(function(){
jumphead[1].value=parseInt(jumphead[1].value)+1;
localStorage.jumphead=jumphead[1].value
},100)
}
jumphead[2].touchend=function(){
window.clearInterval(time2);
}
jumphead[0].onclick=function(){
jumphead[1].value=parseInt(jumphead[1].value)-1;
localStorage.jumphead=jumphead[1].value
}
jumphead[1].onchange=function(){
localStorage.jumphead=this.value
}
var jumpend=document.querySelector('#jumphead').children[1].children[0].children;
if(localStorage.jumpend){
jumpend[1].value=localStorage.jumpend;
}else{
localStorage.jumpend=0
}
jumpend[2].onclick=function(){
jumpend[1].value=parseInt(jumpend[1].value)+1;
localStorage.jumpend=jumpend[1].value
}
jumpend[0].onclick=function(){
jumpend[1].value=parseInt(jumpend[1].value)-1;
localStorage.jumpend=jumpend[1].value
}
jumpend[1].onchange=function(){
localStorage.jumpend=this.value
}
</script>
</div>
</body>
</html>