ap.offBackgroundAudioPlay 移除音乐播放事件的监听

优质
小牛编辑
128浏览
2023-12-01

ap.offBackgroundAudioPlay(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>
<button class="btn btn-default">开启事件监听</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 btnOn = document.querySelector('#J_btnOn');
  var btnOff = document.querySelector('#J_btnOff');
  var output = document.querySelector('#J_output');

  var handlerPlay = function(){
    ap.showToast('开始播放')
  };
  var handlerPause = function(){
    ap.showToast('暂停播放')
  };
  var handlerStop = function(){
    ap.showToast('停止播放')
  };

  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, '  ');
    });
  });
  btnOn.addEventListener('click', onEvents);
  btnOff.addEventListener('click', offEvents);

  function onEvents() {
    ap.onBackgroundAudioPlay(handlerPlay);
    ap.onBackgroundAudioPause(handlerPause);
    ap.onBackgroundAudioStop(handlerStop);
  }
  function offEvents() {
    ap.offBackgroundAudioPlay(handlerPlay);
    ap.offBackgroundAudioPause(handlerPause);
    ap.offBackgroundAudioStop(handlerStop);
  }
</script>