Skip to content
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

Doesn't seem to work in Safari #23

Open
nanandn opened this issue Aug 19, 2021 · 3 comments
Open

Doesn't seem to work in Safari #23

nanandn opened this issue Aug 19, 2021 · 3 comments

Comments

@nanandn
Copy link

nanandn commented Aug 19, 2021

Does this work in Safari. I tried the demo https://esonderegger.github.io/web-audio-peak-meter/
Doesn't do anything. Can't even hear the audio.

Anand

@esonderegger
Copy link
Owner

I'm sorry for taking so long to reply to this and thank you for flagging!

The root cause of this appears to be that using an <audio> element with the controls attribute doesn't capture the user clicking the play button as an event capable of triggering the AudioContext to resume, like it does in Chrome.

The workaround appears to be using custom controls that control the element via javascript. For example, using a <button> element that tells the audio element to play, and then listening for the play event on the audio element, as done in the examples/docs appears to be sufficient.

Example code:

<button id="start-button">Start</button>
<audio id="the-audio">
  <source src="/some_audio.mp3" type="audio/mpeg" />
</audio>
<script>
const audioCtx = new AudioContext();
const buttonElement = document.getElementById('start-button');
const audioElement = document.getElementById('the-audio');
buttonElement.addEventListener('click', () => {
  audioElement.play();
});
audioElement.addEventListener('play', () => {
  audioCtx.resume();
});
const sourceNode = audioCtx.createMediaElementSource(audioElement);
sourceNode.connect(audioCtx.destination);
</script>

@nanandn
Copy link
Author

nanandn commented Jan 26, 2022

@esonderegger Thanks for the response. Actually, In my application, I am connecting a video element. That doesn't work in safari either. I followed the code in #4

Seems like createScriptProcessor is deprecated (in favor of Analyzer node). Maybe that doesn't work in Safari?

Also, I want to mention another issue. Maybe I should do this in a separate thread, but...
When the view (react element) in which the audio meter is rendered is resized, the audio meter does not resize accordingly. Seems like the height and width are set when it is created and does not respond to resizing properly.

BTW: Great tool. Saved a lot of time for me(in understanding the audio APIs etc..). Are you actively maintaining it and planning on making enhancements.

@suterma
Copy link

suterma commented Mar 24, 2023

@nanandn The resizing part is addressed in #27

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants