TXAudioEffectManager
Copyright (c) 2026 Tencent. All rights reserved.
Module: TXAudioEffectManager @ TXLiteAVSDK
Function: APIs for background music, short sound effects and voice effects
Version: 13.5
Background music, in-ear monitoring and voice effect management APIs. Flutter SDK:
tencent_rtc_sdk. Obtain the instance via TRTCCloud's getAudioEffectManager() and then call the APIs.Function List
Voice APIs
API | Description |
Enables in-ear monitoring. | |
Sets the in-ear monitoring volume. | |
Sets the reverb effect of the voice. | |
Sets the voice changing effect. | |
Sets the voice volume. | |
Sets the voice pitch. |
Background Music Playback
API | Description |
Sets the background music event callback. | |
Starts playing background music. | |
Stops playing background music. | |
Pauses playing background music. | |
Resumes playing background music. |
Background Music Volume, Pitch and Speed
API | Description |
Sets the local and remote volume of all background music. | |
Sets the remote volume of a specific background music. | |
Sets the local volume of a specific background music. | |
Adjusts the pitch of background music. | |
Adjusts the playback speed of background music. | |
Adjusts the speed of scratching. |
Background Music Progress and Audio Tracks
API | Description |
Gets the playback progress of background music (in milliseconds). | |
Gets the total duration of background music (in milliseconds). | |
Sets the playback progress of background music (in milliseconds). | |
Gets the number of audio tracks of background music. | |
Specifies the audio track for background music playback. |
Background Music Preloading
API | Description |
Sets the preloading event callback. | |
Preloads background music. |
API Details
The following describes the signature, function, parameters, return value and notes of each API, in the order of the function list.
enableVoiceEarMonitor
void enableVoiceEarMonitor(bool enable)
Enables in-ear monitoring.
After in-ear monitoring is enabled, the anchor can hear their own voice captured by the microphone in the earphones. This feature is suitable for scenarios where the anchor sings.
Due to the high latency of Bluetooth earphones, in-ear monitoring cannot be enabled for Bluetooth earphones. Remind the anchor via the UI to use wired earphones.
Since not all phones can achieve good in-ear monitoring effects, this feature has been disabled on some phones.
Parameter | Type | Description |
enable | bool | true: Enable.false: Disable. |
Description:
In-ear monitoring can be enabled only when the anchor is wearing earphones. Remind the anchor via the UI to use wired earphones.
setVoiceEarMonitorVolume
void setVoiceEarMonitorVolume(int volume)
Sets the in-ear monitoring volume.
This API is used to set the volume of in-ear monitoring.
Parameter | Type | Description |
volume | int | Volume. Value range: 0 - 100. Default value: 100. |
Description:
If the volume is still too low after setting
volume to 100, you can set volume to up to 150, but this may have side effects.setVoiceReverbType
void setVoiceReverbType(TXVoiceReverbType type)
Sets the reverb effect of the voice.
This API is used to set the reverb effect of the voice. For specific effects, see TXVoiceReverbType.
Parameter | Type | Description |
type |
Description:
The effect becomes invalid automatically after room exit. If the effect is needed the next time you enter a room, call this API again to set it.
setVoiceChangerType
void setVoiceChangerType(TXVoiceChangerType type)
Sets the voice changing effect.
Parameter | Type | Description |
type |
Description:
The effect becomes invalid automatically after room exit. If the effect is needed the next time you enter a room, call this API again to set it.
setVoiceCaptureVolume
void setVoiceCaptureVolume(int volume)
Sets the voice volume.
This API sets the voice volume and is generally used together with the music volume API setAllMusicVolume to balance the volume of voice and music.
Parameter | Type | Description |
volume | int | Volume. Value range: 0 - 100. Default value: 100. |
Description:
If the volume is still too low after setting
volume to 100, you can set volume to up to 150, but this may have side effects.setVoicePitch
void setVoicePitch(double pitch)
Sets the voice pitch.
This API sets the voice pitch.
Parameter | Type | Description |
pitch | double | Pitch. Value range: -1.0 - 1.0. Default value: 0.0. |
setMusicObserver
void setMusicObserver(int musicId, TXMusicPlayObserver? observer)
Sets the background music event callback.
Use this API to set the playback event callback before playing background music, so as to track the playback progress of the background music.
Parameter | Type | Description |
musicId | int | Music ID. |
observer | TXMusicPlayObserver? |
Description:
If the ID is no longer needed, you can set
observer to null to completely release the callback.startPlayMusic
void startPlayMusic(AudioMusicParam musicParam)
Starts playing background music.
Each piece of music requires a specific ID, which you can use to start, stop, and set the volume of the music.
Parameter | Type | Description |
musicParam |
Description:
1. To play the same background music multiple times, do not assign a new ID for each playback; using the same ID is recommended.
2. To play multiple different pieces of music at the same time, assign different IDs to them.
3. If the same ID is used to play different music, the SDK stops the old music before playing the new one.
Sample Code
const int musicId = 1;audioEffectManager.setMusicObserver(musicId, TXMusicPlayObserver(onStart: (int id, int errorCode) {debugPrint('music $id start, errorCode: $errorCode');},onPlayProgress: (int id, int curPtsMS, int durationMS) {debugPrint('music $id progress: $curPtsMS / $durationMS');},onComplete: (int id, int errorCode) {debugPrint('music $id complete, errorCode: $errorCode');},));audioEffectManager.startPlayMusic(AudioMusicParam(id: musicId,path: '/path/to/music.mp3',publish: true,));
stopPlayMusic
void stopPlayMusic(int id)
Stops playing background music.
Parameter | Type | Description |
id | int | Music ID. |
pausePlayMusic
void pausePlayMusic(int id)
Pauses playing background music.
Parameter | Type | Description |
id | int | Music ID. |
resumePlayMusic
void resumePlayMusic(int id)
Resumes playing background music.
Parameter | Type | Description |
id | int | Music ID. |
setAllMusicVolume
void setAllMusicVolume(int volume)
Sets the local and remote volume of all background music.
This API sets the local and remote volume of all background music.
Local volume: The volume of background music heard locally by the anchor.
Remote volume: The volume of background music heard by the audience.
Parameter | Type | Description |
volume | int | Volume. Value range: 0 - 100. Default value: 60. |
Description:
If the volume is still too low after setting
volume to 100, you can set volume to up to 150, but this may have side effects.setMusicPublishVolume
void setMusicPublishVolume(int id, int volume)
Sets the remote volume of a specific background music.
This API provides fine-grained control over the remote volume of each background music, i.e., the volume of background music heard by the audience.
Parameter | Type | Description |
id | int | Music ID. |
volume | int | Volume. Value range: 0 - 100. Default value: 60. |
Description:
If the volume is still too low after setting
volume to 100, you can set volume to up to 150, but this may have side effects.setMusicPlayoutVolume
void setMusicPlayoutVolume(int id, int volume)
Sets the local volume of a specific background music.
This API provides fine-grained control over the local volume of each background music, i.e., the volume of background music heard locally by the anchor.
Parameter | Type | Description |
id | int | Music ID. |
volume | int | Volume. Value range: 0 - 100. Default value: 60. |
Description:
If the volume is still too low after setting
volume to 100, you can set volume to up to 150, but this may have side effects.setMusicPitch
void setMusicPitch(int id, double pitch)
Adjusts the pitch of background music.
Parameter | Type | Description |
id | int | Music ID. |
pitch | double | Pitch. Value range: a floating-point number in [-1, 1]. Default value: 0.0. |
setMusicSpeedRate
void setMusicSpeedRate(int id, double rate)
Adjusts the playback speed of background music.
Parameter | Type | Description |
id | int | Music ID. |
rate | double | Speed. Value range: a floating-point number in [0.5, 2]. Default value: 1.0. |
setMusicScratchSpeedRate
void setMusicScratchSpeedRate(int id, double scratchSpeedRate)
Adjusts the speed of scratching.
Parameter | Type | Description |
id | int | Music ID. |
scratchSpeedRate | double | Scratching speed. Value range: a floating-point number in [-12.0, 12.0]. Default value: 1.0. Positive and negative values indicate direction (forward/reverse), and the absolute value indicates the speed. |
Description:
getMusicCurrentPosInMS
int getMusicCurrentPosInMS(int id)
Gets the playback progress of background music (in milliseconds).
Parameter | Type | Description |
id | int | Music ID. |
Return Value
Returns the current playback time in milliseconds on success, or
-1 on failure.getMusicDurationInMS
int getMusicDurationInMS(String path)
Gets the total duration of background music (in milliseconds).
Parameter | Type | Description |
path | String | Music file path. |
Return Value
Returns the duration of the specified music file in milliseconds on success, or
-1 on failure.seekMusicToPosInTime
void seekMusicToPosInTime(int id, int pts)
Sets the playback progress of background music (in milliseconds).
Parameter | Type | Description |
id | int | Music ID. |
pts | int | Playback progress, in milliseconds. |
Description:
Avoid calling this API too frequently, because it may read and write the music file again, which takes a relatively long time.
Therefore, when the user drags the music progress bar, call this API after the user finishes dragging.
Because progress bar controls on the UI often report the user's dragging progress at a very high frequency, not limiting the call frequency will result in a poor user experience.
getMusicTrackCount
int getMusicTrackCount(int id)
Gets the number of audio tracks of background music.
Parameter | Type | Description |
id | int | Music ID. |
Return Value
Number of audio tracks of the specified background music.
setMusicTrack
void setMusicTrack(int id, int trackIndex)
Specifies the audio track for background music playback.
Parameter | Type | Description |
id | int | Music ID. |
trackIndex | int | Specifies which audio track to play (the first track is played by default). Value range: [0, total number of tracks). |
Description:
setPreloadObserver
void setPreloadObserver(TXMusicPreloadObserver? observer)
Sets the preloading event callback.
Use this API to set the callback before preloading background music, so as to track the preloading progress of the background music.
Parameter | Type | Description |
observer | TXMusicPreloadObserver? | Preloading event callback. For details, see TXMusicPreloadObserver. Pass null to remove the callback. |
preloadMusic
void preloadMusic(AudioMusicParam musicParam)
Preloads background music.
Each piece of music requires a specific ID, which you can use to start, stop, and set the volume of the music.
Parameter | Type | Description |
musicParam |
Description:
1. Preloading supports up to 2 different IDs at the same time, and the preloaded duration cannot exceed 10 minutes. After use, you need to call
stopPlayMusic; otherwise, the memory will not be released.2. If the music corresponding to the ID is being played, preloading will fail. You need to call
stopPlayMusic first.3. Preloading takes effect only when the
musicParam passed to startPlayMusic is exactly the same as that used for preloading.Type Definitions
TXVoiceReverbType
Reverb effects.
Reverb effects can be applied to the human voice to simulate sound effects in various environments based on acoustic algorithms.
Enum Value | Value | Description |
type0 | 0 | Disable the effect. |
type1 | 1 | KTV. |
type2 | 2 | Small room. |
type3 | 3 | Great hall. |
type4 | 4 | Deep. |
type5 | 5 | Resonant. |
type6 | 6 | Metallic. |
type7 | 7 | Magnetic. |
type8 | 8 | Ethereal. |
type9 | 9 | Studio. |
type10 | 10 | Melodious. |
type11 | 11 | Studio 2. |
TXVoiceChangerType
Voice changing effects.
Voice changing effects can be applied to the human voice to change its timbre based on acoustic algorithms.
Enum Value | Value | Description |
type0 | 0 | Disable the effect. |
type1 | 1 | Naughty kid. |
type2 | 2 | Little girl. |
type3 | 3 | Middle-aged man. |
type4 | 4 | Heavy metal. |
type5 | 5 | Cold. |
type6 | 6 | Foreigner. |
type7 | 7 | Trapped beast. |
type8 | 8 | Otaku. |
type9 | 9 | Strong current. |
type10 | 10 | Heavy machinery. |
type11 | 11 | Ethereal. |
AudioMusicParam
Playback control information of background music.
This information is used to specify background music information in the startPlayMusic API, including the playback ID, file path and loop count.
1. To play the same background music multiple times, do not assign a new ID for each playback; using the same ID is recommended.
2. To play multiple different pieces of music at the same time, assign different IDs to them.
3. If the same ID is used to play different music, the SDK stops the old music before playing the new one.
Field | Type | Default Value | Description |
id | int | Required | Music ID. The SDK allows playing multiple pieces of music, so an ID is needed to mark each one and control its start, stop, volume, etc. |
path | String | Required | Absolute path or URL of the music file. The mp3, aac, m4a and wav formats are supported. |
loopCount | int | 0 | Number of times the music loops. Value range: 0 or any positive integer. 0 means playing the music once.1 means playing the music twice, and so on. |
publish | bool | false | Whether to send the music to remote users. true: While the music is played locally, remote users can also hear it.false: Only the anchor can hear the music locally; remote audience members cannot hear it. |
isShortFile | bool | false | Whether the file played is a short music file. true: A short music file that needs to be played repeatedly.false: A normal music file. |
startTimeMS | int | 0 | Time point at which the music starts playing, in milliseconds. |
endTimeMS | int | 0 | Time point at which the music stops playing, in milliseconds. 0 means playing to the end of the file. |
TXMusicPlayObserver
Event callbacks of background music playback.
onStart
final void Function(int id, int errorCode) onStart
Background music started playing.
Parameter | Type | Description |
id | int | Music ID. |
errorCode | int | 0: Playback started successfully.-4001: Failed to open the file, for example, invalid data found when processing input, or ffmpeg protocol not found.-4005: Invalid path. Check whether the path passed in points to a valid music file.-4006: Invalid URL. Use a browser to check whether the expected music file can be downloaded from the URL passed in.-4007: No audio stream. Check whether the file passed in is a valid audio file and whether it is corrupted.-4008: Unsupported format. Check whether the format of the file passed in is supported. Mobile supports mp3, aac, m4a, wav, ogg, mp4 and mkv; desktop supports mp3, aac, m4a, wav, mp4 and mkv. |
onPlayProgress
final void Function(int id, int curPtsMSm, int durationMS) onPlayProgress
Playback progress of background music.
Parameter | Type | Description |
id | int | Music ID. |
curPtsMSm | int | Current playback time point, in milliseconds. |
durationMS | int | Total duration of the music, in milliseconds. |
onComplete
final void Function(int id, int errorCode) onComplete
Background music playback finished.
Called back when background music playback finishes or an error occurs during playback.
Parameter | Type | Description |
id | int | Music ID. |
errorCode | int | 0: Playback ended.-4002: Decoding failed, for example, the audio file is corrupted or the online audio file server is inaccessible. |
TXMusicPreloadObserver
Event callbacks of background music preloading.
onLoadProgress
final void Function(int id, int progress) onLoadProgress
Background music preloading progress.
Parameter | Type | Description |
id | int | Music ID. |
progress | int | Preloading progress. |
onLoadError
final void Function(int id, int errorCode) onLoadError
Background music preloading error.
Parameter | Type | Description |
id | int | Music ID. |
errorCode | int | -4001: Failed to open the file, for example, invalid data found when processing input, or ffmpeg protocol not found.-4002: Decoding failed, for example, the audio file is corrupted or the online audio file server is inaccessible.-4003: The number of preloads exceeds the limit. Call stopPlayMusic first to release unused preloads.-4005: Invalid path. Check whether the path passed in points to a valid music file.-4006: Invalid URL. Use a browser to check whether the expected music file can be downloaded from the URL passed in.-4007: No audio stream. Check whether the file passed in is a valid audio file and whether it is corrupted.-4008: Unsupported format. Check whether the format of the file passed in is supported. Mobile supports mp3, aac, m4a, wav, ogg, mp4 and mkv; desktop supports mp3, aac, m4a, wav, mp4 and mkv. |
