# Audio Play
There is only one way to play audio in Mini Game, that is, play with InnerAudioContext.
# Play with InnerAudioContext
An audio instance innerAudioContext can be created via the wx.createInnerAudioContext() interface, through which audio can be played.
var audio = wx.createInnerAudioContext()
audio.src = url // src can set the paths of http(s), local file or code package file
audio.play()
On iOS systems, follow silent switch settings by default. If you want to be able to play sounds even when muted, set obeyMuteSwitch
to false
.
audio.obeyMuteSwitch = false
# Auto Play and Loop Play
Setting the autoplay
and loop
attributes can 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 Returning to Foreground When the Mini Game is hidden in the background, all audio is paused and cannot be played until it returns to the foreground.
After returning to the foreground, the paused audio does not automatically continue to play. If the Mini Game has background music, it needs to subscribe back to the foreground event and call the background music to continue playing after receiving event of returning to the foreground.
wx.onShow(function () {
bgm.play()
})
# Handling Audio Interrupt Events
An audio interruption event refers to an event that is triggered when the audio is interrupted by the system during the game. The audio interruption event are divided into interruption begin and interruption end events, which are subscribed using wx.onAudioInterruptionBegin() and wx.onAudioInterruptionEnd() respectively.
The following events will trigger an audio interruption begin event: a phone call, an alarm ring, a system alert, and a Weixin friend's voice/video call request. After being interrupted, all audio in the Mini Game will be paused and will not be played until the interruption ends.
After the interruption ends, the paused audio does not automatically continue to play. If the game has background music, it needs to subscribe the audio interruption end event and call the background music to continue playing after receiving the interrupt 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 break.
wx.onAudioInterruptionBegin(function () {
// Pause the game
})
# Compatibility Note
Currently, the audio formats fully supported by the two platforms are mp3, aac, and wav. There are system differences in other formats and there is no guarantee of compatibility.
# Best Practices
# Reuse Existing Audio Instances
For the same sound effects, existing audio instances should be reused instead of recreating an audio instance.
# Destroy Undesired Audio Instances in Time
If an audio is no longer needed, this instance can be destroyed in advance by calling the InnerAudioContext.destroy() interface.
# Limit on the number of audio played at the same time by Android
Due to system limitations, up to 10 audios can be played simultaneously on Android. The excess will be lossy processed and will not be perceived by developers. However, developers should avoid playing too much audio at the same time.