# Basic audio interface
Create [InnerAudioContext] (https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/wx.createInnerAudioContext.html) through the basic frequency interface [wx..qq.com/miniprogram/dev/api/media/audio/InnerAudioContext.html) object, use this object to play 2D audio. This way of playing audio is simple and fast, supports streaming, but its performance is slightly lower than that of using audio system, which is suitable for playing larger audio files.
# Play with InnerAudioContext
An audio instance innerAudioContext can be created through the wx.createInnerAudioContext() interface, and audio can be played through this instance.
var audio = wx.createInnerAudioContext()
audio.src = url // src can set http(s) path, local file path or code package file path
audio.play()
On iOS, the mute key setting is followed by default. If you want to play sound even when muted, you can set obeyMuteSwitch
to false
.
audio.obeyMuteSwitch = false
# Auto play and loop play
Set the autoplay
and loop
properties to automatically play and loop audio, which is generally suitable for background music.
var bgm = wx.createInnerAudioContext()
bgm.autoplay = true
bgm.loop = true
bgm.src = url
# Restore background music when back to the front desk
When the mini game is hidden to the background, all audio will be paused and can no longer be played successfully before returning to the foreground.
After returning to the foreground, the paused audio will not automatically continue to play. If the mini game has background music, you need to monitor the return to the foreground event, and call the background music to continue playing after receiving the return to the foreground event.
wx.onShow(function () {
bgm.play()
})
# Handling audio interruption events
Audio interruption events refer to events that are triggered when the audio is interrupted by the system during the game. Audio interruption events are divided into interrupt start and interrupt end events, which are monitored using wx.onAudioInterruptionBegin() and wx.onAudioInterruptionEnd() respectively.
The following events will trigger the audio interruption start event: call received, alarm ringing, system reminder, voice/video call request received from WeChat friends. After being interrupted, all audio in the mini game will be paused, and can no longer be played successfully until the interruption ends.
After the interruption ends, the paused audio will not automatically continue to play. If the mini game has background music, you need to monitor the audio interruption end event, and call the background music to continue playing after receiving the interruption end event.
wx.onAudioInterruptionEnd(function () {
bgm.play()
})
If the logic of the mini game is strongly dependent on the playing of music, you need to pause the game when the audio starts to interrupt
wx.onAudioInterruptionBegin(function () {
// Pause the game
})
# Supported format
Refer to the supported audio resource format. In addition, using the basic audio interface, different audio formats will be different between iOS and Android, please refer to [here](https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/InnerAudioContext.html# %E6%94%AF%E6%8C%81%E6%A0%BC%E5%BC%8F).
# Get started quickly
Click here Learn how to use the wx.createInnerAudioContext interface to play audio.
# Best Practices
# Reuse existing audio examples
For the same sound effect, you should reuse the existing audio instance instead of recreating an audio instance.
# Destroy unnecessary audio instances in time
If an audio is no longer needed, you can call the InnerAudioContext.destroy() interface to destroy the instance in advance.
# Android limit on the number of audios that can be played at the same time
Due to system limitations, up to 10 audios can be played at the same time on Android, and the excess part will be lossy, which is not perceptible to developers, but developers should try to avoid playing too many audios at the same time.