tencent cloud

All product documents
Tencent Cloud Super App as a Service
Last updated: 2025-02-24 17:10:36
Audio
Last updated: 2025-02-24 17:10:36

stopVoice

This API is called using wx.stopVoice(Object object).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Stops the voice playback.
Parameter and description:Object object.
Property
Type
Default value
‍Required
Description
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
Example:
wx.startRecord({
success (res) {
const tempFilePath = res.tempFilePath
wx.playVoice({
filePath: tempFilePath,
})

setTimeout(() => { wx.stopVoice() }, 5000)
}
})

playVoice

This API is called using wx.playVoice(Object object).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Starts the voice playback. Only one voice file can be played at a time. If a previous voice file is still playing, it will be interrupted.
Parameter and description:
Property
Type
Default value
‍Required
Description
filePath
string
-
True
The file path of the voice file to be played (local path).
duration
number
60
False
Specifies the duration of playback in seconds. Playback will stop automatically after this duration.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
Example:
wx.startRecord({
success (res) {
const tempFilePath = res.tempFilePath
wx.playVoice({
filePath: tempFilePath,
complete () { }
})
}
})

pauseVoice

This API is called using wx.pauseVoice(Object object).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Pauses the voice playback. When you call wx.playVoice to play the same file again, it starts from where the file was paused. If you want to play from the beginning, you first need to call wx.stopVoice.
Parameter and description:Object object.
Property
Type
Default value
‍Required
Description
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
Example:
wx.startRecord({
success (res) {
const tempFilePath = res.tempFilePath
wx.playVoice({
filePath: tempFilePath
})

setTimeout(() => { wx.pauseVoice() }, 5000)
}
})

setInnerAudioOption

This API is called using wx.setInnerAudioOption(Object object).
Feature description:Sets the playback options for InnerAudioContext, and applies globally to the current mini program.
Parameter: Object object.
Property
Type
Default value
‍Required
Description
mixWithOther
boolean
true
False
Whether to mix with other audio. If set to true, it won't stop other app's music.
obeyMuteSwitch
boolean
true
False
(iOS only) Whether to follow the mute switch. If set to false, it can play sound even in silent mode.
speakerOn
boolean
true
False
true means using the speaker, false means using the handset. Default is true.
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).

getAvailableAudioSources

Note:
This API is supported in mini programs but not in mini games.
This API is called using wx.getAvailableAudioSources(Object object).
Feature description:Gets the supported audio input sources.
Parameter and description:Object object.
Property
Type
Default value
‍Required
Description
success
function
-
False
Callback function for successful API calls.
fail
function
-
False
Callback function for failed API calls.
complete
function
-
False
Callback function executed after API call ends (regardless of success or failure).
object.success callback function parameter: Object res.
Property
Type
Description
audioSources
Array.<string>
The list of supported audio input sources. For values, see AudioSource
AudioSources valid values
Value
Description
auto
Automatically set. Uses phone mic by default, switches to headset mic when plugged in. Applicable to all platforms.
buildInMic
Phone microphone, iOS only.
headsetMic
Headset microphone, iOS only.
mic
Microphone (phone mic when no headset, headset mic when plugged in), Android only.
camcorder
Same as mic, suitable for recording audio/video content, Android only.
voice_communication
Same as mic, suitable for real-time communication, Android only.
voice_recognition
Same as mic, suitable for voice recognition, Android only.

createAudioContext

This API is called using AudioContext wx.createAudioContext(string id, Object this).
Note:
This API is supported in mini programs but not in mini games.
Feature description:Creates an AudioContext object.
Parameter and description:String id: The ID of the audio component. Object this: In custom components, this refers to the current component instance to operate on the audio component within.
Return value:AudioContext

AudioContext

Note:
This API is supported in mini programs but not in mini games.
An AudioContext instance that can be obtained using wx.createAudioContext. It binds to an audio component via its ID and allows operations on the corresponding audio component.

createInnerAudioContext

This API is called using InnerAudioContext wx.createInnerAudioContext(Object object).
Feature description:Creates an internal audio context, i.e., an InnerAudioContext object.
Return value:InnerAudioContext.
Note:InnerAudioContext audio resources are not automatically released. If the audio is no longer needed, call InnerAudioContext.destroy() to release resources and avoid memory leaks.
Example:
const innerAudioContext = wx.createInnerAudioContext()
innerAudioContext.src = 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&amp;uin=346897220&amp;vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&amp;fromtag=46'

innerAudioContext.play() // Play the audio

innerAudioContext.pause () // Pause the audio

innerAudioContext.stop () // Stop the audio

innerAudioContext.destroy () // Release audio resources

InnerAudioContext

Property

string src: The URL of the audio resource to play.
number startTime: The position to start playing from (in seconds), default is 0.
boolean autoplay: Whether to start playing automatically, default is false.
boolean loop: Whether to loop the audio, default is false.
number volume: The volume level, ranging from 0 to 1. Default is 1.
number duration: The length of the current audio (in seconds). Returns only if there is a valid src (read-only).
number currentTime: The current playback position (in seconds). Returns only if there is a valid src, with time accurate to 6 decimal places (read-only).
boolean paused: Whether the audio is currently paused or stopped (read-only).
number buffered: The time point up to which the audio has been buffered. Only guarantees that the content from the current playback time to this time point has been buffered (read-only).

Supported formats

Format
iOS
Android
flac
x
m4a
ogg
x
ape
x
amr
x
wma
x
wav
mp3
mp4
x
aac
aiff
x
caf
x

Example

const innerAudioContext = wx.createInnerAudioContext()
innerAudioContext.autoplay = true
innerAudioContext.src = 'https://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&amp;uin=346897220&amp;vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&amp;fromtag=46'
innerAudioContext.onPlay(() => {
console.log('Starts to play')
})
innerAudioContext.onError((res) => {
console.log(res.errMsg)
console.log(res.errCode)
})

Methods collection



Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 available.

7x24 Phone Support
Hong Kong, China
+852 800 906 020 (Toll Free)
United States
+1 844 606 0804 (Toll Free)
United Kingdom
+44 808 196 4551 (Toll Free)
Canada
+1 888 605 7930 (Toll Free)
Australia
+61 1300 986 386 (Toll Free)
EdgeOne hotline
+852 300 80699
More local hotlines coming soon