console
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1 user-scalable=0"/>
<title>html5 audio音频播放</title>
</head>
<body>
<script>
$(function() {
getSong();
})
function getSong() {
var audio = document.getElementById("audio");
audio.src = "http://m10.music.126.net/20181225094955/47f148625e7159749bebce7137ea01f5/ymusic/0608/0f5b/040e/1b17759e8a816ab4e84c3db59a6d6efa.mp3";
audio.loop = true;
playCotrol();
}
function clicks() {
var audio = document.getElementById("audio");
$("#control").click(function() {
if ($("#control").hasClass("play")) {
$("#control").addClass("pause").removeClass("play");
audio.play();
dragMove();
$("#control").html("暂停播放");
} else {
$("#control").addClass("play").removeClass("pause");
$("#control").html("点击播放");
audio.pause();
}
})
}
function timeChange(time, timePlace) {
var timePlace = document.getElementById(timePlace);
var minute = time / 60;
var minutes = parseInt(minute);
if (minutes < 10) {
minutes = "0" + minutes;
}
var second = time % 60;
seconds = parseInt(second);
if (seconds < 10) {
seconds = "0" + seconds;
}
var allTime = "" + minutes + "" + ":" + "" + seconds + ""
timePlace.innerHTML = allTime;
}
function playCotrol() {
audio.addEventListener("loadeddata",
function() {
$("#control").addClass("play").removeClass("color_gray");
$("#control").html("点击播放");
addListenTouch();
var allTime = audio.duration;
timeChange(allTime, "allTime");
setInterval(function() {
var currentTime = audio.currentTime;
$("#time .currentTime").html(timeChange(currentTime, "currentTime"));
}, 1000);
clicks();
}, false);
audio.addEventListener("pause",
function() {
$("#control").addClass("play").removeClass("pause");
$("#control").html("点击播放");
if (audio.currentTime == audio.duration) {
audio.stop();
audio.currentTime = 0;
}
}, false);
audio.addEventListener("play",
function() {
$("#control").addClass("pause").removeClass("play");
$("#control").html("暂停播放");
dragMove();
}, false);
audio.addEventListener("ended", function() {
alert(0)
}, false)
}
var startX, x, aboveX = 0;
function addListenTouch() {
document.getElementById("drag").addEventListener("touchstart", touchStart, false);
document.getElementById("drag").addEventListener("touchmove", touchMove, false);
document.getElementById("drag").addEventListener("touchend", touchEnd, false);
var drag = document.getElementById("drag");
var speed = document.getElementById("speed");
}
function touchStart(e) {
e.preventDefault();
var touch = e.touches[0];
startX = touch.pageX;
}
function touchMove(e) {
e.preventDefault();
var touch = e.touches[0];
x = touch.pageX - startX;
drag.style.left = aboveX + x + "px";
speed.style.left = -((window.innerWidth) - (aboveX + x)) + "px";
}
function touchEnd(e) {
e.preventDefault();
aboveX = parseInt(drag.style.left);
var touch = e.touches[0];
var dragPaddingLeft = drag.style.left;
var change = dragPaddingLeft.replace("px", "");
numDragpaddingLeft = parseInt(change);
var currentTime = (numDragpaddingLeft / (window.innerWidth - 30)) * audio.duration;
audio.currentTime = currentTime;
}
function dragMove() {
setInterval(function() {
drag.style.left = (audio.currentTime / audio.duration) * (window.innerWidth - 30) + "px";
speed.style.left = -((window.innerWidth) - (audio.currentTime / audio.duration) * (window.innerWidth - 30)) + "px";
}, 500);
}
</script>
<h1>html5 audio 音频播放demo</h1>
<audio id="audio" src="" loop="loop" autoplay="autoplay" ></audio>
<button id="control" class="">loading</button>
<section class="progressBar">
<div class="progressBac"></div>
<div class="speed" id="speed"></div>
<div class="drag" id="drag"></div>
</section>
<div id="time"><div class="tiemDetail"><span class="currentTime" id="currentTime">00:00</span>/<span class="allTime" id="allTime">00:00</span></div></div>
<div id="songInfo">没时间-Leinov<div class="shareImg"><img src="img/html5audio.jpg" alt=""></div></div>
</body>
</html>
*{ margin: 0; padding:0;}
body{-webkit-tap-highlight-color: rgba(0,0,0,0); font-family: "微软雅黑"}
h1{ width: 100%; font-size: 1.5em; text-align: center; line-height: 3em; color:#47c9af; }
#audio{ width: 100%;}
#control{ width: 150px; height: 150px; line-height: 150px; text-align: center; border-radius: 200px; border:none; color:#fff; margin-top: -75px; margin-left:-75px; left:50%; top:50%; position: absolute; box-shadow: #888 0 0 8px;}
.color_gray{ background: #e4e4e4}
.hide{ display: none;}
.show{ display: block;}
.play{ background: #f06060;}
.pause{ background:skyblue}
.progressBar{ width: 100%; height: 10px;margin: 30px auto 30px auto; position:absolute; left: 0; bottom: 35px;}
.progressBar div{ position: absolute;}
.progressBar .progressBac{ width: 100%; height: 10px; left: 0; top:0; background: #e4e4e4;}
.progressBar .speed{width: 100%; height: 10px; left: -100%; background: #f06060; }
.progressBar .drag{ width: 30px; height: 30px; left: 0; top:-10px; background: skyblue; opacity: 0.8; border-radius: 50px; box-shadow: #fff 0 0 5px;}
#time{ width: 100%; height: 20px;position: absolute; left: 0; bottom:30px; color:#888;}
.tiemDetail{ position: absolute; right:10px; top:0;}
#songInfo{overflow: hidden; width: 200px; height:50px; line-height: 50px; text-align: center; color:#34495e; margin-top: -25px; margin-left:-100px; left:50%; top:70%; position: absolute;}
.shareImg{ position: absolute; left: 100000px;}