ap.onBackgroundAudioPause 监听音乐暂停事件
优质
小牛编辑
129浏览
2023-12-01
ap.onBackgroundAudioPause(CALLBACK)
监听音乐暂停事件。
代码示例
<script src="https://gw.alipayobjects.com/as/g/h5-lib/alipayjsapi/3.1.1/alipayjsapi.inc.min.js"></script>
<style>.output{ display:block; max-width: 100%; overflow: auto}</style>
<button class="btn btn-default">开始播放</button>
<button class="btn btn-default">暂停播放</button>
<button class="btn btn-default">停止播放</button>
<button class="btn btn-default">从0开始播放</button>
<button class="btn btn-default">获取播放状态信息</button>
<pre class="output"></pre>
<script>
var btnPlayBackgroundAudio = document.querySelector('#J_btnPlayBackgroundAudio');
var btnPauseBackgroundAudio = document.querySelector('#J_btnPauseBackgroundAudio');
var btnStopBackgroundAudio = document.querySelector('#J_btnStopBackgroundAudio');
var btnSeekBackgroundAudio = document.querySelector('#J_btnSeekBackgroundAudio');
var btnGetState = document.querySelector('#J_btnGetState');
var output = document.querySelector('#J_output');
btnPlayBackgroundAudio.addEventListener('click', function(){
ap.playBackgroundAudio({
title: '舒缓轻音乐',
url: 'https://os.alipayobjects.com/rmsportal/FOjtUAVjVsfldZgjBdxE.mp3',
logo: 'https://zos.alipayobjects.com/rmsportal/VRrMXbgUSdsibQjwZpyT.png',
cover: 'https://zos.alipayobjects.com/rmsportal/SKMiZgNnNTQNdJWzQfdI.JPEG'
});
});
btnPauseBackgroundAudio.addEventListener('click', function(){
ap.pauseBackgroundAudio();
});
btnStopBackgroundAudio.addEventListener('click', function(){
ap.stopBackgroundAudio();
});
btnSeekBackgroundAudio.addEventListener('click', function(){
ap.seekBackgroundAudio(0);
});
btnGetState.addEventListener('click', function(){
ap.getBackgroundAudioPlayerState(function(res){
output.innerHTML = JSON.stringify(res, undefined, ' ');
});
});
ap.onBackgroundAudioPlay(function(){
ap.showToast('开始播放')
});
ap.onBackgroundAudioPause(function(){
ap.showToast('暂停播放')
});
ap.onBackgroundAudioStop(function(){
ap.showToast('停止播放')
});
</script>