as everyone knows ,audio stay ios It's a common problem that it can't play automatically on , When we want to play automatically , Call directly oncanplay It doesn't work (PC End sum Android The device will play normally ), The following solutions are given here :
Trigger... Manually ( Not recommended )
When our play page comes in from other pages , You can manually trigger playback in the click event of the previous page , Or give a pop-up box after entering the page , Click manually to trigger playback .
The project runs on wechat with the help of WeixinJSBridgeReady Realization ( Use on wechat )
- First load wechat JS-SDK
<script src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
Copy code
- And then through the SDK Page injection configuration information , WeChat JS-SDK Portal .
wx.config({
debug: true, // Turn on debugging mode , Call all api The return value of will be on the client side alert come out , To see the parameters passed in , Can be in pc End open , The parameter information will go through log play , Only in pc Only when the end is printed .
appId: '', // Required , The only sign of official account number
timestamp: , // Required , Generate signature timestamp
nonceStr: '', // Required , Generate a random string of signatures
signature: '',// Required , Signature
jsApiList: [] // Required , Required JS Interface list
});
Copy code
- Finally monitor WeixinJSBridgeReady Called when the paly() How to play it
wx.ready(function() {
let audio = this.$refs.audio;
audio.play();
document.addEventListener("WeixinJSBridgeReady", function() {
audio.play();
},
false
);
});
Copy code
adopt ondurationchange Events to monitor ( recommend )
ondurationchange: Event in video / Audio (audio/video) Triggered when the duration of changes . After this event is triggered , call paly() Play . View details and other event objects Portal
watch: {
// currentSong Refers to the currently playing song
currentSong () {
this.$refs.audio.ondurationchange = () => {
this.totalTime = this.$refs.audio.duration
if (this.isPlaying) {
this.$refs.audio.play()
} else {
this.$refs.audio.pause()
}
}
},
}
Copy code
If there are other ways , Welcome to exchange