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

twrAudioPlayFile issues for Safari #58

Open
JohnDog3112 opened this issue Nov 13, 2024 · 0 comments
Open

twrAudioPlayFile issues for Safari #58

JohnDog3112 opened this issue Nov 13, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@JohnDog3112
Copy link
Collaborator

JohnDog3112 commented Nov 13, 2024

I'm finally moving this to another issue from #48 about audio playback issues when using twrAudioPlayFile on Safari.

Browsers have a set of permissions(?) for playing audio on web pages. The main purpose of this is to prevent people from opening a web page and immediately having really loud audio auto-play into the user's headphones. This is why tests-audio makes you click a button to start the test. This button press is enough of an interaction for browsers to "unlock" the ability for JavaScript to play audio.

However, Safari has a weird exception. In the current implementation of the audio library, there are two ways that audio is played:

  1. AudioContext: This acts as a global audio pipeline audio streams can be set to output to.
  2. HTMLAudioElement: This is a built-in audio element in HTML that is given a URL to load and adds easier-to-use properties for control. If the audio element was added in the HTML it would come with user controls for volume, pause/play, and playback location, however, when creating it with pure JS the interface isn't available to the user.

In this case, Safari has weird permission settings for the 2nd method which is used for twrAudioPlayFile. For the AudioContext, a single button press remains enough to start playing audio. However, with respect to the HTMLAudioElement, Safari requires the user to click the play/button on each individual HTMLAudioElement via its interface. This is a problem in two respects:

  1. The interface is never exposed to the user since it's supposed to be done programmatically
  2. This would either necessitate having the user click the play button every time a sound is played via this method or, would involve having a limited pool of HTMLAudioElements that the user has to click on that are then sort of passed around with their URL being changed as a new URL is played.

I couldn't find a way around this permission system while using an HTMLAudioElement so I added an alternative play method for twrAudioPlayFile. So, if the HTMLAudioElement fails to play, it will fetch the audio file, decode it, and then play it through the AudioContext instead. I'll try to look into it a bit more to see if there is a better workaround, but I'll leave this workaround in until then.

flowchart TD
    G[twrAudioPlayFile] -->|URL| H(Create HTMLAudioElement)
    H --> I{Play Audio}
    I -->|Success| J(Carry on)
    I -->|NotAllowedError| K(fetch Audio URL)
    K --> L(Decode Audio Data)
    L --> M(Play Audio)
   M --> J
Loading
@JohnDog3112 JohnDog3112 self-assigned this Nov 13, 2024
@JohnDog3112 JohnDog3112 added the bug Something isn't working label Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant