uniapp uses uni.createInnerAudioContext() to implement the double-speed function of h5 audio books in the app applet

uni.createInnerAudioContext() implements the double-speed function of h5 audio books in the app applet

I feel that there is no problem with the development of the API provided by the official website, but on different terminals, some seem to work well and some not. I don’t know what the problem is at the moment.

data中的变量:showPlaybackRate: false,
<!-- 倍速弹出层 -->
	<u-popup :show="showPlaybackRate" @close="closePlaybackRate" @open="openPlaybackRate" :round="10">
	<scroll-view scroll-y style="height: 100%;">
		<view class="content" :style="'transform:translateY(' + translateY + 'px);'" :animation="animate">
			<view class="header">
				<view class="title">
					倍速选择
				</view>
				<view class="right" @click="closePlaybackRate">
					<u-icon name="close" color="#000000" size="20"></u-icon>
				</view>
			</view>
	
			<view class="rate-listBox">
				<view class="rate-list" @click="handleRate(0.5)">
					0.5倍速
				</view>
				<view class="rate-list" @click="handleRate(1)">
					1.0倍速
				</view>
				<view class="rate-list" @click="handleRate(1.5)">
					1.5倍速
				</view>
				<view class="rate-list" @click="handleRate(2.0)">
					2.0倍速
				</view>
			</view>
		</view>
	
	</scroll-view>
	</u-popup>

Click the pop-up layer to switch at double speed:

// 倍速播放
//this.bgAudioMannager可以通过两种方法获取,一个支持后台播放,具体可以参考官网,我是用的uni.createInnerAudioContext()
handleRate(type) {
    
    
	let that = this
	// // #ifdef H5
	// 	this.bgAudioMannager = uni.createInnerAudioContext()
	// // #endif
	// // #ifndef H5
	// 	this.bgAudioMannager = uni.getBackgroundAudioManager()
	// // #endif
	// 0.5倍速
	that.bgAudioMannager.pause()
	if (type == 0.5) {
    
    
		that.bgAudioMannager.playbackRate = 0.5
		that.rateText = '0.5X'
	} else if (type == 1.0) {
    
    
		that.bgAudioMannager.playbackRate = 1.0
		that.rateText = '1.0X'
	} else if (type == 1.5) {
    
    
		that.bgAudioMannager.playbackRate = 1.5
		that.rateText = '1.5X'
	} else {
    
    
		that.bgAudioMannager.playbackRate = 2.0
		that.rateText = '2.0X'

	}
	//查资料有的说的是播放和设置倍速不能同时,要不很大可能就没效果,所以用了个定时器,但有的还是没效果,不知道为啥,官网没查到具体问题
	setTimeout(res => {
    
    
		that.bgAudioMannager.play()
	}, 2000)
	// #endif
	that.playStatus = true
	that.showPlaybackRate = false
},

Guess you like

Origin blog.csdn.net/weixin_43877575/article/details/131300283