简体   繁体   English

在javascript中创建“假”媒体播放器?

[英]Creating a “fake” media player in javascript?

I'll try to describe what I'm doing. 我将尝试描述我在做什么。

I currently have 2 HTML5 audio elements, let's call them Master and Slave. 我目前有2个HTML5音频元素,我们称它们为Master和Slave。

They both load the exact same media file but the slave has it's audio muted by default. 它们都加载完全相同的媒体文件,但从属设备默认将其音频静音。

Each player can be controlled (paused, played, scrubbed etc) by the other. 每个玩家都可以由对方控制(暂停,播放,擦洗等)。

Each player has it's own custom styling and receives duration data from a external JSON file (so neither relies on audio.duration to get and calculate duration for other functions in the players like the scrub bar). 每个播放器都有其自己的自定义样式,并从外部JSON文件接收持续时间数据(因此,它们都不依赖audio.duration来获取和计算播放器中其他功能(如滑动条)的持续时间)。

Everything works perfectly, but the obvious downside is that the audio file is being loaded twice for each song. 一切正常,但明显的缺点是音频文件为每首歌曲加载了两次。 (The audio files are being served in small chunks by PHP - there is no direct link to an .mp3 file in the audio.src - instead it's a link to a server side script that requires unique authentication for every initial request, so the audio file isn't being cached - hence the double download). (PHP将音频文件打包为小块文件-在audio.src没有直接链接到.mp3文件的audio.src -而是指向服务器端脚本的链接,该脚本需要对每个初始请求进行唯一身份验证,因此音频文件未缓存-因此需要两次下载。

I actually don't need to use an HTML5 audio element at all on the muted player, other than to get the currentTime of the track, so is there any way I can "fake" an audio element? 实际上,除了获取音轨的currentTime之外,实际上不需要在静音播放器上使用HTML5音频元素,那么有什么方法可以“伪造”音频元素? Maybe by running a setTimeout for the length of the track or something like that? 也许通过为曲目的长度运行setTimeout或类似的方式?

I just need something that can mimic the audio.currentTime value that the HTML5 audio element spits out to update the progress (scrub) bar, but I don't really know where to start or if it's even possible? 我只需要一些可以模仿HTML5音频元素吐出的audio.currentTime值来更新进度(进度条)的工具,但是我真的不知道从哪里开始,或者是否有可能?

Any suggestions welcome, many thanks in advance. 欢迎任何建议,在此先多谢。

EDIT - for clarity, how my site works is something similar to the way that Soundcloud works. 编辑-为了清楚起见,我的网站的工作方式类似于Soundcloud的工作方式。 ie - lots of players (slaves) rendered on screen, all of which can be played individually, all of which can also be controlled by the footer player (master). 即-屏幕上呈现了很多播放器(从属),所有这些都可以单独播放,也可以由页脚播放器(主)控制。 All content on my site (apart from the element containing the Master player) is dynamically loaded with Ajax. 我网站上的所有内容(除了包含Master播放器的元素之外)都动态加载了Ajax。

You should be able to 'fake' the slave audio player as follows: 您应该能够“伪造”从属音频播放器,如下所示:

  • Build the element in HTML & CSS and position it anything other than static, to enable using javascript animations. 在HTML和CSS中构建元素,并将其放置在除静态以外的任何位置,以启用使用Javascript动画。 Your progress bar on the faked player will initially have the following css : 假冒播放器上的进度条最初将具有以下CSS:

     #progress-bar{ left: 0px; } 

    You may animate the fake player's progress bar as follows, using setInterval: 您可以使用setInterval为假玩家的进度栏设置动画,如下所示:

     function moveRight() { var progressBar = document.getElementById('progressBar'); var pos = 0; //position on X Axis let move = setInterval(frame,5); //start moving, once every 5 miliseconds function frame() { if (pos == 400) { clearInterval(move); } else { pos++; progressBar.style.left = 5 + pos/10 + '%'; } } } 

You will have to adjust the numbers in this example to fit your player's length. 在此示例中,您将不得不调整数字以适合播放器的长度。 You will also have to account for the actual audio file's length, on this line: if (pos == 400) When you click on your master player, call this moveRight() function, which will in turn move the fake slider. 您还必须在此行上考虑实际音频文件的长度: if (pos == 400)单击主播放器时,调用此moveRight()函数,该函数将依次移动假滑块。 Play with the numerical values until the movement resembles a real audio player slider. 播放数值,直到动作类似于真实的音频播放器滑块为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM