我正在使用 Ionic 3 框架开发一个互联网广播应用程序。我想出了这个使用 HTML5 音频元素的简单代码。许多人认为这种方法比使用@ionic-native/streaming-media插件更好。
以下是我的实现:
HTML
<audio id="audioStream" src="http://icecastserverIP:8000/icecastchannel" autoplay></audio>
<a href="#" id="player">
<div class="btn-play" id="con-btn-play">
<img src="assets/img/btn-play.png" alt="Play">
</div>
</a>
JS
$(document).ready(function(){
var audio = $('#audioStream')[0]
// Preloader animation
audio.addEventListener('waiting', function () {
$('#con-btn-play').html('<img src="assets/img/preloader.gif">');
}, false);
audio.addEventListener('playing', function () {
$('#con-btn-play').html('<img src="assets/img/btn-pause.png">');
}, false);
// Play button behaviour
$('#player').click(function(){
if (audio.paused){
audio.play();
$('#con-btn-play').html('<img src="assets/img/btn-pause.png" alt="Pause">');
}else {
audio.pause();
$('#con-btn-play').html('<img src="assets/imgs/btn-play.png" alt="Play">');
}
});
});
该流在 Android 和 iOS 中播放良好。但是,在最轻微的连接断开时,音频将停止并且不会重新连接。更不用说如果我按下暂停,流将继续在后台使用数据。
我的问题是:有没有更好的方法来处理 Icecast 流?您会推荐一个第三方插件以获得更好的缓冲管理和播放体验?