创建HTML5音频和玩它不工作 [英] Create a html5 audio and play it doesn't work

查看:195
本文介绍了创建HTML5音频和玩它不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建动态HTML5的音频播放,这里是code:

I want create a html5 audio in dynamic and play it,here is the code:

 function playAnotherMusic(playUrl){
                var audioElement = document.createElement('audio'); 
                audioElement.setAttribute('src', playUrl); 
                audioElement.setAttribute('controls', true); 
                audioElement.setAttribute('preload', true); 
                audioElement.setAttribute('type', 'audio/mpeg'); 


                audioElement.addEventListener("load", function() { 
                audioElement.play(); 
                }, true);

                console.log(playUrl);
                audioElement.load();

  }

但它不工作,萤火虫assigin我 HTTP音频/ MPEG的内容类型不支持。

我该如何解决这个问题?

how can I solve this problem?

推荐答案

您需要将音频元素附加到现有元素。
这将是类似

You need to append the audio element to the an existing element. This would be something like

document.getElementById("my_audio_div").appendChild(audioElement);

Idealy,这应该是你添加事件侦听器之前,但已经设置的所有属性后,

Idealy, this should be done before you add the event listener, but after setting all the attributes

相关搜索音频/ MP3 而不是:
    audioElement.setAttribute('型','音频/ MP3');

Also try audio/mp3 instead: audioElement.setAttribute('type', 'audio/mp3');

这篇关于创建HTML5音频和玩它不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆