> ## Documentation Index
> Fetch the complete documentation index at: https://docs.rebellapp.com/llms.txt
> Use this file to discover all available pages before exploring further.

# my.createInnerAudioContext

> Create a foreground audio context to manage audio playback.

Creates a foreground audio context (**InnerAudioContext**) for managing audio playback in the Mini Program: play, pause, stop, play from a specific time, and configure volume and looping. Foreground audio stops automatically when the user exits the Mini Program or the screen turns off.

```javascript theme={null}
const innerAudioContext = my.createInnerAudioContext();
```

## Return Value

| Type | Description |
| - | - |
| InnerAudioContext | Foreground audio manager with the properties and methods below |

## InnerAudioContext Properties

Assigning an invalid value to a writable property has no effect.

| Property | Type | Read-only | Description |
| - | - | - | - |
| src | String | No | Audio source: a network URL or a Mini Program path. Default: empty string. Setting a new `src` starts playback automatically; if not set, nothing plays and no error is raised |
| startTime | Number | No | Start position in seconds, between 0 and the audio length. Default: `0` |
| playbackRate | Number | No | Playback speed, `0.5`–`2.0`. Default: `1` |
| autoplay | Boolean | No | Start playback automatically. Default: `false` |
| loop | Boolean | No | Loop the audio. Default: `false` |
| obeyMuteSwitch | Boolean | No | Follow the device mute switch. Default: `true`. iOS only |
| volume | Number | No | Volume, `0`–`1` |
| duration | Number | Yes | Total length of the audio, in seconds |
| paused | Boolean | Yes | `true` if paused or stopped, `false` if playing |
| currentTime | Number | Yes | Current playback position, in seconds (floating point) |
| buffered | Number | Yes | Position up to which audio is buffered |

## InnerAudioContext Methods

| Method | Description |
| - | - |
| play() | Start playback |
| pause() | Pause playback |
| stop() | Stop playback and reset the position |
| seek(position) | Jump to `position` (seconds, up to 3 decimal places, between 0 and the audio length). Returns `{ error, errorMessage }` on failure; error `2` means invalid parameters |
| destroy() | Destroy the instance and release its resources |

## InnerAudioContext Events

Each `on*` method registers a listener; the matching `off*` method removes the listener passed in, or all listeners of that event if none is passed.

| Listener | Remove with | Triggered when |
| - | - | - |
| onCanPlay(listener) | offCanPlay | The audio is ready to play (enough data buffered; smooth playback is not guaranteed) |
| onPlay(listener) | offPlay | Playback starts |
| onPause(listener) | offPause | Playback is paused |
| onStop(listener) | offStop | Playback is stopped |
| onEnded(listener) | offEnded | Playback finishes |
| onError(listener) | offError | A playback error occurs; the listener receives `errCode` (Number) and `errMessage` (String) |

### onError Error Codes

| Code | Message | Action |
| - | - | - |
| 10007 | Audio src error | Check that the audio path is correct and the resource is valid |
| -1 | Unknown error | Check that the foreground audio resource is valid |

## Code Example

```javascript theme={null}
Page({
  onLoad() {
    this.audio = my.createInnerAudioContext();
    this.audio.src = 'https://example.com/sound.mp3';
    this.audio.autoplay = false;
    this.audio.loop = false;
    this.audio.volume = 0.8;
    this.onEnded = () => console.log('Playback finished');
    this.onError = (res) => console.error('Audio error', res.errCode, res.errMessage);
    this.audio.onEnded(this.onEnded);
    this.audio.onError(this.onError);
  },
  play() {
    this.audio.play();
  },
  pause() {
    this.audio.pause();
  },
  seekTo30() {
    this.audio.seek(30);
  },
  onUnload() {
    this.audio.offEnded(this.onEnded);
    this.audio.offError(this.onError);
    this.audio.destroy();
  }
});
```

## Related APIs

> [card] [my.getRecorderManager](https://docs.rebellapp.com/jsapi/media/recorder/my.getRecorderManager.md)
>
> Record audio

> [card] [my.createVideoContext](https://docs.rebellapp.com/jsapi/media/video/my.createVideoContext.md)
>
> Control video playback


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.