uniapp中發(fā)布為H5,在微信中自動(dòng)播放音頻和視頻。
音頻自動(dòng)播放
安裝微信jssdk
npm install jweixin-module --save
頁面內(nèi)使用:
var jweixin = require('jweixin-module'); //引入jssdk
this.innerAudioContext = uni.createInnerAudioContext();
this.innerAudioContext.src = '音頻路徑';
this.innerAudioContext.autoplay = true;
this.innerAudioContext.loop = true;
this.innerAudioContext.onPlay(() => {
console.log('開始播放');
});
this.innerAudioContext.onError((res) => {
console.log(res.errMsg);
console.log(res.errCode);
});
jweixin.config({}); // 模擬簽名
let that = this;
jweixin.ready(function() {
WeixinJSBridge.invoke('getNetworkType', {}, function(e) {
that.innerAudioContext.play();
})
})
這塊最關(guān)鍵的就是引入jssdk!。經(jīng)測(cè)試,音頻自動(dòng)播放效果相對(duì)穩(wěn)定,兼容性也比較好。
視頻自動(dòng)播放
安裝微信jssdk
npm install jweixin-module --save
頁面內(nèi)使用
<template>
<video :src="src" class="video-block" autoplay="false" controls @play="playing" id="myVideo"></video>
</template>
<script>
var jweixin = require('jweixin-module');
playing(e){
console.log("開始播放");
}
configMedia(){
this.videoContext = uni.createVideoContext('myVideo');
jweixin.config({}); // 模擬簽名
let that = this;
this.$nextTick(()=>{
jweixin.ready(function() {
WeixinJSBridge.invoke('getNetworkType', {}, function(e) {
that.videoContext.play();
})
})
})
}
</script>
經(jīng)測(cè)試,ios/安卓都可以在微信瀏覽器中實(shí)現(xiàn)自動(dòng)播放。如果視頻本身比較小,則可以平滑實(shí)現(xiàn)自動(dòng)播放。如果尺寸比較大。
需要等待加載(此時(shí)可能無法自動(dòng)播放)文章來源:http://www.zghlxwxcb.cn/news/detail-712478.html
總結(jié):
以上均為實(shí)踐經(jīng)驗(yàn),僅供參考。文章來源地址http://www.zghlxwxcb.cn/news/detail-712478.html
到了這里,關(guān)于uniapp中發(fā)布為H5,在微信中自動(dòng)播放音頻和視頻。的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!