react-native-agora Documentation

repository·main·Indexed 20 days ago

https://github.com/agoraio-extensions/react-native-agora

Official Agora RTC SDK for React Native (version 4.6.2), providing real-time audio and video communication capabilities for Android and iOS. Includes guides for Expo integration, engine initialization, basic video call implementation, and advanced features like Picture-in-Picture (PiP) and custom video/audio source configuration.

Tokens
20.5K
Snippets
62
Records
80
Agent score
71%

What's inside react-native-agora

  1. Manage PiP lifecycle and control styles

    main

    When using the PiP feature, follow these best practices for memory management and UI customization:

    Memory Management

    Always dispose of the AgoraPipController when it is no longer needed to prevent memory leaks.

    iOS Control Styles

    You can customize the appearance of the PiP window on iOS using the following control style indices:

    • 0: All system controls (default)
    • 1: Hide forward/backward buttons
    • 2: Hide play/pause and progress bar (recommended for video conferencing)
    • 3: Hide all controls
  2. Implement Picture-in-Picture (PiP) for iOS

    main

    iOS requires specific capabilities and user-initiated actions for PiP to function correctly.

    1. Configure Background Modes

    In Xcode:

    1. Select your app's target and go to the Signing & Capabilities tab.
    2. Click the + Capability button.
    3. Add Background Modes.
    4. Select Audio, AirPlay, and Picture in Picture.

    2. Camera Access in Multitasking (Optional)

    If you need to show the local video stream in the PiP window while multitasking:

    • iOS < 16: You must request the com.apple.developer.avfoundation.multitasking-camera-access entitlement from Apple.
    • iOS ≥ 16: Set multitaskingCameraAccessEnabled to true in the capture session (feature coming soon).

    3. Important iOS Requirement

    PiP must be initiated by a user action on iOS. Programmatic or automatic activation may lead to App Store rejection.

  3. Implement Picture-in-Picture (PiP) for Android

    main

    To enable Picture-in-Picture on Android, you must declare support in your AndroidManifest.xml and ensure your MainActivity inherits from AgoraPIPActivity to handle automatic PiP mode entry (especially for Android 12+).

    1. Update AndroidManifest.xml

    Add android:supportsPictureInPicture="true" and include the necessary android:configChanges to your activity:

    <activity android:name=".MainActivity"
        android:supportsPictureInPicture="true"
        android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
        ...

    2. Configure MainActivity

    Inherit from AgoraPIPActivity to leverage built-in automatic PiP activation when the app goes to the background. If you need to customize this behavior, you can implement the AgoraPIPActivityProxy interface.

  4. Install react-native-agora

    main

    To install the SDK in a React Native project (version >= 0.60.0), use npm or yarn, then install the iOS dependencies using CocoaPods.

    Note: This SDK is designed for Agora Video 4.x APIs. For older 3.x versions, refer to the 3.x branch.

    # Using yarn
    yarn add react-native-agora
    
    # Or using npm
    npm i --save react-native-agora
    
    # For iOS
    cd ios && pod install
  5. Set Minimum Deployment Targets for Agora

    main

    Agora SDK requires specific minimum SDK versions. Use expo-build-properties to configure these in your app.json.

    Requirements:

    • Android: minSdkVersion = 24
    • iOS: deploymentTarget = 12.4+

    Setup:

    1. Install the plugin:
      npx expo install expo-build-properties --save-dev
    2. Configure app.json:
    {
      "expo": {
        "plugins": [
          [
            "expo-build-properties",
            {
              "android": {
                "minSdkVersion": 24
              },
              "ios": {
                "deploymentTarget": "12.4"
              }
            }
          ]
        ]
      }
    }
  6. Setup an Expo project with Agora SDK

    main

    To use Agora in an Expo-managed application, you must use expo-dev-client to enable native module support. Follow these steps to initialize your project:

    1. Create the Expo app:
      npx create-expo-app my-agora-app && cd my-agora-app
    2. Install expo-dev-client:
      npx expo install expo-dev-client
    3. Install the Agora SDK:
      npx expo install react-native-agora
    4. Test the build (this compiles the project using local Android SDK or Xcode):
      • Android: npx expo run:android
      • iOS: npx expo run:ios
    npx create-expo-app my-agora-app && cd my-agora-app
    npx expo install expo-dev-client
    npx expo install react-native-agora
  7. Configure App Permissions for Agora in Expo

    main

    Agora requires specific permissions for camera, microphone, and network access. These must be configured in your app.json.

    Android Permissions

    Add the following to the android.permissions array in app.json:

    • android.permission.CAMERA
    • android.permission.RECORD_AUDIO
    • android.permission.MODIFY_AUDIO_SETTINGS
    • android.permission.ACCESS_WIFI_STATE
    • android.permission.ACCESS_NETWORK_STATE
    • android.permission.BLUETOOTH
    • android.permission.FOREGROUND_SERVICE

    iOS Permissions

    Use the expo-camera plugin in app.json to define the permission request messages:

    {
      "expo": {
        "plugins": [
          [
            "expo-camera",
            {
              "cameraPermission": "Allow $(PRODUCT_NAME) to access your camera for video calls",
              "microphonePermission": "Allow $(PRODUCT_NAME) to access your microphone for audio calls"
            }
          ]
        ]
      }
    }
    {
      "expo": {
        "android": {
          "permissions": [
            "android.permission.CAMERA",
            "android.permission.RECORD_AUDIO",
            "android.permission.MODIFY_AUDIO_SETTINGS",
            "android.permission.ACCESS_WIFI_STATE",
            "android.permission.ACCESS_NETWORK_STATE",
            "android.permission.BLUETOOTH",
            "android.permission.FOREGROUND_SERVICE"
          ]
        }
      }
    }
  8. Manage multiple channels with IRtcEngineEx

    main

    The IRtcEngineEx class extends IRtcEngine to provide multi-channel capabilities, allowing you to join and manage multiple channels simultaneously. Most methods in this class require an RtcConnection object to specify which channel/user context the operation applies to.

    Key Multi-Channel Methods

    • joinChannelEx: Joins a specific channel. If you are already in a channel, ensure you use a different user ID to join the same channel again.
      • Returns: 0 on success, or a negative error code (e.g., -2 for invalid parameters, -17 if already in the channel, -102 for invalid channel name).
    • leaveChannelEx: Leaves a specific channel and stops all audio/video interactions. This is an asynchronous method; the channel is not immediately left when the call returns.
    • updateChannelMediaOptionsEx: Updates media options (like role or audio/video subscription) for a specific channel after joining.
    • setVideoEncoderConfigurationEx: Sets the maximum achievable video encoding properties (resolution, bitrate, etc.) for a specific channel connection.
    // Example concept of using RtcConnection with IRtcEngineEx
    const connection = {
      channelId: 'my-channel',
      localUid: 12345
    };
    
    // Joining a channel
    engineEx.joinChannelEx(token, connection, channelMediaOptions);
    
    // Leaving a specific channel
    engineEx.leaveChannelEx(connection, { stopMicrophoneRecording: true });
  9. Work with Data Streams

    main

    Agora allows you to create and send custom data streams (up to 5 per user) for non-media data.

    1. Create a stream: Use createDataStreamEx(config, connection) to get a streamId.
    2. Send data: Use sendStreamMessageEx(streamId, data, length, connection) to broadcast data to all users in the channel.

    Restrictions:

    • Max 5 data channels per client.
    • Total sending bitrate limit: 30 KB/s.
    • Max 60 packets per second per channel.
    • Max 1 KB per packet.
    // 1. Create the stream
    const streamId = engineEx.createDataStreamEx(dataStreamConfig, connection);
    
    // 2. Send data
    const data = new Uint8Array([0x01, 0x02, 0x03]);
    engineEx.sendStreamMessageEx(streamId, data, data.length, connection);
  10. Exclude Screen Sharing module to avoid FOREGROUND_SERVICE_MEDIA_PROJECTION permission

    main

    If your application does not use the screen sharing feature, you can exclude the screen sharing extension in your project-level build.gradle to avoid requiring the FOREGROUND_SERVICE_MEDIA_PROJECTION permission.

    // build.gradle (project-level)
    
    configurations.configureEach {
        exclude group: "io.agora.rtc", module: "full-screen-sharing"
    }
  11. Fix Pod install failure (conflicting libcrypto.a)

    main

    If you encounter a pod install error stating that targets have libraries with conflicting names (e.g., libcrypto.a), you must disable Flipper in your iOS configuration. This is common if you have use_frameworks! enabled.

    1. In your Podfile, comment out the Flipper configuration:
    # add_flipper_pods!
    # post_install do |installer|
    #   flipper_post_install(installer)
    # end
    1. Comment out the Flipper initialization code in your AppDelegate.
    # Disable these lines in Podfile if pod install fails due to conflicting names
    add_flipper_pods!
    post_install do |installer|
      flipper_post_install(installer)
    end
  12. Initialize the Agora RTC Engine

    main

    To start using the SDK, import createAgoraRtcEngine and initialize it with your Agora App ID. This creates the engine instance required for all RTC operations.

    import { createAgoraRtcEngine } from 'react-native-agora';
    
    const engine = createAgoraRtcEngine();
    engine.initialize({ appId: 'YOUR APP ID' });