Playing Audio Data Generated on the Audio Engine Server
Application Scenario
Play the audio data sent from the audio engine server by calling the API provided by the audio engine.
Prerequisites
N/A
Development Process
- Receive the audio data sent from the audio engine server through the communication module.
- Call the API provided by the audio engine client to play the audio.
- Register the hook function for saving the audio data on the client. The audio engine client saves the received audio data by using the hook function.
- Call the API provided by the audio engine client to enable or disable audio playback.
Encoding Instance
Scenario
According to the development process, call the API of the audio engine client to play the audio data sent by the server and save the audio data.
Code Sample
import android.util.Log;
import com.huawei.cloudgame.audioplay.AUDIOSAVEHOOK;
import com.huawei.cloudgame.audioplay.AudioTrackPlayer;
public class DataPipe implements AUDIOSAVEHOOK {
private static DataPipe instance = new DataPipe();
private DataPipe() {
}
public static DataPipe getInstance() {
return instance;
}
public void audioSaveData(byte[] data, int length) {
// The audio data saving function is implemented by you.
}
public static void registerAudioSaveHook() {
int ret = AudioTrackPlayer.getInstance().registerAudioSaveHook(DataPipe.getInstance());
if (ret != AudioTrackPlayer.VMI_SUCCESS) {
Log.e("AudioClient", "register audio save hook fail. ret=%d", ret);
}
}
}
public class Test {
public static void main(String[] args) {
DataPipe.registerAudioSaveHook();
byte[] data = getRecvData() // Example function for obtaining the audio data sent from the audio engine server.
int ret = AudioTrackPlayer.getInstance().onRecvAudioPacket(data, data.length);
if (ret != AudioTrackPlayer.VMI_SUCCESS) {
Log.e("AudioClient", "audio client play audio fail");
}
ret = AudioTrackPlayer.startAudio();
if (ret != AudioTrackPlayer.VMI_SUCCESS) {
Log.e("AudioClient", "audio client start fail");
}
ret = AudioTrackPlayer.stopAudio();
if (ret != AudioTrackPlayer.VMI_SUCCESS) {
Log.e("AudioClient", "audio client stop fail");
}
}
}
Parent topic: Audio Engine Client