-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
In Vue 2.5 ,the use of MessageChannel in nextTick function will lead to audio can not play in some mobile browsers. #7109
Comments
This is because Mobile browsers only allow programmatic audio play in the same task tick of a user interaction:
The change indeed causes this behavior, but IMO it's somewhat expected because watchers are asynchronous by design. Instead of using a watcher to react to url changes, why not just call a method that sets the url and start playing in the same function? I don't really see a point of using a watcher especially when you need the |
@yyx990803, thanks for your response. This is just a simple demo to show the reason why it can not play in mobile browsers. In our music app, we design it using vuex, set // state.js
const state = {
playlist: [],
currentIndex:0
}
// getters.js
export const currentSong = (state) => {
return state.playlist[state.currentIndex] || {}
} Because // player.vue
watch : {
currentSong(newSong,oldSong) {
if (!newSong.id || !newSong.url || newSong.id === oldSong.id) {
return
}
this.$refs.audio.src = newSong.url
this.$refs.audio.play()
}
} You may say 'why not use sync watcher?',I have tried this, but it cause another problem, because there is a feature that we can set the Currently I fixed this problem by setting the global |
m |
…ers, such as iOS Safari、iOS Wechat Webview、Android Chrome,it can not play. related vuejs/vue#7109 https://juejin.im/post/5a1af88f5188254a701ec230
m |
Version
2.5.8
Reproduction link
https://codesandbox.io/s/vpomxl5p3
Steps to reproduce
Click the 'click me' button and the audio will play.
What is expected?
It can play well in all of the modern browsers that support audio.
What is actually happening?
In PC browsers, it can play well. However, in some mobile browsers, such as iOS Safari、iOS Wechat Webview、Android Chrome,it can not play.
In Vue 2.4 or below, the
nextTick
function usemicroTask
, likeMutationObserver
orPromise
at first,the click callback and audio play function are in the same tick,so it can play well in all browsers.Howerver, in Vue 2.5 ,
macroTask
will be used in DOM action, so the click callback and audio play function are not in the same tick. If we just usesetTimeout 0
, it will be fine, but actuallyMessageChannel
will be firstly used because of the priority, which lead to the audio can not play in some mobile browsers mentioned above.The text was updated successfully, but these errors were encountered: