简体   繁体   English

HTML5音频/ jQuery

[英]HTML5 audio/jQuery

Started to do some research for creating a rather simple media section for a site and I seem to be running into some problems. 开始进行一些研究,以便为网站创建一个非常简单的媒体部分,但我似乎遇到了一些问题。 I will explain to the best of my ability. 我将尽我所能解释。

I have a 'play list' which consist of 'links' - li elements atm - that utilizes aJax/PHP to pull data and loads into an audio element. 我有一个由“链接”(li元素atm)组成的“播放列表”,该列表利用aJax / PHP将数据提取并加载到音频元素中。 I can pull the data just fine, no problems there. 我可以很好地提取数据,那里没有问题。

However my issues arises when a user selects a 'track'. 但是,当用户选择“曲目”时,就会出现我的问题。

The intended song plays with no issues, however if the user selects the same song or a different song, the first 'track' continues to play while the 'newer selected' track starts. 预期的歌曲播放没有问题,但是,如果用户选择同一首歌曲或另一首歌曲,则第一首“曲目”会继续播放,而“新选择的”曲目会开始播放。 I essentially can play 10+ tracks at the same time - rather annoying. 我基本上可以同时播放10多个曲目,这很烦人。

i am attempting to stop the first song - either my destroying the audio tag or whatever other method - , and play the newer selected song when the user selects a different 'track' 我试图停止播放第一首歌曲-我销毁音频标签或其他方法-并在用户选择其他“曲目”时播放较新选择的歌曲

I think i may understand the problem, I keep creating new instances of audio element without destroying the initial instance. 我想我可能会理解问题,我会不断创建音频元素的新实例,而不会破坏初始实例。 thus, was wondering the best way to look for any previous occurrences of audio and upon detection destroy that instance and create a new one. 因此,我们想知道寻找音频的任何先前出现的最佳方法,并在检测到该音频时销毁该实例并创建一个新实例。

Below is some code i am testing with, currently this script runs once the aJax callback = success. 以下是我正在测试的一些代码,当前,一旦aJax回调=成功,此脚本就会运行。

--COMMENTS The below code is in jQuery audio = instances of HTML5 audio arydata = an array that contains data from the PHP script --COMMENTS --COMMENTS下面的代码在jQuery音频中= HTML5音频实例arydata =包含来自PHP脚本的数据的数组--COMMENTS

I am thinking, I would need to add some code before the below code ran to validate if there is any existing audio elements, but im not sure if that is the best approach. 我在想,我需要在运行以下代码之前添加一些代码,以验证是否有任何现有的音频元素,但是我不确定这是否是最佳方法。

var audio = new Audio();
audio.src = arydata[1] + ".mp3";
audio.load();
audio.play();

Let me know if anything else is needed. 让我知道是否还有其他需要。

Found a solution. 找到了解决方案。

I erroneously was creating a new Audio instances every time a user selected a track. 每当用户选择曲目时,我都会错误地创建一个新的Audio实例。

Thus, to fix this issue, I instead created the Audio instance upon the script load. 因此,为了解决此问题,我改为在脚本加载时创建了Audio实例。 Then once a user selects a track, I then find the Audio instance and load the data to it. 然后,一旦用户选择了一个曲目,我便找到Audio实例并将数据加载到该实例。

Below is the code. 下面是代码。

    var audio = new Audio();

$("#media_player_playlist_wrapper li").click(function(){
    var player = $(audio).get(0);
    player.src = filepath;
    player.load();
    player.play();
});

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

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