JSPM

ios-safe-audio-context

1.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 20
  • Score
    100M100P100Q49102F
  • License MIT

create a WebAudio context that works in iOS and everywhere else

Package Exports

  • ios-safe-audio-context

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ios-safe-audio-context) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

ios-safe-audio-context

experimental

Creates a AudioContext that works as expected in desktop and mobile, particularly across iOS devices.

Motivation

There is a bug in iOS where the AudioContext sampleRate is sometimes not what you would expect, and as a result, all WebAudio plays with heavy distortion. This occurs when you play an audio/video element with a different sample rate, or when you first boot up Safari (tested on iOS9.2, iPhone5S, no headphones).

To get around this, we try to detect a broken state, and if so, play a dummy buffer before returning a new AudioContext which should have the desired sample rate (default 44100).

Example

On iOS, this function must be called from user gesture event, such as touchend.

const createAudioContext = require('ios-safe-audio-context')

clickToPlay.addEventListener('touchend', () => {
  const audioContext = createAudioContext()
  
  // now you can use this context for playback
})

Usage

NPM

context = createAudioContext([desiredSampleRate])

Returns a new AudioContext, only applying the hack if we detect a broken state (iOS). desiredSampleRate defaults to 44100.

License

MIT, see LICENSE.md for details.