Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

webkitaudiocontext - How can I detect if Safari is really playing an audio file with webkit audio?

I am working on a multiuser webpage which plays sounds when buttons are pressed by any user. The system uses websockets to communicate who press what and when.

When a sound is initiated by the user pressing a button it works fine, and future playings of that sound then work when received via websockets from another user.

When a sound is initiated first from websockets, Safari doesn't play it, which I understand because it wasn't initiated from a user's action. The problem I am having is detecting when this has occurred.

Here is my code, snipped from the original (including snipping error handling) to contain what I think is just the relevant parts:

var source = context.createBufferSource(); 
source.buffer = buffer; // tell the source which sound to play
context.createGainNode();
source.connect(volNode); 
volNode.connect(context.destination);
volNode.gain.setValueAtTime(startVol, context.currentTime);
source.loop = false;

source.onended = function () {
   console.log('Ended');
};
 
source.start();

Examining source, I cannot tell the difference between when it plays sound and when it doesn't. playbackState is 1 in both cases and the volume is 1 in both cases.

Is there a way to tell whether it worked or not? I would like to present the user with a play button if it failed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...