Android
Updated at:
By reading this article, you can learn how to output audio and video data.
Output video data
- When an application needs to output video media data, it must register AliVideoObserver callbacks to implement onLocalVideoSample and onRemoteVideoSample callbacks to receive locally collected video naked data and subscribed remote video naked data.
// Receive local data callback void onLocalVideoSample(AliVideoSourceType sourceType, AliVideoSample videoSample); // The void onRemoteVideoSample of the callback for receiving remote data (String callId, AliVideoSourceType sourceType, AliVideoSample videoSample);Note Currently, the default local data callback for Android data output only supports YUV(NV21) format data output, and remote data only supports YUV(I420) output. If the application side has other format requirements, please convert it yourself. - After an application creates an AliRtcEngine engine instance, it calls the operation to registerVideoSampleObserver register the output of bare video data. After registration, when preview is enabled or stream pulling is subscribed to, the video data is continuously called back through onCaptureVideoSample and onRemoteVideoSample.
AliRtcEngine mAliRtcEngine = AliRtcEngine.getInstance(getApplicationContext()); // Register the bare data output of the video. mAliRtcEngine.registerVideoSampleObserver(new AliRtcEngine.AliVideoObserver() { @Override public void onLocalVideoSample(AliRtcEngine.AliVideoSourceType sourceType, AliRtcEngine.AliVideoSample videoSample) { } @Override public void onRemoteVideoSample(String callId, AliRtcEngine.AliVideoSourceType sourceType, AliRtcEngine.AliVideoSample videoSample) { } }); // Start previewing, streaming, and subscribe to videos of other users. .....Note To collect video data locally, you must enable the camera to preview or push the video stream. To collect video data locally, you must enable the camera to preview or push the video stream. To generate the callback, you must also enable the remote video data to subscribe to the video stream of another user. - To stop receiving bare video data, you can call the unRegisterVideoSampleObserver operation to disable the output of bare video data.
// Stop the output of bare video data. mAliRtcEngine.unRegisterVideoSampleObserver();
Output audio data
- When an application needs to output audio media data, it first needs to register the registerAudioObserver interface to implement AliAudioObserver callback for receiving audio media data. The data format is PCM data. Currently, the SDK supports outputting audio data of different links. The registration uses the AliAudioType parameter to specify the type of the current callback audio data.
The specific meaning is as follows:
- PUBOBSERVER: the audio data processed by Audio 3A.
- SUBOBSERVER: the audio data that is remixed by the current subscribed remote user.
- RAWDATAOBSERVER: raw audio data collected locally.
// AliAudioObserver interface data callback. public interface AliAudioObserver { /* @param dataPtr audio data @ param numSamples Number of samples @ param bytesPerSample @ param numChannels Number of channels @ param sampleRate sample rate @ param samplesPerSec Sample Count (bytes) */ void onCaptureRawData(long dataPtr, int numSamples, int bytesPerSample, int numChannels, int sampleRate, int samplesPerSec); void onCaptureData(long dataPtr, int numSamples, int bytesPerSample, int numChannels, int sampleRate, int samplesPerSec); void onRenderData(long dataPtr, int numSamples, int bytesPerSample, int numChannels, int sampleRate, int samplesPerSec); } - After an AliRtcEngine instance is created, an application registers an audio data callback. The AliAudioType parameter specifies the type of audio data to be output. For more information about the type of audio data to be output, see the previous step. After the registration is complete, the audio data will be continuously called back through the AliAudioObserver.
AliRtcEngine mAliRtcEngine = AliRtcEngine.getInstance(getApplicationContext()); // Start local audio data collection and output. For more information about other types of type output, see enumerated AliAudioType. mAliRtcEngine.registerAudioObserver(type, new AliRtcEngine.AliAudioObserver() { @Override public void onCaptureRawData(long dataPtr, int numSamples, int bytesPerSample, int numChannels, int sampleRate, int samplesPerSec) { } @Override public void onCaptureData(long dataPtr, int numSamples, int bytesPerSample, int numChannels, int sampleRate, int samplesPerSec) { } @Override public void onRenderData(long dataPtr, int numSamples, int bytesPerSample, int numChannels, int sampleRate, int samplesPerSec) { } }); // Start audio streaming and subscribe to videos from other users. .....Note- The callback output is only available when the microphone is turned on to start pushing audio streams for local audio data collection. The callback output is also available when the remote video data is subscribed to the audio streams of other users.
- The VOLUME_DATA_OBSERVER type in the audio type enumeration AliAudioType is registered user volume value callback, which has been obsolete. You must use the registerAudioVolumeObserver interface since version 1.16.
- When you need to stop receiving bare video data, call the operation unRegisterAudioObserver to disable audio data output. The stop type must correspond to the audio data type registered in Step 2.
// Stop local audio data collection and output. The stop type must correspond to the audio data type AliAudioType registered in step 2. pEngine->unRegisterAudioObserver(type);
Is this page helpful?