tencent cloud

Tencent Cloud Super App as a Service

문서Tencent Cloud Super App as a Service

Audio

다운로드
포커스 모드
폰트 크기
마지막 업데이트 시간: 2025-03-25 18:15:56
Note: If you are developing with a game engine, the engine may already have audio handling integrated. In that case, you should use the components provided by the game engine.

InnerAudio

InnerAudio provides a simple and efficient way to play audio, supporting streaming playback. It is suitable for playing larger audio files, such as background music.

Relevant APIs

Audio configuration:setInnerAudioOption

Example

const innerAudioContext = wx.createInnerAudioContext();
innerAudioContext.src = 'https://wx_test.mp3'; // Replace with your own link
innerAudioContext.loop = true; // Set to loop playback
// innerAudioContext.autoplay = true; // Automatically start playing when ready
innerAudioContext.onError(res => {
console.log('Error', res);
});
innerAudioContext.onPlay(() => {
console.log('Playback started')
});
innerAudioContext.onCanplay(() => {
console.log('Ready to play')
innerAudioContext.play(); // Start playback
});
innerAudioContext.onStop(() => {
console.log('Playback stopped')
})
innerAudioContext.onEnded(() => {
console.log('Playback ended')
});
innerAudioContext.pause(); // Pause
innerAudioContext.stop(); // Stop
innerAudioContext.destroy(); // Release audio resources

wx.onShow(() => {
innerAudioContext.play(); // Resume playback when the mini game returns to the foreground
})

wx.onAudioInterruptionEnd(function () {
innerAudioContext.play() //Resume playback after being interrupted by another event (e.g., a phone call)
})

Supported formats

The supported audio formats may vary between iOS and Android. For more information, please see Audio.

Usage Suggestions

Reusing existing audio instances

For the same sound effect, you should reuse existing audio instances instead of creating new ones.

Destroy unneeded audio instances promptly

If an audio instance is no longer needed, you can call InnerAudioContext.destroy() to destroy the instance and free up resources.

Limitation on simultaneous audio playback

Due to system limitations, a maximum of 10 audio files can be played simultaneously on Android. Any additional audio files will be handled with lossy processing, which is not noticeable to developers but should be avoided by minimizing the number of simultaneous audio playbacks.





도움말 및 지원

문제 해결에 도움이 되었나요?

피드백