stopVoice
This API is used via wx.stopVoice(Object object).
Feature Description: Terminates the playback of audio.
Parameter and Description: Object.
|
success | function | - | No | Callback Function of Successful Interface Call |
fail | function | - | No | Callback Function of Failing Interface Call |
complete | function | - | No | Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations) |
Sample Code
wx.startRecord({
success (res) {
const tempFilePath = res.tempFilePath
wx.playVoice({
filePath: tempFilePath,
})
setTimeout(() => { wx.stopVoice() }, 5000)
}
})
playVoice
This API is used via wx.playVoice(Object object).
Feature Description: Starts the playback of voice. Only one voice file is permitted to play at a time. If a previous voice file is still playing, it will be interrupted.
Parameter and Description:
|
filePath | string | - | Yes | Path of the audio file to be played (local path) |
duration | number | 60 | No | Specifies the playback duration. Upon reaching the specified duration, the playback will automatically cease. Unit: seconds. |
success | function | - | No | Callback Function of Successful Interface Call |
fail | function | - | No | Callback Function of Failing Interface Call |
complete | function | - | No | Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations) |
Sample Code
wx.startRecord({
success (res) {
const tempFilePath = res.tempFilePath
wx.playVoice({
filePath: tempFilePath,
complete () { }
})
}
})
pauseVoice
This API is used via wx.pauseVoice(Object object).
Feature Description: Pauses the currently playing audio. When wx.playVoice is called again to play the same file, it will resume from the point of pause. If playback is to start from the beginning, wx.stopVoice must be called first. Parameter and Description: Object.
|
success | function | - | No | Callback Function of Successful Interface Call |
fail | function | - | No | Callback Function of Failing Interface Call |
complete | function | - | No | Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations) |
Sample Code
wx.startRecord({
success (res) {
const tempFilePath = res.tempFilePath
wx.playVoice({
filePath: tempFilePath
})
setTimeout(() => { wx.pauseVoice() }, 5000)
}
})
setInnerAudioOption
This API is used via wx.setInnerAudioOption(Object object).
Feature Description: Sets the playback options for InnerAudioContext. Once set, it applies globally to the current mini program. Parameter: Object.
|
mixWithOther | boolean | true | No | Whether to mix with other audio. When set to true, it will not terminate the music of other applications. |
obeyMuteSwitch | boolean | true | No | (Effective only on iOS) Whether to adhere to the mute switch. When set to false, sound can be played even in mute mode. |
speakerOn | boolean | true | No | true for playback from speakers, false for playback from earpiece, default value is true |
success | function | - | No | Callback Function of Successful Interface Call |
fail | function | - | No | Callback Function of Failing Interface Call |
complete | function | - | No | Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations) |
getAvailableAudioSources
This API is used via wx.getAvailableAudioSources(Object object).
Feature Description: Gets the currently supported audio input sources.
Parameter and Description: Object.
|
success | function | - | No | Callback Function of Successful Interface Call |
fail | function | - | No | Callback Function of Failing Interface Call |
complete | function | - | No | Callback function executed upon the completion of the interface invocation (both successful and unsuccessful invocations) |
Parameters for object.success callback function:
|
audioSources | Array.<string> | List of supported audio input sources. See AudioSource for the definition of return values. |
Valid values for audioSources
|
auto | Automatically configured to default to the mobile microphone. Upon connecting a headset, it automatically switches to the headset microphone. Applicable across all platforms. |
buildInMic | Mobile microphone, exclusive to iOS. |
headsetMic | Headset microphone, exclusive to iOS. |
mic | Microphone (defaults to mobile microphone when headset is not connected, switches to headset microphone when connected), exclusive to Android. |
camcorder | Similar to the microphone, applicable for recording audio and video content, exclusive to Android. |
voice_communication | Similar to the microphone, applicable for real-time communications, exclusive to Android. |
voice_recognition | Similar to the microphone, applicable for recording audio content, exclusive to Android. |
createAudioContext
This API is used via AudioContext wx.createAudioContext(string id, Object this).
Parameter and Description: string id, ID of the audio component; Object this, "this" of the current component instance under custom components, used to operate the audio component within.
Note: InnerAudioContext audio resources will not be automatically released, so if you no longer need to use the audio, please call InnerAudioContext.destroy() to release the resources in time to avoid memory leakage.
Sample code:
const innerAudioContext = wx.createInnerAudioContext()
innerAudioContext.src = 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46'
innerAudioContext.play()
innerAudioContext.pause()
innerAudioContext.stop()
innerAudioContext.destroy()
AudioContext
An AudioContext instance can be obtained through wx.createAudioContext. It binds with an audio component through an id, allowing operations on the corresponding audio component. pause
This method is used via AudioContext.pause().
Feature Description: Pauses the audio.
play
This method is used via AudioContext.play().
Feature Description: Plays the audio.
seek
This method is used via AudioContext.seek(number position).
Feature Description: Redirects to the specified position.
Parameter and Description: number position, the position to redirect to, unit: seconds.
setSrc
This method is used via AudioContext.setSrc(string src).
Feature Description: Sets the audio address.
Parameter and Description: string src, audio address.
createInnerAudioContext
This API is used via InnerAudioContext wx.createInnerAudioContext(Object object).
Feature Description: Creates an internal audio
context InnerAudioContext object, with the usage method being wx.createInnerAudioContext().
InnerAudioContext
Attributes
string src: The address of the audio resource, used for direct playback. Cloud file ID is supported.
number startTime: The starting position for playback (in seconds), defaulting to 0.
boolean autoplay: Determines whether playback starts automatically, defaulting to "false".
boolean loop: Determines whether the playback circulates, defaulting to "false".
number volume: Volume level, ranging from 0 to 1, defaulting to 1.
number duration: The length of the current audio (in seconds). Only returned when there is a valid src (read-only).
number currentTime: The current playback position of the audio (in seconds). Only returned when there is a valid src, and the time is accurate to 6 decimal places (read-only).
boolean paused: Indicates whether the current status is paused or stopped (read-only).
number buffered: The time point that the audio is buffered to, ensuring content from the current playback point to this point has been buffered (read-only).
Supported format
|
flac | x | √ |
m4a | √ | √ |
ogg | x | √ |
ape | x | √ |
amr | x | √ |
wma | x | √ |
wav | √ | √ |
mp3 | √ | √ |
mp4 | x | √ |
aac | √ | √ |
aiff | √ | x |
caf | √ | x |
Sample code
const innerAudioContext = wx.createInnerAudioContext()
innerAudioContext.autoplay = true
innerAudioContext.src = 'https://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46'
innerAudioContext.onPlay(() => {
console.log('Start playback')
})
innerAudioContext.onError((res) => {
console.log(res.errMsg)
console.log(res.errCode)
})
Method Set
play
This method is used via InnerAudioContext.play().
Feature Description: Play.
pause
This method is used via InnerAudioContext.pause().
Feature Description: Pause. Upon resumption, the audio will start playback from the point of pause.
stop
This method is used via InnerAudioContext.stop().
Feature Description: Stop. Upon resumption, the audio will start playback from the beginning.
seek
This method is used via InnerAudioContext.seek(number position).
Feature Description: Redirects to the specified position.
destroy
This method is used via InnerAudioContext.destroy().
Feature Description: Terminates the current instance.
onCanplay
This method is used via InnerAudioContext.onCanplay(function listener).
Feature Description: Monitors events where the audio enters a playable status. However, it does not guarantee smooth playback thereafter.
Parameter and Description: function listener, the listener function for events where the audio enters a playable status.
offCanplay
This method is used via InnerAudioContext.offCanplay(function listener).
Feature Description: Removes the listener function for events where the audio enters a playable status.
Parameter and Description: function listener, the listener function passed in by onCanplay. If this parameter is not passed in, all listener functions will be removed.
onPlay
This method is used via InnerAudioContext.onPlay(function listener).
Feature Description: Monitors audio playback events.
Parameters and Description: function listener, the listener function for audio playback events.
offPlay
This method is used via InnerAudioContext.offPlay(function listener).
Feature Description: Removes the listener function for audio playback events.
Parameter and Description: function listener, the listener function passed in by onPlay. If this parameter is not passed in, all listener functions will be removed.
onPuase
This method is used via InnerAudioContext.onPause(function listener).
Feature Description: Monitors audio pause events.
Parameter and Description: function listener, the listener function for audio pause events.
offPause
This method is used via InnerAudioContext.offPause(function listener).
Feature Description: Removes the listener function for audio pause events.
Parameter and Description: function listener, the listener function passed in by onPause. If this parameter is not passed in, all listener functions will be removed.
onStop
This method is used via InnerAudioContext.onStop(function listener).
Feature Description: Monitors audio stop events.
Parameter and Description: function listener, the listener function for audio stop events.
offStop
This method is used via InnerAudioContext.offStop(function listener).
Feature Description: Removes the listener function for audio stop events.
Parameter and Description: function listener, the listener function passed in by onStop. If this parameter is not passed in, all listener functions will be removed.
onEnded
This method is used via InnerAudioContext.onEnded(function listener).
Feature Description: Monitors events of audio naturally playing to its end.
Parameters and Description: function listener, the listener function for events of audio naturally playing to its end.
offEnded
This method is used via InnerAudioContext.offEnded(function listener).
Feature Description: Removes the listener function for events of audio naturally playing to its end.
Parameter and Description: function listener, the listener function passed in by onEnded. If this parameter is not passed in, all listener functions will be removed.
onTimeUpdate
This method is used via InnerAudioContext.onTimeUpdate(function listener).
Feature Description: Monitors events of audio playback progress updates.
Parameter and Description: function listener, the listener function for events of audio playback progress updates.
offTimeUpdate
This method is used via InnerAudioContext.offTimeUpdate(function listener).
Feature Description: Removes the listener function for events of audio playback progress updates.
Parameter and Description: function listener, the listener function passed in by onTimeUpdate. If this parameter is not passed in, all listener functions will be removed.
onError
This method is used via InnerAudioContext.onError(function listener).
Feature Description: Monitors audio playback error events.
Parameter and Description: function listener, the listener function for audio playback error events.
Object res
Valid values for errCode
|
10001 | System error |
10002 | Network Error |
10003 | File Error |
10004 | Incorrect format. |
-1 | Unknown error. |
offError
This method is used via InnerAudioContext.offError(function listener).
Feature Description: Removes the listener function for audio playback error events.
Parameter and Description: function listener, the listener function passed in by onError. If this parameter is not passed in, all listener functions will be removed.
onWaitting
This method is used via InnerAudioContext.onWaiting(function listener).
Feature Description: Monitors audio loading events. It is triggered when the audio needs to stop and load due to insufficient data.
Parameters and Description: function listener, the listener function passed in by onWaiting. If this parameter is not passed in, all listener functions will be removed.
offWaitting
This method is used via InnerAudioContext.offWaiting(function listener).
Feature Description: Removes the listener function for audio loading events.
Parameter and Description: function listener, the listener function for audio loading events.
onSeeking
This method is used via InnerAudioContext.onSeeking(function listener).
Feature Description: Monitors audio skipping operation events.
Parameters and Description: function listener, the listener function for audio skipping operation events.
offSeeking
This method is used via InnerAudioContext.offSeeking(function listener).
Feature Description: Removes the listener function for audio skipping operation events.
Parameter and Description: function listener, the listener function passed in by onSeeking. If this parameter is not passed in, all listener functions will be removed.
onSeeked
This method is used via InnerAudioContext.onSeeked(function listener).
Feature Description: Monitors events of completion of audio skipping operations.
Parameter and Description: function listener, the listener function for events of completion of audio skipping operations.
offSeeked
This method is used via InnerAudioContext.offSeeked(function listener).
Feature Description: Removes the listener function for events of completion of audio skipping operations.
Parameter and Description: function listener, the listener function passed in by onSeeked. If this parameter is not passed in, all listener functions will be removed.
Was this page helpful?