《拖拽媒体流音乐播放器》

在这里插入图片描述

<body>
    <i>(友情建议不要用 360IE 浏览器,最好使用<a href="javascript:if(confirm(%27 https://blog.csdn.net/qq_42628504/article/details/121061550 \n\nThis file was not retrieved by Teleport Pro, because the server reports that this file cannot be found.  \n\nDo you want to open it from the server?%27))window.location=%27https://blog.csdn.net/qq_42628504/article/details/121061550"tppabs="https://blog.csdn.net/qq_42628504/article/details/121061550">谷歌浏览器</a>)</i>
	<button type="button" id="btn" style='position:absolute;width:100px;height:30px;'>暂停</button>
    <canvas>微信: h1715982897</canvas> 
<script>
		
		/*开始我的音乐跳动之旅*/
		let oC = document.querySelector("canvas");
		let w = (oC.width = window.innerWidth);
		let h = (oC.height = window.innerHeight);
		window.addEventListener("resize", function () {
		  w = oC.width = window.innerWidth;
		  h = oC.height = window.innerHeight;
		});
		let cxt = oC.getContext("2d");
		let color = cxt.createLinearGradient(
		  w / 2,
		  h / 2 - 100,
		  w / 2,
		  h / 2 + 100
		);
		 color.addColorStop(0, "#00f");
		 color.addColorStop(0.5,"#f00");
		 color.addColorStop(1, "#0f0");
		
		btn.onclick=function(e){
			let musicDiv = document.getElementsByClassName('musicStyle')[0];
		    e =e ||window.event;
			console.log(musicDiv)
			if(musicDiv){
				if(!musicDiv.paused){  //false时播放  true是暂停
					musicDiv.pause();
				}else{
					musicDiv.play();
				}
			}
		}
		
		
		document.addEventListener('dragover',function(e){
			  var e = e||window.event;
			  e.preventDefault();
			  e.stopPropagation();
		},{
			passive:false
		})
		
		
		document.addEventListener('drop',createdDrop,{
			passive: false,
		});
		
		
		function createdDrop(e){
			    e.preventDefault();
			    e.stopPropagation();
				if(document.getElementsByClassName('musicStyle')[0]){
						 return;
				}
				let dataTransfer = e.dataTransfer;
				let dataAudio =  dataTransfer.files[0];
				console.log(dataAudio)
				let blob = new Blob([dataAudio]);
				let url =window.URL.createObjectURL(blob);
				if(dataAudio.type.indexOf('audio')!=-1) audioAndVideo('audio',url);
				if(dataAudio.type.indexOf('video')!=-1) audioAndVideo('video',url)	  
		}

	
		   function audioAndVideo(type, url) {
		     let element = document.createElement(type);
		     element.src = url;
			 element.className='musicStyle'
		     if (type === "video") {
		       element.controls = "controls";
		     }
		     element.autoplay = "autoplay";
		     document.body.appendChild(element);
		     element.volume = 0.2;
		     element.play();
			 console.log(element)
		     audioCanvas(element);
		   }
		
		   function audioCanvas(buffer) {
		     // 创建音频对象
		     let oCtx = new AudioContext();
		     // 创建分析器
		     let analyser = oCtx.createAnalyser();
		     // 创建媒体源
		     let audioSrc = oCtx.createMediaElementSource(buffer);
		     // 连接分析器
		     audioSrc.connect(analyser);
		
		     //  mp3  ->  分析器  -> 耳机
		     audioSrc.connect(oCtx.destination);
		
		     let count = 200;
		     let voicehigh = new Uint8Array(analyser.frequencyBinCount);
		     (function draw() {
		       cxt.clearRect(0, 0, w, h);
		       analyser.getByteFrequencyData(voicehigh);
		       let step = Math.round(voicehigh.length / count);
		       for (let i = 0; i < count; i++) {
		         let iH = voicehigh[step * i] / 1;
		         cxt.fillStyle = color;
		         cxt.fillRect(i * 4 + w / 2, h / 2, 2, -iH);
		         cxt.fillRect(w / 2 - (i - 1) * 4, h / 2, 2, -iH);
		         cxt.fillRect(i * 4 + w / 2, h / 2, 2, iH);
		         cxt.fillRect(w / 2 - (i - 1) * 4, h / 2, 2, iH);
		       }
		       requestAnimationFrame(draw);
		     })();
		   }
		
	</script>
	</body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值