tencent cloud

Feedback

Background Music

Last updated: 2025-01-09 17:35:27
    This document mainly introduces how to use the RTC Room Engine SDK to implement background music features.

    Prerequisites

    Before using the background music setting features provided by the RTC Room Engine SDK, you need to complete the SDK login.

    User Guide

    Starting/Stopping Background Music

    iOS
    Android
    You can start or stop background music playback by calling the startPlayMusic and stopPlayMusic APIs respectively.
    When calling startPlayMusic to start background music playback, you need to pass a TXAudioMusicParam parameter to set playback control information. TXAudioMusicParam includes the following information, which you can set individually:
    Enumeration Types
    Description
    id
    Field Meaning: Music ID.
    Special Instructions: The SDK allows playing multiple music tracks simultaneously, so IDs are needed to control start, stop, volume, etc.
    endTimeMS
    Field Meaning: Music play end time, in milliseconds. 0 means play until the end of the file.
    isShortFile
    Field Meaning: Whether the played file is a short music file.
    Recommended Value: YES: short music file that needs to be played repeatedly; NO: normal music file. Default: NO.
    loopCount
    Field Meaning: The number of times the music loops.
    Recommended Value: Value range is 0 to any positive integer. Default: 0. 0 means play the music once; 1 means play the music twice; and so on.
    path
    Field Meaning: The full path or URL of the audio file. Supported audio formats include MP3, AAC, M4A, WAV.
    publish
    Field Meaning: Whether to transmit the music to the remote end.
    Recommended Value: YES: The music is played locally and is also heard by remote users; NO: The host hears the music locally, but remote audience cannot. Default: NO.
    startTimeMS
    Field Meaning: Music play start time, in milliseconds.
    You can start or stop background music playback by calling the startPlayMusic and stopPlayMusic APIs respectively.
    When calling startPlayMusic to start background music playback, you need to pass an AudioMusicParam parameter to set playback control information. AudioMusicParam includes the following information, which you can set individually:
    Enumeration Types
    Description
    id
    Field Meaning: Music ID.
    Special Instructions: The SDK allows playing multiple music tracks simultaneously, so IDs are needed to control start, stop, volume, etc.
    endTimeMS
    Field Meaning: Music play end time, in milliseconds. 0 means play until the end of the file.
    isShortFile
    Field Meaning: Whether the played file is a short music file.
    Recommended Value: YES: short music file that needs to be played repeatedly; NO: normal music file. Default: NO.
    loopCount
    Field Meaning: The number of times the music loops.
    Recommended Value: Value range is 0 to any positive integer. Default: 0. 0 means play the music once; 1 means play the music twice; and so on.
    path
    Field Meaning: The full path or URL of the audio file. Supported audio formats include MP3, AAC, M4A, WAV.
    publish
    Field Meaning: Whether to transmit the music to the remote end.
    Recommended Value: YES: The music is played locally and is also heard by remote users; NO: The host hears the music locally, but remote audience cannot. Default: NO.
    startTimeMS
    Field Meaning: Music play start time, in milliseconds.
    Below is a simple example of starting/stopping background music playback:
    iOS
    Android
    import RTCRoomEngine
    import TXLiteAVSDK_Professional
    
    let audioEffectManager = TUIRoomEngine.sharedInstance().getTRTCCloud().getAudioEffectManager()
    
    // Start background music playback.
    let musicParam = TXAudioMusicParam()
    musicParam.id = 0 // Replace with your own music ID
    musicParam.path = "path" // Replace with the full path or URL of the music file
    musicParam.publish = true // Publish the music to the remote
    musicParam.loopCount = 0 // Replace with the number of times you need to loop playback
    audioEffectManager.startPlayMusic(musicParam) { code in
    if code == 0 {
    // Start playback successful
    } else {
    // Start playback failed
    }
    } onProgress: { progress, duration in
    // Playback progress callback.
    } onComplete: { _ in
    // Playback end callback.
    }
    
    // Stop background music playback.
    audioEffectManager.stopPlayMusic(musicId) // Replace with the music ID you need to stop playing
    
    TXAudioEffectManager audioEffectManager = TUIRoomEngine.sharedInstance().getTRTCCloud().getAudioEffectManager();
    
    int id = 0; // Replace with your own music ID
    String path = "path"; // Replace with the full path or URL of the music file
    // Start background music playback.
    TXAudioEffectManager.AudioMusicParam musicParam = new TXAudioEffectManager.AudioMusicParam(id, path);
    musicParam.publish = true; // Publish the music to the remote
    musicParam.loopCount = 0; // Replace with the number of times you need to loop playback
    audioEffectManager.startPlayMusic(musicParam);
    
    // Stop background music playback.
    audioEffectManager.stopPlayMusic(id); // Replace with the music ID you need to stop playing

    Setting the Background Music Volume

    You can set the background music volume by calling the setAllMusicVolume API and passing an Int value.
    The passed Int value represents the volume level, ranging from 0 to 100. Here is an example of calling setAllMusicVolume to set the background music volume:
    iOS
    Android
    import RTCRoomEngine
    import TXLiteAVSDK_Professional
    
    let audioEffectManager = TUIRoomEngine.sharedInstance().getTRTCCloud().getAudioEffectManager()
    
    let volume = 60 // Replace with the volume level you need to set
    audioEffectManager.setAllMusicVolume(volume)
    TXAudioEffectManager audioEffectManager = TUIRoomEngine.sharedInstance().getTRTCCloud().getAudioEffectManager();
    
    int volume = 60; // Replace with the volume level you need to set
    audioEffectManager.setAllMusicVolume(volume);
    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 avaliable.

    7x24 Phone Support