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:
|
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 Note:
|
startTime | Number | No | No | The starting position for playback, measured in seconds. Defaults to |
playbackRate | Number | No | No | The playback speed. Defaults to Value range: 0.5-2.0 |
autoplay | Boolean | No | No | Indicates whether to start audio playback automatically. Valid values are:
If not set, the value defaults to |
loop | Boolean | No | No | Indicates whether to loop the audio continuously. Valid values are:
If not set, the value defaults to |
obeyMuteSwitch | Boolean | No | No | Indicates whether to follow the system mute settings on the user's device. Valid values are:
If not set, the value defaults to Note: This property is only supported on iOS. |
volume | Number | No | No | The audio volume. For example, Value range: 0-1 |
Methods
Method | Description |
Start the audio playback. | |
Pause the audio playback. | |
Stop the audio playback and reset the playback position. | |
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. | |
Clean up and destroy the current | |
Register a callback that is executed when the audio is ready to play (which does not guarantee smooth playback). | |
Remove an event listener previously registered with | |
Register a callback that is executed when the audio starts playing. | |
Remove an event listener previously registered with | |
Register a callback that is executed when the audio is paused. | |
Remove an event listener previously registered with | |
Register a callback that is executed when the audio is stopped. | |
Remove an event listener previously registered with | |
Register a callback that is executed when the audio finishes playing. | |
Remove an event listener previously registered with | |
Register a callback that is executed when an error occurs during audio playback. | |
Remove an event listener previously registered with |
Sample codes
.axml
<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
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);
},
});