• Unlock JavaScript Web Audio in Safari and Chrome

Update: This is now necessary for Chrome, too. You might see this message in the console:

It can be tricky to deal with Web Audio because browser vendors have measures in place to protect users from undesired sound playback. In short, user interaction is required to unlock the AudioContext.

Sanity check: make sure your device is unmuted - I know at least Safari iOS will prevent Web Audio playback if the ringer is set to vibrate.

Here's some small boilerplate code to add the necessary hooks. This is written in ES6 JavaScript, and will work in Safari (iOS and macOS) and Chrome:

Call the function immediately after creating the audio context, like this:

Here's something very similar in ES5:

9 Responses Leave a Reply

' src=

Digbalay Bose

Thanks for the code Matt ! The code worked for safari from macbooks but when I tried it from safari in iphone and ipad, i could not hear any sound.

' src=

James Moore

Thanks. Finally something that worked for me. The only problem I have now is that I have to click the button twice or second click outside the button to hear the audio. After 4 times or so, it just stops working and I have to refresh the browser to get it to work again. Very frustrating!

' src=

Hello How can i add this function in an angular 8 application thank you in advance

' src=

Erik Hermansen

Great little article. It got me past the gesture-unlocking problem I had on Safari. Thanks!

To James Moore or whoever else comes along, I've read that Safari allows just 4 open AudioContext instances for a web page. Maybe that matches the "after 4 times or so, it just stops working". Reusing a single AudioContext instance seems like a good practice in any case.

' src=

The key insight that helped me was discovering that upon user interaction that AudioContext.resume() needed to be called. The comments here also helped me realize a single AudioContext could be unlocked once and reused for as many different audio streams as needed.

Thank you very much for sharing this!

' src=

I believe in the ES6 formula, "context.state" should be "audioCtx.state"

' src=

Eric : I believe in the ES6 formula, "context.state" should be "audioCtx.state"

Thanks! Fixed.

' src=

Thanks a lot! Fixed one condition!!

' src=

Sir. Dude. Bro. This totally rocks. Thank you so much. ??

Leave a Reply

Click here to cancel reply.

  • BIGCITY.MID
  • Unlocking the Yamaha DSR-2000
  • Fixing messed up VGM tracks from Shining Force
  • MEGAfm Teardown
  • Hotkeys in Chip Player JS
  • Chip Player JS
  • Flat-Shaded Polygon 3D Games
  • NES APU Note Table
  • An Update on UMG Watermarks
  • How to type emoji in one keystroke on a Mac
  • A Phase-Aligned Oscilloscope for Web Audio
  • Yamaha DX7 and MIDI in JavaScript
  • Thoughts on Rdio UX
  • Watermark Listening Test Results
  • Enable Mac Volume Control for HDMI and DisplayPort Audio Devices
  • Generate Mac App Icons Photoshop Action
  • Making Sausage: Fixing a Previous Git Commit
  • Shuffle Algorithms for Music
  • Subpixel Letter-Spacing in Webkit
  • Avoid Faux-Bold with Web Fonts
  • Converting to Retina
  • Google Analytics for Backbone Apps
  • Chat for the Workplace
  • Double-Click-Through
  • Music Streaming Snapshot
  • Universal's Audible Watermark
  • WFS Paper Presented at AES 131
  • Mac OS X Speech Synthesis Markup
  • Gaze-Enhanced HDR Viewing
  • OpenCV 2.1 and 2.3 with Visual Studio 2010 Quick Start
  • Google Latitude is Cool
  • Music Smasher: Streaming Music API Mashup
  • Weird Spotify Compression Artifacts
  • Exact MP3 Recompression
  • Multi-Touch for Musical Applications
  • Speech Gender Conversion
  • Voice to NES Music Converter
  • WFS Designer Preview
  • Human Birdwings Hoax
  • Display-Aided Head Tracking
  • Wave field synthesis graphical simulation
  • Wave field synthesis build progress
  • Pandora and Flash Player
  • Wave field synthesis first speaker module
  • Wave field synthesis array plans
  • What’s Wrong with the University of Miami IT Policy
  • Research Topics
  • PADsynth in MATLAB
  • Loudspeaker Array Simulation
  • A note to Digsby developers: Fix file transfers
  • Wave Field Synthesis speakers
  • 4D Wave Field Synthesis
  • Hacking Picasa Web Album URLs
  • How to Fix: DVI or VGA cable doesn't work
  • How to Fix: Buttons don't work on Blackberry Curve
  • Stractor Final Presentation
  • Parallax Starfield HTML+CSS Effect
  • Binauralization
  • Sound Auralization
  • PostgreSQL + PostGIS 1.5
  • Location Based Services Survey
  • Fill In The Blanks
  • Preparing the Playstation Eye for Multitouch
  • Multitouch Air Hockey in Processing
  • DG Centenary Collection
  • Protected: Global Warming
  • Carol Montag
  • Eric Humphrey
  • Jeff Dlouhy
  • Patrick Montag

iOS Safari: audio only recording noise

I'm developing an application that allows voice recording in the web browser. This is working fine on most browsers but I have some issues with iOS Safari. Below you can find an extract of the code, it is not complete but it gives an idea of what's going on.

For the record, I'm using audio recorder polyfill ( https://ai.github.io/audio-recorder-polyfill/ ) in order to achieve recording, as MediaRecorder is not yet available on Safari.

The recorder works fine on all navigators (this including OS X Safari), yet on iOS Safari it only records noice. If I set the volume of my speakers at maximal level I can hear myself speak, but it is from "very far away".

All the online dictaphones/recorders that I found have the same issue, they always recording noise. (Tested with an iPhone 5S, 5SE and X, all up to date).

As required, the AudioContext is created on a user event (in this case a touch on a button), I even made a dirty hack to achieve this with polyfill.

I even tried to change the gain of but that didn't help. Trying to access the audio without setting a media device isn't helping.

I think there is an issue with the media-stream, when hooking it up to HarkJs, no detection is made that a user is talking.

  • Safari Extensions

Instantly share code, notes, and snippets.

@mregio-tic

mregio-tic / fixIOSAudioContext.js

  • Download ZIP
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Embed Embed this gist in your website.
  • Share Copy sharable link for this gist.
  • Clone via HTTPS Clone using the web URL.
  • Learn more about clone URLs
  • Save mregio-tic/9480ace0e261ecc4e97979fa5b52481e to your computer and use it in GitHub Desktop.
  • Skip to main content
  • Select language
  • Skip to search
  • AudioContext

Event handlers

The AudioContext interface represents an audio-processing graph built from audio modules linked together, each represented by an or <video> element, an OscillatorNode, etc.), the audio destination, intermediate processing module (e.g. a filter like BiquadFilterNode or ConvolverNode), or volume control (like GainNode)." href="AudioNode.html"> AudioNode . An audio context controls both the creation of the nodes it contains and the execution of the audio processing, or decoding. You need to create an AudioContext before you do anything else, as everything happens inside a context.

An AudioContext can be a target of events, therefore it implements the EventTarget interface.

Constructor

Also implements methods from the interface EventTarget .

Obsolete methods

Basic audio context declaration:

Cross browser variant:

Specifications

Browser compatibility.

  • Using the Web Audio API
  • OfflineAudioContext

Constructor

Specifications, browser compatibility.

The AudioContext interface represents an audio-processing graph built from audio modules linked together, each represented by an AudioNode . An audio context controls both the creation of the nodes it contains and the execution of the audio processing, or decoding. You need to create an AudioContext before you do anything else, as everything happens inside a context.

Also inherits properties from its parent interface, BaseAudioContext .

Also inherits methods from its parent interface, BaseAudioContext .

Basic audio context declaration:

Cross browser variant:

  • Using the Web Audio API
  • OfflineAudioContext

Document Tags and Contributors

  • Web Audio API
  • AudioContext()
  • baseLatency
  • outputLatency
  • createJavaScriptNode()
  • createMediaElementSource()
  • createMediaStreamDestination()
  • createMediaStreamSource()
  • createMediaStreamTrackSource()
  • createWaveTable()
  • getOutputTimestamp()
  • EventTarget
  • AnalyserNode
  • AudioBuffer
  • AudioBufferSourceNode
  • AudioContextOptions
  • AudioDestinationNode
  • AudioListener
  • AudioNodeOptions
  • AudioProcessingEvent
  • AudioScheduledSourceNode
  • BaseAudioContext
  • BiquadFilterNode
  • ChannelMergerNode
  • ChannelSplitterNode
  • ConstantSourceNode
  • ConvolverNode
  • DynamicsCompressorNode
  • IIRFilterNode
  • MediaElementAudioSourceNode
  • MediaStreamAudioDestinationNode
  • MediaStreamAudioSourceNode
  • OfflineAudioCompletionEvent
  • OscillatorNode
  • PeriodicWave
  • StereoPannerNode
  • WaveShaperNode

Learn the best of web development

Get the latest and greatest from MDN delivered straight to your inbox.

Thanks! Please check your inbox to confirm your subscription.

If you haven’t previously confirmed a subscription to a Mozilla-related newsletter you may have to do so. Please check your inbox or your spam filter for an email from us.

  • | New Account
  • | Log In Remember [x]
  • | Forgot Password Login: [x]
  • Format For Printing
  •  -  XML
  •  -  Clone This Bug
  •  -  Top of page

IMAGES

  1. HTML5 Audio Delay

    audiocontext ios safari

  2. HTML5 Audio Delay

    audiocontext ios safari

  3. 「iOS 15」の「Safari」ではWebM オーディオコーデックをサポートへ

    audiocontext ios safari

  4. ios/safari audioContext with wrong sampleRate · Issue #1141 · goldfire

    audiocontext ios safari

  5. 3招技巧讓iPhone 實現Safari 翻譯網頁功能,iOS用戶必學招式

    audiocontext ios safari

  6. How to enable audio on Safari

    audiocontext ios safari

VIDEO

  1. AMAZING! Your own macOS AI copilot in 2 minutes (using OpenAI Vision/Whisper/TTS)

  2. iOS SAFARI BROWSER FOR ANY ANDROID ❗ #ios #ytshorts #safari

  3. Обзор VK 2.2 для iOS 8 + Как вернуть доступ к вашим аудиозаписям?!

  4. Расширения для Safari на iPhone: для чего нужны, как установить? 5 лучших бесплатных расширений

  5. Francis Forever |Ride the Cyclone |Talia Angst

  6. Как скачивать музыку на ios (iPhone/Ipad) с твиком safari downloader+

COMMENTS

  1. javascript

    this.audioContext = new AudioContext(); While this code may resolve the OP's issue, it is best to include an explanation as to how your code addresses the OP's issue. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge.

  2. Fix iOS AudioContext on Safari not playing any audio. It needs to be

    Fix iOS AudioContext on Safari not playing any audio. It needs to be "warmed up" from a user interaction, then you can play audio with it as normal throughout the rest of the life cycle of the page. - fixIOSAudioContext.js. Fix iOS AudioContext on Safari not playing any audio. It needs to be "warmed up" from a user interaction, then ...

  3. Unlock JavaScript Web Audio in Safari and Chrome

    It can be tricky to deal with Web Audio because browser vendors have measures in place to protect users from undesired sound playback. In short, user interaction is required to unlock the AudioContext. Sanity check: make sure your device is unmuted - I know at least Safari iOS will prevent Web Audio playback if the ringer is set to vibrate.

  4. iOS Safari: audio only recording n…

    This is working fine on most browsers but I have some issues with iOS Safari. Below you can find an extract of the code, it is not complete but it gives an idea of what's going on. //Create new audio context. let audioContext = new (window.AudioContext || window.webkitAudioContext); //Hack polyfill media recorder to re-use the audioContex.

  5. AudioContext

    Safari Desktop 10.0+ Safari Mobile 9.0+. interface AudioContext. Topics.

  6. Programmatically play a sound with web audio, even on iOS.

    In order to play a sound programmatically on iOS Safari, the so-called audio context needs to be unlocked first. That unlocking has to be done, again, by playing a sound as a reaction to user interaction. Luckily, the audio context can be unlocked once and then forever be used to play sounds. So any touch event on the screen can be used for that.

  7. Fix iOS AudioContext on Safari not playing any audio. It needs to be

    Fix iOS AudioContext on Safari not playing any audio. It needs to be "warmed up" from a user interaction, then you can play audio with it as normal throughout the rest of the life cycle of the page. - fixIOSAudioContext.js. Fix iOS AudioContext on Safari not playing any audio. It needs to be "warmed up" from a user interaction, then ...

  8. AudioContext

    The AudioContext interface represents an audio-processing graph built from audio modules linked together, each represented by an AudioNode. An audio context controls both the creation of the nodes it contains and the execution of the audio processing, or decoding. You need to create an AudioContext before you do anything else, as everything ...

  9. Web Audio API

    General containers and definitions that shape audio graphs in Web Audio API usage. AudioContext. The AudioContext interface represents an audio-processing graph built from audio modules linked together, each represented by an AudioNode.An audio context controls the creation of the nodes it contains and the execution of the audio processing, or decoding.

  10. Playing Sounds with the Web Audio API

    Note: The Web Audio API is available on Safari 6 and later, and Safari on iOS 6 and later. In Audio and Video HTML , you learned how to stream audio using the <audio> HTML5 element. While the <audio> tag is suitable for basic needs such as streaming and media playback, another option called the Web Audio API offers a more comprehensive audio ...

  11. AudioContext

    The AudioContext interface represents an audio-processing graph built from audio modules linked together, each represented by an AudioNode.An audio context controls both the creation of the nodes it contains and the execution of the audio processing, or decoding. You need to create an AudioContext before you do anything else, as everything happens inside a context.

  12. BaseAudioContext: state property

    The audio context has been suspended (with the AudioContext.suspend() method.) running. The audio context is running normally. closed. ... In iOS Safari, when a user leaves the page (e.g. switches tabs, minimizes the browser, or turns off the screen) the audio context's state changes to "interrupted" and needs to be resumed. ...

  13. Web Audio API on iPad

    No problems on iPhone or other compatible browsers. The Apple documentation makes it clear that audio must be initiated from a user action for Web Audio API to work. They state: Note: On iOS, the Web Audio API requires sounds to be triggered from an explicit user action, such as a tap. Calling noteOn () from an onload event will not play sound.

  14. AudioContext

    The AudioContext interface represents an audio-processing graph built from audio modules linked together, each represented by an AudioNode. An audio context controls both the creation of the nodes it contains and the execution of the audio processing, or decoding. You need to create an AudioContext before you do anything else, as everything happens inside a context.

  15. 231105

    We noticed starting iOS 15 with web audio in Safari the audio stops playing after the device is locked, or in some cases when Safari is backgrounded and resumed or an incoming call/notification comes in. ... - On iOS, AudioContext gets suspended when going in the background, whether playing audio or not. In that case, AudioContext can be ...

  16. No sound in safari using Web Audio API webkitAudioContext()

    1. I am trying to use the Web Audio API to play sound in my React application. It's currently playing sound in all browsers except Safari v12.1. I am aware Safari has restrictions on autoplay and requires user interaction to play sound, so I have a play button which calls the _play() function: _play = (url, index) => {. this._getData(url);

  17. AudioContext: AudioContext() constructor

    options Optional. An object used to configure the context. The available properties are: latencyHint Optional. The type of playback that the context will be used for, as a predefined string ("balanced", "interactive" or "playback") or a double-precision floating-point value indicating the preferred maximum latency of the context in seconds.The user agent may or may not choose to meet this ...

  18. Can't get Web Audio API to work with iOS 11 Safari

    So iOS 11 Safari was supposed to add support for the Web Audio API, but it still doesn't seem to work with this javascript code: //called on page load get_user_media = get_user_media || navigator. ... replaced var audio_context = new AudioContext(); with var audio_context = window.webkitAudioContext; but doesnt seem to help - Artikash ...

  19. AudioContext: createMediaStreamSource() method

    The createMediaStreamSource() method of the AudioContext Interface is used to create a new MediaStreamAudioSourceNode object, given a media stream (say, from a MediaDevices.getUserMedia instance), the audio from which can then be played and manipulated.. For more details about media stream audio source nodes, check out the MediaStreamAudioSourceNode reference page.