温馨提示×

html5中音频播放的实现方法

发布时间:2021-01-30 14:30:40 阅读:147 作者:小新 栏目:web开发

这篇文章给大家分享的是有关html5中音频播放的实现方法的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

undefinedundefinedundefined

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width,initial-scale=1 user-scalable=0"/>
	<link rel="shortcut icon" href="img/logo.png">
	<title>html5 audio音频播放</title>
	<style>
		*{ margin0padding:0;}
		body{-webkit-tap-highlight-colorrgba(0,0,0,0); font-family"微软雅黑"}
		h2width100%font-size1.5emtext-align: center; line-height3emcolor:#47c9af; }
		#audiowidth100%;}
		#controlwidth150pxheight150pxline-height150pxtext-align: center; border-radius200pxborder:none; color:#fffmargin-top: -75pxmargin-left:-75pxleft:50%top:50%position: absolute; box-shadow#888 0 0 8px;}
		.color_graybackground#e4e4e4}
		.hidedisplay: none;}
		.showdisplay: block;}
		.playbackground:  #f06060;}
		.pausebackground:skyblue}
		/*进度条样式*/
		.progressBarwidth100%height10px;margin30px auto 30px auto; position:absolute; left0bottom35px;}
		.progressBar pposition: absolute;}
		.progressBar .progressBacwidth100%height10px;  left0top:0background#e4e4e4;}
		.progressBar .speed{width100%height10pxleft: -100%background#f06060; }
		.progressBar .dragwidth30pxheight30pxleft0top:-10px;  background: skyblue; opacity0.8border-radius50pxbox-shadow#fff 0 0 5px;}
		/*时间样式*/
		#timewidth100%height20px;position: absolute; left0bottom:30pxcolor:#888;}
		.tiemDetailposition: absolute; right:10pxtop:0;}
		#songInfo{overflow: hidden; width200pxheight:50pxline-height50pxtext-align: center; color:#34495e;   margin-top: -25pxmargin-left:-100pxleft:50%top:70%position: absolute;}
		.shareImgposition: absolute; left100000px;}
	</style>
</head>
	
<body>
	<script>
$(function({
	getSong();
})

//获取歌曲链接并插入dom中
function getSong(var audio = document.getElementById("audio");
	audio.src = "http://frontman.qiniudn.com/songnotime.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)
}
	
//进度条
这里我用的是事件实现进度拖动 如果不太熟悉touch的可以看下我之前写的一个小demo http://www.cnblogs.com/leinov/p/3701951.html
 var startX, x, aboveX = 0//触摸时的坐标 //滑动的距离  //设一个全局变量记录上一次内部块滑动的位置 

//1拖动监听touch事件
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");
}

//touchstart,touchmove,touchend事件函数
 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.webkitTransform = 'translate(' + 0+ 'px, ' + y + 'px)';  //也可以用css3的方式     
 	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;//30是拖动圆圈的长度,减掉是为了让歌曲结束的时候不会跑到window以外
 	audio.currentTime = currentTime;
 }
//3拖动的滑动条前进
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>

<h2>html5 audio 音频播放demo</h2>

<!--audiostart-->
<audio id="audio" src=""  loop="loop" autoplay="autoplay" ></audio>
<!--audio End-->



<!--播放控制按钮start-->
<button id="control" class="">loading</button>
<!--播放控制按钮end-->

<!--时间进度条块儿start-->
<section class="progressBar">
	<p class="progressBac"></p>
	<p class="speed" id="speed"></p>
	<p class="drag" id="drag"></p>
</section>
<!--时间进度条块儿end-->

<!--播放时间start-->
<p id="time"><p class="tiemDetail"><span class="currentTime" id="currentTime">00:00</span>/<span class="allTime" id="allTime">00:00</span></p></p>
<!--播放时间end-->
<!--歌曲信息start-->
<p id="songInfo">没时间-Leinov<p class="shareImg"><img src="img/html5audio.jpg" alt=""></p></p>
<!--歌曲信息end-->
<script src="js/zepto.js"></script>
</body>
</html>

2. [图片] audioplay.png

html5中音频播放的实现方法

感谢各位的阅读!关于“html5中音频播放的实现方法”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

温馨提示×

网络异常,请检查网络