In a call or live interactive streaming, you may need to play custom audio or music files to all the users in the channel. For example, adding sound effects in a game, or playing background music. We provide two groups of methods for playing audio effect files and audio mixing.
The play audio effect methods can be used to play ambient sound, such as clapping and gunshots. You can play multiple audio effects at the same time, and preload the audio effect file for efficiency.
The audio effect file is specified by the file path, but the SDK uses the sound ID to identify the audio effect file. The SDK does not have the rule to define the sound ID. You need to ensure that each audio effect file has a unique sound ID. Common practices include automatically incrementing the ID, and using the hashCode of the audio effect file.
// Assigns a sound ID to the specified audio effect file path, and associates the audio effect file path with the sound ID. m_mapEffect.insert(std::make_pair(filePath, m_soundId++)); // Preloads the audio effect (recommended). Note the file size and preload the file before joining the channel. m_rtcEngine->preloadEffect(m_mapEffect[strEffect], filePath); // Sets the number of playback loops. -1 means an infinite loop. int loops = -1; // Sets the pitch of the audio effect. The value range is [0.5,2.0]. double pitch = 1.5; // Sets the volume. The value ranges between 0 and 100. 100 is the original volume. int gain = 100; // Sets the spatial position of the audio effect. 1.0 means the effect shows to the right. double pan = 1.0; // Sets whether to publish the audio effect. BOOL publish = true; // Plays the specified audio effect file. m_rtcEngine->playEffect(m_mapEffect[strEffect], filePath, loops, pitch, pan, gain, publish); // Stops playing the specified audio effect. m_rtcEngine->stopEffect(m_mapEffect[strEffect]); // Pauses the specified audio effect. m_rtcEngine->pauseEffect(m_mapEffect[strEffect]); // Pause all audio effects. m_rtcEngine->pauseAllEffects(); // Sets the volume of the audio effects. The value ranges between 0 and 100. m_rtcEngine->setEffectsVolume(50); // Resumes playing the audio effect. m_rtcEngine->resumeAllEffects(); // Stops playing all audio effects. m_rtcEngine->stopAllEffects(); // Releases a specified preloaded audio effect from the memory. m_rtcEngine->unloadEffect(m_mapEffect[strEffect]);
Audio mixing is playing a local or online music file while speaking, so that other users in the channel can hear the music. The audio mixing methods can be used to play background music. For example, playing music in the interactive live streaming. Only one music file can be played at one time. If you start playing a second music file during audio mixing, the first music file stops playing.
Agora audio mixing supports the following options:
// Specifies the absolute path of the local or online audio file to mix. std::string filePath = "http://www.hochmuth.com/mp3/Haydn_Cello_Concerto_D-1.mp3"; // Sets whether other users can hear the audio mixing. // If loopback is set as true, only the local user can hear the audio mixing; // if loopback is set as false, both users can hear the audio mixing. BOOL loopback = false; // Sets whether the audio captured by the microphone is replaced by the audio mixing file. // If replace is set as true, the SDK only publishes the specified audio file; // If replace is set as false, the local audio file is mixed with the audio stream from the microphone. BOOL replace = true; // Sets the number of playback loops. int cycle = 1; // Starts audio mixing. int nRet = m_rtcEngine->startAudioMixing(filePath, loopback, replace, cycle); // Sets the audio mixing volume for local playback. The value ranges between 0 and 100. m_rtcEngine->adjustAudioMixingPlayoutVolume(50); // Sets the audio mixing volume for publishing. The value ranges between 0 and 100. // Stops audio mixing. m_rtcEngine->stopAudioMixing();