chrome會沒有聲音

creator版本是1.8.2
之前做好的遊戲都有好好的播放音樂音效
這幾天發現每次運行都會跑出以下警告 並且沒有聲音

The AudioContext was not allowed to start. It must be resume (or created) after a user gesture on the page. https://goo.gl/7K7WLu

雖然有提示,但是不知道如何在程式裡找出這個AudioContext來resume它

你解决了么

chrome應該有改設定檔
chrome://flags/#autoplay-policy

不過還是得找解決方案

解決了,隨便找個腳本丟進去就行了

public lateUpdate() {
    let context = cc.sys.__audioSupport.context;
    if (context.state === 'suspended') {
        context.resume();
        console.log(context.state);
    }
}
2赞

cocos 2d js 么有 cc.sys.__audioSupport.context。请问有人知道吗

赞一个 就是不知道下个版本的chrome 谷歌以后会不会检测手势点击html标签

WebAudio
First, be reminded that it is good practice to wait for a user interaction before starting audio playback as user is aware of something happening. Think of a "play" button or "on/off" switch for instance. You can also simply add an "unmute" button depending on the flow of the app.

Key Point: An AudioContext must be created or resumed after the document received a user gesture to enable audio playback.
If you create your AudioContext on page load, you’ll have to call resume() later when user interacts with the page (e.g., user clicked a button).

// Existing code unchanged.
window.onload = function() {
  var context = new AudioContext();
  // Setup all nodes
  ...
}

// One-liner to resume playback when user interacted with the page.
document.querySelector('button').addEventListener('click', function() {
  context.resume().then(() => {
    console.log('Playback resumed successfully');
  });
});
You may also create the AudioContext only when user interacts wit the page.

document.querySelector('button').addEventListener('click', function() {
  var context = new AudioContext();
  // Setup all nodes
  ...
});
To detect whether browser will require user interaction to play audio, you can check the state of the AudioContext after you've created it. If you are allowed to play, it should immediately switch to running. Otherwise it will be suspended. If you listen to the statechange event, you can detect changes asynchronously.

For info, checkout the small Pull Request that fixes WebAudio playback due to these autoplay policy changes for https://airhorner.com 

.

Feedback
At the time of writing, Chrome's autoplay policies aren't carved in stone. Please reach out to the Chrome team, ChromiumDev on Twitter to share your thoughts.

好用的

cocos creator 的 ts 项目,也没有找到 这个 ,,,你解决了嗯?

workaround失效了, 現在(Chrome 71)每次contex.resume都會跳警告.
不過這只有音檔用cc.loader.loadRes()才會發生, 拉進property的音檔沒事.

1赞

所有TS項目找不到, 但JS有的都能先轉成any再用, 例如
const context: AudioContext = (cc.sys as any).__audioSupport.context;

不過反正新版本的Chrome也不管用了.

直接用就好了,可以用的