Updated on 2023-11-01 GMT+08:00

Playing an Audio File

Function

Audio mixing is the process of combining a music file with the audio captured by a microphone and is generally for background music or accompaniment that lasts for a long time. Only one audio file can be played at a time for other users in the room.

Local or online music files in WAV, PCM, or mono MP3 format can be played.

API Calling

Implementation

  1. Join a room and publish the local video stream.

    Join a room by referring to the sequence diagram in API Calling and publish the local video stream.

  2. Play an audio file.

    Call startAudioMixing to play an audio file. The following is an example of parameter settings. Only one audio file can be played at a time.

    // localStream indicates the local video stream.
    localStream.startAudioMixing({
    // filePath indicates the path for downloading an online audio file.
    "filePath":"https://***.***.***.***:50007/music.mp3",
    // startTime indicates the time when the audio file starts to be played. The default value is 0.
    "startTime":0,
    // replace indicates whether to replace the local audio stream with an audio file.
    "replace":false,
    // loop indicates whether infinite loop playback is required.
    "loop":false,
    // repeatCount indicates the number of times of repeating the audio file.
    "repeatCount":0
    })
  3. Set the volume of the audio file.

    During playback, call setAudioMixingVolume to set the volume of the audio file.

    // volume indicates the volume.
     let volume = 50
     localStream.setAudioMixingVolume (volume)
  4. Obtain the total duration of the audio file.

    After an audio file is played, call getAudioMixingDuration to obtain its total duration, which can be used to refresh the progress bar on the UI.

    localStream.getAudioMixingDuration()
  5. Obtain the progress of audio file playback.

    After an audio file is played, call getAudioMixingCurrentPosition to obtain its current playback position.

    localStream.getAudioMixingCurrentPosition()
  6. Set the playback position of the audio file.

    During playback, call setAudioMixingPosition to set the playback position of the audio file and drag the progress bar to seek to the desired position.

    localStream.getAudioMixingDuration()
  7. Pause the playback.

    After the audio file is played, call pauseAudioMixing to pause the playback.

    localStream.pauseAudioMixing()
  8. Resume the playback.

    After the audio file is paused, call resumeAudioMixing to resume the playback.

    localStream.resumeAudioMixing()
  9. Stop the playback.

    After the audio file is played, call stopAudioMixing to stop the playback.

    localStream.stopAudioMixing()
  10. Trigger a callback when the audio file playback starts.

    When the audio file playback starts, the audio-mixing-played callback is triggered to notify the application.

    localStream.on('audio-mixing-played', () => {
     console.info('audioMixing: audio-mixing-played')
    })
  11. Trigger a callback when the audio file playback is complete.

    When the audio file playback is complete, the audio-mixing-finished callback is triggered to notify the application.

    localStream.on('audio-mixing-finished', () => {
     console.info('audioMixing: audio-mixing-finished')
    })