Skip to content

Audio

stopVoice

TIP

The API usage is as follows: wx.stopVoice(Object object)

  • Functional description: Stop playing speech

  • Parameters and descriptions: Object object

    PropertiesTypeDefault valueRequiredDescription
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • Sample code:

js
wx.startRecord({
  success (res) {
    const tempFilePath = res.tempFilePath
    wx.playVoice({
      filePath: tempFilePath,
    })

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

playVoice

TIP

The API usage is as follows: wx.playVoice(Object object)
This API is supported by mini programs, but not by mini games.

  • Functional description: Start playing voice. Only one voice file is allowed to be played at the same time. If the previous voice file has not been played, the previous voice will be interrupted.

  • Parameters and descriptions:

    PropertiesTypeDefault valueRequiredDescription
    filePathstring-YesFile path (local path) of the voice file to be played
    durationnumber60NoSpecify the playback time. After reaching the specified playback time, it will automatically stop playing. Unit: seconds
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • Sample code:

js
wx.startRecord({
  success (res) {
    const tempFilePath = res.tempFilePath
    wx.playVoice({
      filePath: tempFilePath,
      complete () { }
    })
  }
})

pauseVoice

TIP

The API usage is as follows: wx.pauseVoice(Object object)
This API is supported by mini programs, but not by mini games.

  • Functional description: Pause the currently playing voice. Call again wx.playVoice When playing the same file, it will start playing from the pause point. If you want to play from the beginning, you need to call wx.stopVoice

  • Parameters and descriptions: Object object。

    PropertiesTypeDefault valueRequiredDescription
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • Sample code:

js
wx.startRecord({
  success (res) {
    const tempFilePath = res.tempFilePath
    wx.playVoice({
      filePath: tempFilePath
    })

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

setInnerAudioOption

TIP

The API usage is as follows: wx.setInnerAudioOption(Object object)

  • Functional description: Settings InnerAudioContext the playback option first. After setting, it will take effect globally for the current applet.
  • Parameters and descriptions: Object object。
    PropertiesTypeDefault valueRequiredDescription
    mixWithOtherbooleantrueNoWhether to mix with other audio. After setting to true, the music of other applications will not be terminated
    obeyMuteSwitchbooleantrueNo(Only effective on iOS) Whether to follow the mute switch. After setting to false, the sound can be played even in silent mode
    speakerOnbooleantrueNotrue means playing with the speaker, false means playing with the earpiece, the default value is true
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)

getAvailableAudioSources

TIP

The API usage is as follows: wx.getAvailableAudioSources(Object object)
This API is supported by mini programs, but not by mini games.

  • Functional description: Get the currently supported audio input sources

  • Parameters and descriptions: Object object。

    PropertiesTypeDefault valueRequiredDescription
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • object.success callback function parameters: Object res。

    PropertiesTypeDescription
    audioSourcesArray.<string>List of supported audio input sources. Return value definition reference AudioSource
    • audioSources legal value
    Legal valueDescription
    autoAutomatically set, use the mobile phone microphone by default, and automatically switch to the headphone microphone after plugging in the headset, applicable to all platforms
    buildInMicMobile phone microphone, iOS only
    headsetMicWired headphone microphone, iOS only
    micMicrophone (mobile phone microphone when no headset is plugged in, headphone microphone when headset is plugged in), Android only
    camcorderSame as mic, suitable for recording audio and video content, Android only
    voice_communicationSame as mic, suitable for real-time communication, Android only
    voice_recognitionSame as mic, for speech recognition, only for Android

createAudioContext

TIP

The API usage is as follows: AudioContext wx.createAudioContext(string id, Object this)
This API is supported by mini programs, but not by mini games.

  • Functional description: Create audio context AudioContext Object
  • Parameters and descriptions: string id, audio component id; Object this, under custom components, this of the current component instance, to operate the audio component in the component.
  • Return value: AudioContext

AudioContext

TIP

This API is supported by mini programs, but not by mini games.

AudioContext instance, which can be obtained through wx.createAudioContext , bind to an audio component through id, and operate the corresponding audio component.

pause

TIP

The API usage is as follows: AudioContext.pause()

  • Functional description: Pause audio
play

TIP

The API usage is as follows: AudioContext.play()

  • Functional description: Play audio
seek

TIP

The API usage is as follows: AudioContext.seek(number position)

  • Functional description: Jump to the specified position
  • Parameters and descriptions: number position, Jump position, unit: s.
setSrc

TIP

The API usage is as follows: AudioContext.setSrc(string src)

  • Functional description: Set the audio address
  • Parameters and descriptions: string src, Audio address.

createInnerAudioContext

TIP

The API usage is as follows: InnerAudioContext wx.createInnerAudioContext(Object object)

  • Functional description: Create internal audio context InnerAudioContext object, using wx.createInnerAudioContext().

  • Return value: InnerAudioContext

  • Things To Note InnerAudioContext audio resources will not be released automatically, so if you no longer need to use audio, please call InnerAudioContext.destroy() in time to release resources to avoid memory leaks.

  • Sample code:

js
const innerAudioContext = wx.createInnerAudioContext()
innerAudioContext.src = 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46'

innerAudioContext.play() // play

innerAudioContext.pause() // pause

innerAudioContext.stop() // stop

innerAudioContext.destroy() // destroy

InnerAudioContext

Properties

  • string src:The address of audio resources for direct playback.
  • number startTime:Start playback position (unit: s), default is 0.
  • boolean autoplay:Whether to start playback automatically, default is false.
  • boolean loop:Whether to loop playback, default is false.
  • number volume:Volume, range 0-1. Default is 1.
  • number duration:The length of the current audio (unit: s). Only returns when there is a valid src (read-only).
  • number currentTime:The playback position of the current audio (unit: s). Only returns when there is a valid src, and the time is retained to 6 decimal places (read-only).
  • boolean paused:Whether it is currently paused or stopped (read-only).
  • number buffered:The time point of the audio buffering, only ensure that the content from the current playback time point to this time point has been buffered (read-only).

Supported formats

Supported formatsiOSAndroid
flacx
m4a
oggx
apex
amrx
wmax
wav
mp3
mp4x
aac
aiffx
cafx
  • Sample code:
js
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 play')
})
innerAudioContext.onError((res) => {
  console.log(res.errMsg)
  console.log(res.errCode)
})

Method set

play

TIP

The API usage is as follows: InnerAudioContext.play()

  • Functional description: Start playing
pause

TIP

The API usage is as follows: InnerAudioContext.pause()

  • Functional description: Pause, the audio after the pause will start playing from the pause point.
stop

TIP

The API usage is as follows: InnerAudioContext.stop()

  • Functional description: Stop, the audio after the stop will start playing from the beginning.
seek

TIP

The API usage is as follows: InnerAudioContext.seek(number position)

  • Functional description: Jump to the specified position
destroy

TIP

The API usage is as follows: InnerAudioContext.destroy()

  • Functional description: Destroy the current instance.
onCanplay

TIP

The API usage is as follows: InnerAudioContext.onCanplay(function listener)

  • Functional description: Listen for the event that the audio enters the playable state. But it does not guarantee smooth playback afterwards.
  • Parameters and descriptions: function listener, Listener function for the event that the audio enters the playable state.
offCanplay

TIP

The API usage is as follows: InnerAudioContext.offCanplay(function listener)

  • Functional description: Remove the listener function for the event that the audio enters the playable state
  • Parameters and descriptions: function listener, onCanplay The listener function passed in. If this parameter is not passed, all listeners will be removed.
  • Sample code:
js
const listener = function (res) { console.log(res) }

InnerAudioContext.onCanplay(listener)
InnerAudioContext.offCanplay(listener) // Pass the same function object used for listening.
onPlay

TIP

The API usage is as follows: InnerAudioContext.onPlay(function listener)

  • Functional description: Listen for audio playback events
  • Parameters and descriptions: function listener, Listener function for the audio playback event.
offPlay

TIP

The API usage is as follows: InnerAudioContext.offPlay(function listener)

  • Functional description: Remove the listener function for the audio playback event

  • Parameters and descriptions: function listener, onPlay The listener function passed in. If this parameter is not passed, all listeners will be removed.

  • Sample code:

js
const listener = function (res) { console.log(res) }

InnerAudioContext.onPlay(listener)
InnerAudioContext.offPlay(listener) // Pass the same function object used for listening.
onPause

TIP

The API usage is as follows: InnerAudioContext.onPause(function listener)

  • Functional description: Listen for the audio pause event
  • Parameters and descriptions: function listener, Listener function for audio pause event.
offPause

TIP

The API usage is as follows: InnerAudioContext.offPause(function listener)

  • Functional description: Remove the listener function for the audio pause event

  • Parameters and descriptions: function listener, Listener function passed in by onPause. If this parameter is not passed, all listeners are removed.

  • Sample code:

js
const listener = function (res) { console.log(res) }

InnerAudioContext.onPause(listener)
InnerAudioContext.offPause(listener) // Pass the same function object used for listening.
onStop

TIP

The API usage is as follows: InnerAudioContext.onStop(function listener)

  • Functional description: Listen for the audio stop event
  • Parameters and descriptions: function listener, Listener function for audio stop event.
offStop

TIP

The API usage is as follows: InnerAudioContext.offStop(function listener)

  • Functional description: Remove the listener function for the audio stop event

  • Parameters and descriptions: function listener, Listener function passed in by onStop. If this parameter is not passed, all listeners are removed.

  • Sample code:

js
const listener = function (res) { console.log(res) }

InnerAudioContext.onStop(listener)
InnerAudioContext.offStop(listener) // Pass the same function object used for listening.
onEnded

TIP

The API usage is as follows: InnerAudioContext.onEnded(function listener)

  • Functional description: Listen for the event that the audio is naturally played to the end
  • Parameters and descriptions: function listener, Listen for the event that the audio is naturally played to the end
offEnded

TIP

The API usage is as follows: InnerAudioContext.offEnded(function listener)

  • Functional description: Remove the listener function for the event that the audio is naturally played to the end

  • Parameters and descriptions: function listener, Listener function passed in by onEnded. If this parameter is not passed, all listeners are removed.

  • Sample code:

js
const listener = function (res) { console.log(res) }

InnerAudioContext.onEnded(listener)
InnerAudioContext.offEnded(listener) // Pass the same function object used for listening.
onTimeUpdate

TIP

The API usage is as follows: InnerAudioContext.onTimeUpdate(function listener)

  • Functional description: Listen for the audio playback progress update event
  • Parameters and descriptions: function listener, Listen for the event that the background audio playback progress is updated.
offTimeUpdate

TIP

The API usage is as follows: InnerAudioContext.offTimeUpdate(function listener)

  • Functional description: Remove the listener function for the audio playback progress update event

  • Parameters and descriptions: function listener, Listener function passed in by onTimeUpdate. If this parameter is not passed, all listeners are removed.

  • Sample code:

js
const listener = function (res) { console.log(res) }

InnerAudioContext.onTimeUpdate(listener)
InnerAudioContext.offTimeUpdate(listener) // Pass the same function object used for listening.
onError

TIP

The API usage is as follows: InnerAudioContext.onError(function listener)

  • Functional description: Listen for the audio playback error event
  • Parameters and descriptions: function listener, Listener function for audio playback error event.
  • Object res
    PropertiesTypeDescription
    errMsgstring-
    errCodenumber-
  • Legal value of errCode
    ValueDescription
    10001System error
    10002Network error
    10003File error, please check whether the response header lacks Content-Length
    10004Format error
    -1Unknown error
  • tips:
    • When errCode=100001, if there is INNERCODE -11828 in errMsg, please check whether the response header lacks Content-Length first.
    • When errCode=100001, if there is systemErrCode:200333420 in errMsg, please check whether the file encoding format and fileExtension are consistent.
offError

TIP

The API usage is as follows: InnerAudioContext.offError(function listener)

  • Functional description: Remove the listener function for the audio playback error event

  • Parameters and descriptions: function listener, onError The passed listener function. Without this parameter, all listeners are removed.

  • Sample code:

js
const listener = function (res) { console.log(res) }

InnerAudioContext.onError(listener)
InnerAudioContext.offError(listener)  // Pass the same function object used for listening.
onWaiting

TIP

The API usage is as follows: InnerAudioContext.onWaiting(function listener)

  • Functional description: Listen for the event that the audio is loading. It will be triggered when the audio needs to stop loading due to insufficient data.
  • Parameters and descriptions: function listener, Listen for the event that the audio is loading.
offWaiting

TIP

The API usage is as follows: InnerAudioContext.offWaiting(function listener)

  • Functional description: Remove the listener function for the audio loading event

  • Parameters and descriptions: function listener, onWaiting The listening function passed in. If this parameter is not passed, all listening functions will be removed.

  • Sample code:

js
const listener = function (res) { console.log(res) }

InnerAudioContext.onWaiting(listener)
InnerAudioContext.offWaiting(listener)  // Pass the same function object used for listening.
onSeeking

TIP

The API usage is as follows: InnerAudioContext.onSeeking(function listener)

  • Functional description: Listen for the event that the audio performs the jump operation
  • Parameters and descriptions: function listener, The listening function for the event of the audio jumping operation.
offWaiting

TIP

The API usage is as follows: InnerAudioContext.offSeeking(function listener)

  • Functional description: Remove the listener function for the audio jump operation event

  • Parameters and descriptions: function listener, onSeeking The listening function passed in. If this parameter is not passed, all listening functions will be removed.

  • Sample code:

js
const listener = function (res) { console.log(res) }

InnerAudioContext.onSeeking(listener)
InnerAudioContext.offSeeking(listener)  // Pass the same function object used for listening.
onSeeked

TIP

The API usage is as follows: InnerAudioContext.onSeeked(function listener)

  • Functional description: Listen for the event that the audio completes the jump operation
  • Parameters and descriptions: function listener, The listening function for the event of the audio completing the jumping operation.
offSeeked

TIP

The API usage is as follows: InnerAudioContext.offSeeked(function listener)

  • Functional description: Remove the listener function for the audio jump operation event

  • Parameters and descriptions: function listener, onSeeked The listener function passed in. If this parameter is not passed, all listeners are removed.

  • Sample code:

js
const listener = function (res) { console.log(res) }

InnerAudioContext.onSeeked(listener)
InnerAudioContext.offSeeked(listener)  // Pass the same function object used for listening.