# startaudiocontext

> Start the AudioContext on an explicit user action.

Latest version **1.2.1** (published 2017-05-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install startaudiocontext
pnpm add startaudiocontext
yarn add startaudiocontext
bun add startaudiocontext
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.1 |
| Published | 2017-05-10 |
| First published | 2016-01-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 90 |
| Author | Yotam Mann |
| Maintainers | yotam, tambien |
| Keywords | Web, Audio, API |

## Links

- npm: https://www.npmjs.com/package/startaudiocontext
- Repository: https://github.com/tambien/StartAudioContext
- Homepage: https://github.com/tambien/StartAudioContext#readme
- Issues: https://github.com/tambien/StartAudioContext/issues
- npm.io page: https://npm.io/package/startaudiocontext

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 1.2.1 (latest) — 2017-05-10
- 1.2.0 — 2016-09-14
- 1.1.0 — 2016-02-25
- 1.0.0 — 2016-01-25

## README

StartAudioContext starts the Web Audio API's AudioContext on an explicit user action. 

According to the [Apple's documentation](https://developer.apple.com/library/safari/documentation/AudioVideo/Conceptual/Using_HTML5_Audio_Video/PlayingandSynthesizingSounds/PlayingandSynthesizingSounds.html): 
> 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.

StartAudioContext listens for the first non-dragged `touchend` or `mouseup` event on any of the given elements, then triggers a silent AudioBuffer which will start the AudioContext if it isn't already started.

## Installation

Choose one:

* [download the js](https://raw.githubusercontent.com/tambien/StartAudioContext/master/StartAudioContext.js)
* `npm install startaudiocontext`
* `bower install startaudiocontext`

## Basic

```javascript
//pass in the audio context
var context = new AudioContext();

//on iOS, the context will be started on the first valid user action on the #playButton element
StartAudioContext(context, "#playButton")
```

## API

`StartAudioContext(AudioContext, (Elements), (Callback)) => Promise`

#### AudioContext

StartAudioContext will monitor the passed in AudioContext and resolve the promise and/or invoke the passed in callback when the `AudioContext.state === 'running'`. 

#### Elements

The second argument can be an Element, Selector String, NodeList, jQuery Element or an Array of any of those. An event listener is bound to any of the passed in elements which will listen for a valid touch events to trigger a silent AudioBuffer which will start the AudioContext.

```javascript
StartAudioContext(audioContext, '#button', function(){
	//audio context is started
})
```

If no element is passed in, it will bind to document.body.

#### Callback

The third argument is the callback to invoke when the AudioContext has started.

#### => Promise

StartAudioContext returns a promise which is resolved when the AudioContext state is 'running'. 

```javascript
StartAudioContext(audioContext).then(function(){
	//context is started	
})
```

### Tone.js

If you're using StartAudioContext with Tone.js or any other library which creates an AudioContext, pass in that AudioContext instead of creating a new one:

```javascript
StartAudioContext(Tone.context, '#button').then(function(){
	//started
})
```

## MIT License

Copyright 2016 Yotam Mann

---
_Source: https://npm.io/package/startaudiocontext · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
