InnerAudioContext Overview

The InnerAudioContext object serves as a foreground audio manager for controlling audio playback. This topic provides a detailed overview of its properties, methods, and sample codes.

Properties

Note: If you assign an invalid value to a non-read-only property, the value does not take effect.

Property

Type

Read-only

Required

Description

duration

Number

Yes

N/A

The total length of the audio, measured in seconds.

paused

Boolean

Yes

N/A

Indicates whether the audio is paused or stopped. Valid values are:

  • true: The audio is paused or stopped.
  • false: The audio is playing.

currentTime

Number

Yes

N/A

The current playback position of the audio, measured in seconds. This value is a floating-point number.

buffered

Number

Yes

N/A

The time point up to which audio content has been buffered. It guarantees that audio content from the current playback time to this point is loaded.

src

String

No

Yes

The audio source URL. Defaults to an empty string. If a new src is set, the audio automatically starts playing.

Note:

  • Set this property using either network URLs or mini program paths.
  • If this property is not set, no audio will play, and the system will not raise any errors or exceptions.

startTime

Number

No

No

The starting position for playback, measured in seconds. Defaults to 0. The value must be between 0 and the audio length.

playbackRate

Number

No

No

The playback speed. Defaults to 1.

Value range: 0.5-2.0

autoplay

Boolean

No

No

Indicates whether to start audio playback automatically. Valid values are:

  • true: Start playback automatically.
  • false: Do not start playback automatically.

If not set, the value defaults to false.

loop

Boolean

No

No

Indicates whether to loop the audio continuously. Valid values are:

  • true: Loop the audio continuously.
  • false: Do not loop the audio.

If not set, the value defaults to false.

obeyMuteSwitch

Boolean

No

No

Indicates whether to follow the system mute settings on the user's device. Valid values are:

  • true: Mute the audio when the device is muted.
  • false: Play sound even when the device is muted.

If not set, the value defaults to true.

Note: This property is only supported on iOS.

volume

Number

No

No

The audio volume. For example, this.innerAudioContext.volume = 0.5.

Value range: 0-1

Methods

Method

Description

InnerAudioContext.play

Start the audio playback.

InnerAudioContext.pause

Pause the audio playback.

InnerAudioContext.stop

Stop the audio playback and reset the playback position.

InnerAudioContext.seek

Jump to a specific position in the audio playback. The position is specified in seconds. The value supports up to 3 decimal places, allowing for millisecond precision.

InnerAudioContext.destroy

Clean up and destroy the current InnerAudioContext instance, releasing resources.

InnerAudioContext.onCanPlay

Register a callback that is executed when the audio is ready to play (which does not guarantee smooth playback).

InnerAudioContext.offCanPlay

Remove an event listener previously registered with InnerAudioContext.onCanPlay, which stops monitoring the event that occurs when the audio is ready to play.

InnerAudioContext.onPlay

Register a callback that is executed when the audio starts playing.

InnerAudioContext.offPlay

Remove an event listener previously registered with InnerAudioContext.onPlay, which stops monitoring the event that occurs when the audio starts playing.

InnerAudioContext.onPause

Register a callback that is executed when the audio is paused.

InnerAudioContext.offPause

Remove an event listener previously registered with InnerAudioContext.onPause, which stops monitoring the event that occurs when the audio is paused.

InnerAudioContext.onStop

Register a callback that is executed when the audio is stopped.

InnerAudioContext.offStop

Remove an event listener previously registered with InnerAudioContext.onStop, which stops monitoring the event that occurs when the audio is stopped.

InnerAudioContext.onEnded

Register a callback that is executed when the audio finishes playing.

InnerAudioContext.offEnded

Remove an event listener previously registered with InnerAudioContext.onEnded, which stops monitoring the event that occurs when the audio finishes playing.

InnerAudioContext.onError

Register a callback that is executed when an error occurs during audio playback.

InnerAudioContext.offError

Remove an event listener previously registered with InnerAudioContext.onError, which stops monitoring the event that occurs when an error occurs during playback.

Sample codes

.axml

copy
<view>
  <view>
    <button
      type="primary"
      onTap="playInnerAudio"
      >
      playAudio
    </button>
  </view>
  <view>
    <button
      type="primary"
      onTap="pauseInnerAudio"
      >
      pauseAudio
    </button>
  </view>
  <view>
    <button
      type="primary"
      onTap="stopInnerAudio"
      >
      stopAudio
    </button>
  </view>
  <view>
    <button
      type="primary"
      onTap="seekInnerAudio"
      >
      seekAudio
    </button>
  </view>
</view>

.js

copy
Page ({
  onLoad () {
    this.innerAudioContext = my.createInnerAudioContext();
    this.innerAudioContext.src = 'https://www.example.com';
    this.innerAudioContext.autoplay = false;
    this.innerAudioContext.loop = false;
    this.innerAudioContext.obeyMuteSwitch = false;

    this.innerAudioContext.onPlay(() => {
      console.log('innerAudioContext onPlay');
    });
    this.innerAudioContext.onPause(() => {
      console.log ('innerAudioContext onPause');
    });
    this.innerAudioContext.onStop(() => {
      console.log ('innerAudioContext onStop');
    });
    this.innerAudioContext.onError(err => {
      console.log ('innerAudioContext onError', err);
    });
  },
  playInnerAudio() {
    console.log('innerAudioContext.play');
    this.innerAudioContext.play();
  },
  stopInnerAudio() {
    console.log('innerAudioContext.stop');
    this.innerAudioContext.stop();
  },
  pauseInnerAudio() {
    console.log('innerAudioContext.pause');
    this.innerAudioContext.pause();
  },
  seekInnerAudio() {
    console.log('innerAudioContext.seek');
    this.innerAudioContext.seek(20);
  },
});