如何使用 Angular 2 播放 mp3? [英] How to play mp3 with Angular 2?

查看:16
本文介绍了如何使用 Angular 2 播放 mp3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个小型离子/角度应用程序,在其中从远程服务器加载动态 mp3 文件并播放它以按下按钮,或者如果选中它,我会自动播放它.我尝试使用简单的 <audio></audio> 但我不确定这是否是好方法......

I want to write a small ionic/angular application where I load a dynamic mp3 file from a remote server and play it for pressing a button OR if it is checked I play it automaticly. I tried to use the simple <audio></audio> but i am not sure if this is the good way for this...

代码:

html

<audio id="player">
<source id="source" src="http://remote.address.com/example.mp3"></source>
</audio>

javascript

play(){
var audio= document.getElementById('player');
audio.play();
}

推荐答案

您可以在 Javascript 代码中创建一个新的 Audio 元素......而不是在 HTML 中

You can create a new Audio element in the Javascript code ... not in HTML

例如:

var audio = new Audio();
audio.src = "http://remote.address.com/example.mp3";
audio.load();
audio.play();

这篇关于如何使用 Angular 2 播放 mp3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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