问题描述
我试图找出如何触发打从JavaScript音频。我得到了一些HTML,看起来像:
I'm trying to figure out how to trigger playing an audio from javascript. I got some html which looks like:
<div class='audio' >foo
<audio preload='auto' controls src='test.wav'>
<b>Your browser does not support the audio tag.</b>
</audio>
</div>
和我想与触发它:
$(document).ready(function () {
$('.audio').each(function(){
var audio = $(this).find('audio').get();
$(this).click(function(){
audio.volume = 100;
alert('1 '+audio);
audio.play();
alert('2');
});
});
});
警报(1+音频);如预期报告音频作为HTMLAudioElement工作。然而警报('2');不叫,因为我得到的错误'audio.play'是不是一个函数。我能做些什么来解决这个问题?
alert('1 '+audio); works as expected and reports audio as HTMLAudioElement. However alert('2'); is not called because I get the error 'audio.play' is not a function. What can I do to solve this problem?
推荐答案
您code看起来很完美。什么是你所得到的错误是alert.play不是一个函数或audio.play是不是一个函数。
Your code looks perfect. What is the error you are getting is it "alert.play" is not a function or "audio.play" is not a function.
我觉得
var audio = $(this).find('audio').get();
返回一个数组,试试这个
returns an array try this
var audio = $(this).find('audio').get(0);
这篇关于使用JavaScript发挥audioelement(HTML5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!