Callbacks and listeners

更新时间:
复制 MD 格式

This topic describes the callback and listener APIs of the Android software development kit (SDK).

Contents

Note Callback APIs are invoked asynchronously.

AliRtcEngineEventListener: Provides callbacks related to the local user's behavior.

APIDescriptionSupported in the versions above
onJoinChannelResultCallback for the result of joining a channel.1.1
onLeaveChannelResultCallback for the result of leaving a channel.1.1
onNetworkQualityChangedCallback for when the network quality changes.1.1
onOccurWarningCallback for warnings.1.1
onOccurErrorCallback for errors.1.1
onFirstPacketSentCallback for when the first data packet is sent.1.1
onFirstPacketReceivedCallback for when the first data packet is received.1.13
onConnectionLostCallback for when the network connection is lost.1.14
onTryToReconnectCallback for when the SDK tries to reconnect to the network.1.14
onConnectionRecoveryCallback for when the network connection is recovered.1.14
onParticipantStatusNotifyCallback for when the status of a remote user changes.1.14
onPerformanceLowCallback for when the device performance is low.1.16
onPerformanceRecoveryCallback for when the device performance recovers.1.16
onFirstLocalVideoFrameDrawnCallback for when the first frame of the local preview is rendered.1.17.2
onAudioPlayingStateChangedCallback for the audio accompaniment playback state.1.16.2
onNetworkQualityProbeTestCallback for the network quality probe test.1.16.2
onFirstRemoteVideoFrameDrawnCallback for when the first frame of a remote video stream is rendered.1.16.2
onUserAudioMutedCallback for notifications that a user has muted or unmuted their audio.1.16.3
onUserVideoMutedCallback for notifications that a user has muted or unmuted their video.1.16.3
onMediaRecordEventCallback for recording events.1.17
onRtcLocalVideoStatsCallback for local video statistics.1.17
onRtcRemoteVideoStatsCallback for remote video performance statistics.1.17
onPublishChangedNotifyCallback for the stream publishing result.1.17.1
onSubscribeChangedNotifyCallback for the subscription result.1.17.1
onMediaExtensionMsgReceivedCallback for receiving custom data.1.17.1

AliRtcEngineNotify: Provides callbacks related to a remote user's behavior.

APIDescriptionSupported in the versions above
onRemoteUserOnLineNotifyCallback for when a remote user comes online.1.1
onRemoteUserOffLineNotifyCallback for when a remote user goes offline.1.1
onRemoteTrackAvailableNotifyCallback for when a remote user's audio or video stream changes.1.1
onRemoteUserUnPublishCallback for when a remote user stops publishing a stream.1.1
onAliRtcStatsCallback for real-time data. This callback is triggered every 2 seconds.1.16
onUpdateRoleNotifyCallback for when a user's role changes.1.16
onAudioEffectFinishedCallback for when an audio effect finishes playing.1.17.30
onByeCallback for when the user is removed from the channel by the server or the channel is closed.1.1
onParticipantSubscribeNotifyCallback for when a remote user subscribes to a stream. This callback is deprecated and not recommended.1.17
onFirstFramereceivedCallback for when the first frame is received.1.17
onParticipantUnsubscribeNotifyCallback for when a remote user unsubscribes from a stream.1.17
onUserAudioInterruptedBeginNotification for when a user's audio is interrupted, for example, by an incoming call.1.17
onUserAudioInterruptedEndedNotification for when a user's audio interruption ends.1.17
onUserWillResignActiveThe remote user's application moves to the background.1.17
onUserWillBecomeActiveCallback for when a remote user's application returns to the foreground.1.17
onAudioFocusChangeNotification for the result of an audio focus change. The SDK requests audio focus internally. If an external module needs to use the audio focus, it must request the focus again in this callback.1.17

AliRtcAudioVolumeObserver: Registers a callback for volume levels.

APIDescriptionSupported versions
AliRtcAudioVolumeObserverCallback for volume levels.1.16.2

AliAudioObserver: Callback for audio data.

APIDescriptionSupported in the preceding versions
onCaptureRawDataRaw audio data.1.17
onCaptureDataAudio data of the local published stream.1.17
onRenderDataRemote audio data.1.17
AliVideoObserver: Callback for video data.
APIDescriptionSupported in the versions listed above
onLocalVideoSampleLocally captured video data.1.17
onRemoteVideoSampleVideo data of a subscribed remote stream.1.17
AliDetectObserver: Pre-processing interface for facial recognition.
APIDescriptionSupported in the above versions
onDataCallback interface for facial recognition.1.15
AliRtcTextureObserver: Interface for video stream callbacks.
APIDescriptionSupported in the specified versions
onTextureCreateA texture for the local video stream is created.1.15
onTextureEach video frame is processed.1.15
onTextureDestroyThe texture for the local video stream is destroyed.1.15

Deprecated callback APIs

AliRtcEngineEventListener: Provides callbacks related to the local user's behavior.

APIDescriptionSupported in the versions above
onPublishResultCallback for the result of publishing an audio or video stream.1.1
onUnpublishResultCallback for stopping the publishing of an audio and video stream.1.1
onSubscribeResultCallback for a subscription.1.1
onUnsubscribeResultCallback for an unsubscription.1.1

API details

  • onJoinChannelResult: Callback for the result of joining a channel.
    void onJoinChannelResult(int result);
    ParameterTypeDescription
    resultint0 indicates that the user successfully joined the channel. A non-zero value indicates failure.
  • onLeaveChannelResult: Callback for the result of leaving a channel.
    void onLeaveChannelResult(int result);
    ParameterTypeDescription
    resultint0 indicates that the user successfully left the channel. A non-zero value indicates failure.
  • onNetworkQualityChanged: Callback for when the network quality changes.

    You can display a notification on the UI when the network quality is poor.

    void onNetworkQualityChanged(String uid, AliRtcNetworkQuality upQuality, AliRtcNetworkQuality downQuality);
    ParameterTypeDescription
    downQualityAliRtcNetworkQualityThe downlink network quality.
    upQualityAliRtcNetworkQualityThe uplink network quality.
    uidStringThe ID of the user whose network quality has changed. An empty UID indicates the local user. A non-empty UID indicates a remote user.
  • onOccurWarning: Callback for when a warning occurs.

    This warning callback can be ignored. It does not affect the normal use of the SDK.

    void onOccurWarning(int warn);
    ParameterTypeDescription
    warnintThe type of warning. For more information, see the error code table.
  • onOccurError: Callback for when an error occurs.

    If the error parameter is 16908812 or 33620229, call `destroy` to destroy the instance. Then, create a new SDK instance and call `joinChannel` to join the channel.

    void onOccurError(int error);
    ParameterTypeDescription
    errorintThe type of error. For more information, see Error codes.
  • onFirstPacketSent: Callback for when the first data packet is sent.
    public void onFirstPacketSent(String callId, String streamLabel, String trackLabel, int timeCost);
    ParameterTypeDescription
    callIdStringThe remote callId.
    streamLabelStringThe stream label.
    trackLabelStringThe stream tag.
    timeCostintThe time elapsed in milliseconds (ms).
  • onFirstPacketReceived: Callback for when the first data packet is received.
    void onFirstPacketReceived(String callId, String streamLabel, String trackLabel, int timeCost);
    ParameterTypeDescription
    callIdStringThe remote callId.
    streamLabelStringThe stream label.
    trackLabelStringThe track label.
    timeCostintThe time elapsed in milliseconds (ms).
  • onConnectionLost: Callback for when the network connection is lost.
    void onConnectionLost();
  • onTryToReconnect: Callback for when the SDK tries to reconnect to the network.
    void onTryToReconnect();
  • onConnectionRecovery: Callback for when the network connection is recovered.
    void onConnectionRecovery();
  • onParticipantStatusNotify: Callback for when the status of a remote user changes.
    void onParticipantStatusNotify(AliStatusInfo[] status_info_list, int count);
    ParameterTypeDescription
    status_info_listAliStatusInfo[]An array of user statuses.
    countintThe length of the array.
  • onPerformanceLow: Callback for when the device performance is low.

    void onPerformanceLow();
  • onPerformanceRecovery: Callback for when the device performance recovers.

    void onPermormanceRecovery();
  • onFirstLocalVideoFrameDrawn: Callback for when the first frame of the local preview is rendered.

    void onFirstLocalVideoFrameDrawn();
  • onAudioPlayingStateChanged: Callback for the playback state of the audio accompaniment.

    public void onAudioPlayingStateChanged(AliRtcAudioPlayingStateCode playState, AliRtcAudioPlayingErrorCode errorCode) {}
    ParameterTypeDescription
    playStatusAliRtcAudioPlayingStatusThe current playback state. Valid values:
    • AliRtcAudioPlayingStarted: Playback starts.
    • AliRtcAudioPlayingStopped: Playback stops.
    • AliRtcAudioPlayingPaused: Playback is paused.
    • AliRtcAudioPlayingResumed: Playback resumes.
    • AliRtcAudioPlayingEnded: Playback ends.
    • AliRtcAudioPlayingBuffering: Buffering.
    • AliRtcAudioPlayingBufferingEnd: Buffering ends.
    • AliRtcAudioPlayingFailed: Playback fails.
    errorCodeAliRtcAudioPlayingErrorCodeThe error code.
  • onNetworkQualityProbeTest: Callback for the result of a network quality probe test.

    void onNetworkQualityProbeTest(AliRtcNetworkQuality networkQuality);
    ParameterTypeDescription
    networkQualityAliRtcNetworkQualityThe network quality.
  • onFirstRemoteVideoFrameDrawn: Callback for when the first frame of a remote video stream is rendered.

     void onFirstRemoteVideoFrameDrawn(String uid, AliRtcVideoTrack videoTrack);
    ParameterTypeDescription
    uidStringThe user ID.
    VideoTrackAliRtcVideoTrackThe video stream type.
  • onUserAudioMuted: Callback for when a user mutes or unmutes their audio.

    void onUserAudioMuted(String uid, boolean mute);
    ParameterTypeDescription
    uidStringThe user ID. This is a unique identifier assigned by the AppServer.
    mutebooleantrue: muted. false: unmuted.
  • onUserVideoMuted: Callback for when a user mutes or unmutes their video.

    void onUserVideoMuted(String uid, boolean mute);
    ParameterTypeDescription
    uidStringThe user ID. This is a unique identifier assigned by the AppServer.
    mutebooleantrue: Black frames are published. false: The video is published as normal.
  • onMediaRecordEvent: Callback for recording events.

    public void onMediaRecordEvent(int result, String filePath);
    ParameterTypeDescription
    resultint
    • 0: Recording starts.
    • 1: Recording ends.
    • 2: Failed to open the file.
    • 3: Failed to write to the file.
    filePathStringThe path where the recording file is stored.
  • onRtcLocalVideoStats: Callback for local video statistics.

    public void onRtcLocalVideoStats(AliRTCLocalVideoStats aliRtcStats);
    ParameterTypeDescription
    aliRtcStatsAliRTCLocalVideoStatsThe data of the published video stream.
  • onRtcRemoteVideoStats: Callback for remote video statistics.

    public void onRtcRemoteVideoStats(AliRTCRemoteVideoStats aliRtcStats);
    ParameterTypeDescription
    aliRtcStatsAliRTCRemoteVideoStatsThe data of the subscribed video stream.
  • onPublishChangedNotify: Callback for the result of publishing a stream.

    public void onPublishChangedNotify(int result, boolean isPublished);
    ParameterTypeDescription
    resultint0 indicates success. Other values indicate failure.
    isPublishedbooleantrue: The stream is published. false: The stream is unpublished.
  • onSubscribeChangedNotify: Callback for the result of subscribing to a stream.

    public void onSubscribeChangedNotify(String uid, AliRtcAudioTrack audioTrack, AliRtcVideoTrack videoTrack);
    ParameterTypeDescription
    uidStringThe user ID.
    audioTrackAliRtcAudioTrackThe audio stream.
    videoTrackAliRtcVideoTrackThe video stream.
  • onMediaExtensionMsgReceived: Callback for receiving custom data from a user in the channel.
    public void onMediaExtensionMsgReceived(String uid byte[]message) {}
    ParameterTypeDescription
    uidStringThe user UID.
    messagebyte[]The received custom data.
  • onRemoteUserOnLineNotify: Callback for when a remote user goes online.
    void onRemoteUserOnLineNotify(String uid);
    ParameterTypeDescription
    uidStringThe ID of the remote user.
  • onRemoteUserOffLineNotify: Callback for when a remote user goes offline.
    void onRemoteUserOffLineNotify(String uid);
    ParameterTypeDescription
    uidStringThe ID of the remote user.
  • onRemoteTrackAvailableNotify: Callback for when the state of a remote user's audio or video stream changes.
    void onRemoteTrackAvailableNotify(String uid, AliRtcAudioTrack audioTrack, AliRtcVideoTrack videoTrack);
    ParameterTypeDescription
    uidStringThe ID of the remote user.
    audioTrackAliRtcAudioTrackThe audio stream of the remote user after the change.
    videoTrackAliRtcVideoTrackThe video stream of the remote user after the change.
  • onAliRtcStats: Callback for real-time data. This callback is triggered every 2 seconds.

    void onAliRtcStats(AliRtcStats stats);
    ParameterTypeDescription
    statsAliRtcStatsThe real-time data of the user.
  • onUpdateRoleNotify: Callback for when a user's role changes.

    void onUpdateRoleNotify(AliRTCSDK_Client_Role old_role , AliRTCSDK_Client_Role new_role);
    ParameterTypeDescription
    old_roleAliRTCSDK_Client_RoleThe role before the change.
    new_roleAliRTCSDK_Client_RoleThe role after the change.
  • onAudioEffectFinished: Callback for when an audio effect finishes playing.
    public void onAudioEffectFinished(int soundId) {}
    ParameterTypeDescription
    soundIdintThe ID that the user assigned to the audio effect file.
  • onBye: Callback for when a user is removed from the channel by the server or when the channel is closed.
    void onBye(int code);
    ParameterTypeDescription
    codeintThe message type.
    • 1: The user is removed from the channel by the server.
    • 2: The channel is closed.
    • 3: The user is removed from the channel because the same UID is used to log on from another device.
  • onParticipantSubscribeNotify: Callback for when a remote user subscribes to a stream. This callback is deprecated and not recommended.
    public void onParticipantSubscribeNotify(AliSubscriberInfo[] subcribeinfoList, int feedCount){}
    ParameterTypeDescription
    subcribeinfoListAliSubscriberInfo[]Information about the remote users who subscribed to the stream.
    feedCountintNumber of subscriptions
  • onFirstFrameReceived: Callback for when the first frame is received.
    public void onFirstFramereceived(String userId, AliRtcVideoTrack videoTrack, AliRtcAudioTrack audioTrack, int timeCost) {}
    ParameterTypeDescription
    userIdStringThe user UID.
    videoTrackAliRtcVideoTrackThe type of the successfully received video stream.
    audioTrackAliRtcVideoTrackThe type of the successfully received audio stream.
    timeCostintThe time elapsed in milliseconds (ms).
  • onParticipantUnsubscribeNotify: Callback for when a remote user unsubscribes from a stream.
    void onParticipantUnsubscribeNotify(AliParticipantInfo[] participantList, int feedCount)
    ParameterTypeDescription
    participantListAliParticipantInfoInformation about the remote users who unsubscribed from the stream.
    feedCountintThe number of users who unsubscribed.
  • onUserAudioInterruptedBegin: Callback for when a user's audio is interrupted, such as when the audio is preempted by an incoming call.
    void onUserAudioInterruptedBegin(String uid);
    ParameterTypeDescription
    uidStringThe user ID. This is a unique identifier assigned by the AppServer.
  • onUserAudioInterruptedEnded: Callback for when a user's audio interruption ends.
    void onUserAudioInterruptedEnded(String uid);
    ParameterTypeDescription
    uidStringThe user ID. This is a unique identifier assigned by the AppServer.
  • onUserWillResignActive: Callback for when a remote user's application is switched to the background.
    void onUserWillResignActive(String uid);
    ParameterTypeDescription
    uidStringThe user ID. This is a unique identifier assigned by the AppServer.
  • onUserWillBecomeActive: Callback for when a remote user's application returns to the foreground.
    void onUserWillBecomeActive(String uid);
    ParameterTypeDescription
    uidStringThe user ID. This is a unique identifier assigned by the AppServer.
  • onAudioFocusChange: Callback for the result of an audio focus change. The SDK requests audio focus internally. If an external module also needs to use the audio focus, it must request the focus again in this callback.
    public void onAudioFocusChange(int focusChange){}
    ParameterTypeDescription
    focusChangeintThe audio state. Valid values:
    • AudioManager.AUDIOFOCUS_GAIN: A permanent audio focus gain.
    • AudioManager.AUDIOFOCUS_GAIN_TRANSIENT: A transient audio focus gain.
    • AudioManager.AUDIOFOCUS_LOSS: A permanent audio focus loss.
    • AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: A transient audio focus loss.
  • AliRtcAudioVolumeObserver: Registers a callback for volume levels.
    void onAudioVolume(List<AliRtcEngine.AliRtcAudioVolume> list, int volume);
    ParameterTypeDescription
    listList<AliRtcEngine.AliRtcAudioVolume>If the UID is 0, it indicates the volume of the local user. If the UID has a value, it indicates the volume of a remote user.
    volumeintThe current volume of the remote mixed stream.
  • onCaptureRawData: Callback for raw audio data.
    void onCaptureRawData(long dataPtr, int numSamples, int bytesPerSample, int numChannels, int sampleRate, int samplesPerSec);
    ParameterTypeDescription
    dataPtrlongThe audio data.
    numOfSamplesint The number of samples.
    bytesPerSampleint The number of bytes per sample. For PCM data, this is typically 16 bits, which is 2 bytes.
    numOfChannelsint The number of sound channels. Valid values:
    • 1: mono.
    • 2: stereo.
    sampleRateint The sample rate.
    samplesPerSecint The number of samples per second for each sound channel.
  • onCaptureData: Callback for the audio data of the local published stream.
    public void onCaptureData(long dataPtr, int numSamples, int bytesPerSample, int numChannels, int sampleRate, int samplesPerSec);
    ParameterTypeDescription
    dataPtrlongThe audio data.
    numOfSamplesint The number of samples.
    bytesPerSampleint The number of bytes per sample. For PCM data, this is typically 16 bits, which is 2 bytes.
    numOfChannelsint The number of sound channels. Valid values:
    • 1: mono.
    • 2: stereo.
    sampleRateintThe sample rate.
    samplesPerSecint The number of samples per second for each sound channel.
  • onRenderData: Callback for remote audio data.
    public void onRenderData(long dataPtr, int numSamples, int bytesPerSample, int numChannels, int sampleRate, int samplesPerSec);
    ParameterTypeDescription
    dataPtrlongThe audio data.
    numOfSamplesint The number of samples.
    bytesPerSampleint The number of bytes per sample. For PCM data, this is typically 16 bits, which is 2 bytes.
    numOfChannelsint The number of sound channels. Valid values:
    • 1: mono.
    • 2: stereo.
    sampleRateint The sample rate.
    samplesPerSecint The number of samples per second for each sound channel.
  • onLocalVideoSample: Callback for locally captured video data.
    void onLocalVideoSample(AliRtcEngine.AliVideoSourceType videoSourceType, AliRtcEngine.AliVideoSample videoSample);
    ParameterTypeDescription
    videoSourceTypeAliVideoSourceTypeThe video source.
    videoSampleAliVideoSampleThe video sample.
  • onRemoteVideoSample: Callback for the video data of a subscribed remote stream.
    void onRemoteVideoSample(String userId, AliRtcEngine.AliVideoSourceType videoSourceType, AliRtcEngine.AliVideoSample videoSample);
    ParameterTypeDescription
    userIdStringThe user ID.
    videoSourceTypeAliVideoSourceTypeThe video source.
    videoSampleAliVideoSampleThe video sample.
  • onData: Callback interface for facial recognition.
    long onData(long dataFrameY, long dataFrameU, long dataFrameV, AliRtcEngine.AliRTCImageFormat aliRTCImageFormat, int width, int height, int strideY, int strideU, int strideV, int rotate, long extraData);
    ParameterTypeDescription
    dataFrameYlongA pointer to the Y component.
    dataFrameUlongA pointer to the U component.
    dataFrameVlongA pointer to the V component. This pointer is null for NV12 and NV21.
    aliRTCImageFormatAliRtcEngine.AliRTCImageFormatThe image data format.
    widthintThe image width.
    heightintThe image height.
    strideYintThe stride of the Y component of the image.
    strideUintThe stride of the U component of the image.
    strideVintThe stride of the V component of the image.
    rotateintThe rotation angle of the image.
    extraDatalongAn additional field. You can ignore this parameter if it is not customized.
  • onTextureCreate: Callback for when a texture for the local video stream is created.
    void onTextureCreate(String userId, long context);
    ParameterTypeDescription
    userIdStringThe ID of the subscribed user. If retouching is required for the local user, enter an empty string ("") or the local UID.
    contextlongA pointer to the OpenGL context EGLContext.
  • onTexture: Callback for processing each video frame.
    int onTexture(String userId, int textureId, int width, int height, int stride, int rotate, long extraData);
    ParameterTypeDescription
    userIdStringThe ID of the subscribed user. If retouching is required for the local user, enter an empty string ("") or the local UID.
    textureIdintThe input texture ID of the video stream.
    widthintThe width of the video stream texture.
    heightintThe height of the video stream texture.
    strideintThe stride of the video stream texture.
    rotateintThe rotation angle of the video stream texture.
    extraDatalongThe detected face data. Valid values:
    • If facial recognition is enabled: A pointer to the face structure data returned by the onData callback of AliDetectObserver.
    • If facial recognition is disabled: 0.

    Return value

    Returns the processed texture ID. If retouching is not required, you can return the original texture ID.

  • onTextureDestroy: Callback for when the texture for the local video stream is destroyed.
    void onTextureDestroy(String userId);
    ParameterTypeDescription
    userIdStringThe ID of the subscribed user. If retouching is required for the local user, enter an empty string ("") or the local UID.
  • onPublishResult: Callback for the result of publishing an audio or video stream.
    void onPublishResult(int result, String publishId);
    ParameterTypeDescription
    resultint0 indicates success. A non-zero value indicates failure.
    publishIdStringThe stream ID.
  • onUnpublishResult: Callback for the result of unpublishing an audio or video stream.
    void onUnpublishResult(int result);
    ParameterTypeDescription
    resultint0 indicates success. A non-zero value indicates failure.
  • onSubscribeResult: Callback for the result of subscribing to a stream.
    void onSubscribeResult(String uid, int result, AliRtcVideoTrack vt, AliRtcAudioTrack at);
    ParameterTypeDescription
    uidStringThe user ID.
    resultint0 indicates success. A non-zero value indicates failure.
    vtAliRtcVideoTrackThe successfully subscribed video stream.
    atAliRtcAudioTrackThe successfully subscribed audio stream.
  • onUnsubscribeResult: Callback for the result of unsubscribing from a stream.
    void onUnsubscribeResult(int result, String userId);
    ParameterTypeDescription
    resultint0 indicates success. A non-zero value indicates failure.
    userIdStringThe user ID.
  • onRemoteUserUnPublish: Callback for when a remote user unpublishes a stream.
    void onRemoteUserUnPublish(AliRtcEngine rtcEngine, String userId);
    ParameterTypeDescription
    rtcEngineAliRtcEngineThe AliRtcEngine instance.
    userIdStringThe ID of the remote user.