BackgroundAudioManager.offPrev

收藏
我的收藏
基础库 1.36.0 开始支持本方法,这是一个同步方法。
取消监听 **BackgroundAudioManager.onPrev **注册的事件处理函数。

语法

backgroundAudioManager.offPrev(callback)

参数说明

callback

类型
默认值
必填
说明
最低支持版本
function
BackgroundAudioManager.onPrev 注册的事件处理函数引用,如果没有传递则取消监听 BackgroundAudioManager.onPrev 所有的事件处理函数
1.36.0

返回值

扫码体验

代码示例

<!-- index.ttml --> <view class="container"> <view class="body body-bg"> <view class="page-section page-section-space"> <view class="page-body-buttons"> <block tt:if="{{playing === true}}"> <view class="page-body-button" bindtap="stop"> <image src="{{imageUrl+'stop.png'}}"></image> </view> <view class="page-body-button" bindtap="pause"> <image src="{{imageUrl+'pause.png'}}"></image> </view> </block> <block tt:if="{{playing === false}}"> <view class="page-body-button"></view> <view class="page-body-button" bindtap="play"> <image src="{{imageUrl+'play.png'}}"></image> </view> </block> <view class="page-body-button"></view> </view> <view class="page-section-title page-section-title-space"> 已监听用户在系统音乐播放面板点击上一曲事件 <view> 提示: 暂仅支持iOS </view> </view> </view> <button class="on-prev-btn" type="primary" bindtap="offPrev"> offPrev </button> </view> </view>
// index.js const musicUrls = [ "https://sf1-ttcdn-tos.pstatp.com/obj/developer/sdk/0000-0002.mp3", "https://sf1-ttcdn-tos.pstatp.com/obj/developer/sdk/0000-0001.mp3", "https://sf1-ttcdn-tos.pstatp.com/obj/developer/sdk/0000-0003.mp3", ]; const app = getApp(); Page({ data: { imageUrl: app.globalData.imageUrl, playing: false, paused: false, curSrcIdx: 1, }, onPrevCallback: null, onLoad() { const bgam = tt.getBackgroundAudioManager(); this.backgroundAudioManager = bgam; this.canUpdateUI = true; bgam.startTime = 0; bgam.title = "测试背景音乐"; bgam.audioPage = { path: "pages/API/BackgroundAudioManager.play" }; bgam.onPlay(() => { this.updateUI(); }); bgam.onPause(() => { this.updateUI(); }); bgam.onStop(() => { this.updateUI(); }); bgam.onEnded(() => { this.updateUI(); }); this.onPrevCallback = () => { const { curSrcIdx } = this.data; this.setData( { curSrcIdx: curSrcIdx <= 0 ? musicUrls.length - 1 : curSrcIdx - 1, }, () => { this.backgroundAudioManager.src = musicUrls[this.data.curSrcIdx]; this.updateUI(); } ); }; bgam.onPrev(this.onPrevCallback); }, onUnload() { this.offPrev(); }, updateUI() { const bgam = this.backgroundAudioManager; if (this.canUpdateUI) { this.setData({ playing: !bgam.paused, }); } }, pause() { this.backgroundAudioManager.pause(); this.updateUI(); }, play() { if (!this.backgroundAudioManager.src) { this.backgroundAudioManager.src = musicUrls[this.data.curSrcIdx]; } this.backgroundAudioManager.play(); this.updateUI(); }, stop() { this.backgroundAudioManager.stop(); this.updateUI(); }, offPrev() { if (this.onPrevCallback) { this.backgroundAudioManager.offPrev(this.onPrevCallback); this.onPrevCallback = null; tt.showToast({ title: "offPrev", icon: "none", }); } }, });
/* index.ttss */ .body-bg { border-radius: 18rpx; background: #8c8c8c; overflow: hidden; } image { width: 150rpx; height: 150rpx; } .page-body-buttons { display: flex; justify-content: space-around; margin: 50rpx 0; } .page-body-button { width: 250rpx; text-align: center; } .page-section { overflow: hidden; margin-bottom: 0; } .page-section-title-space { color: #eee; }

Bug & Tip