react-native-incall-manager

repository·master·Indexed 20 days ago

https://github.com/react-native-webrtc/react-native-incall-manager

A telecommunication module for React Native designed to handle media routes, sensors, and events during audio/video calls. Optimized for use with react-native-webrtc, it provides APIs for managing speakerphone routing, proximity sensors, ringtones, ringback tones, and audio focus on Android. Version 4.2.2.

Tokens
3.3K
Snippets
12
Records
13
Agent score
20%

What's inside react-native-incall-manager

  1. Configure Android installation

    master

    The module requires Android 7.0 (API level 24) or later.

    1. Set minSdkVersion to at least 24 in android/app/build.gradle.
    2. Ensure you have android.permission.BLUETOOTH permissions if using Bluetooth.
    3. Add the Android support library v4 to your dependencies in android/app/build.gradle: compile "com.android.support:support-v4:$YOUR_VERSION"
    4. Link the module using react-native link react-native-incall-manager or manually follow the manual linking steps in the documentation.
    // android/app/build.gradle
    dependencies {
        compile "com.android.support:support-v4:$YOUR_VERSION"
        compile(project(':react-native-incall-manager'))
    }
  2. Handle incoming ringtones

    master

    For incoming calls, use startRingtone to notify the user before they pick up.

    • startRingtone(ringtone, vibrate_pattern, ios_category, seconds)
    • ringtone can be _DEFAULT_, _BUNDLE_, or a specific system filename with extension.
    • On iOS, only .mp3 files are supported if using _BUNDLE_.
    • Once the user picks up or hangs up, call stopRingtone().
    import InCallManager from 'react-native-incall-manager';
    
    // Play ringtone for incoming call
    InCallManager.startRingtone('_BUNDLE_');
    
    // When user picks up
    InCallManager.stopRingtone();
    InCallManager.start();
    
    // Or if user hangs up
    InCallManager.stopRingtone();
    InCallManager.stop();
  3. Use InCallManager for basic call handling

    master

    The module provides automatic handling for media routing, proximity sensors, and screen state.

    • Starting a call: Use InCallManager.start({media: 'audio' | 'video', ringback: string}). If media is audio, it routes to the earpiece; if video, it routes to the speaker.
    • Stopping a call: Use InCallManager.stop({busytone: string}) to clean up listeners and restore settings.
    • Ringback: Used for outgoing calls. Pass _BUNDLE_, _DEFAULT_, or _DTMF_ to start(). You must call InCallManager.stopRingback() when the callee answers.
    • Busytone: Used for failed outgoing calls. Pass _BUNDLE_, _DEFAULT_, or _DTMF_ to stop().
    import InCallManager from 'react-native-incall-manager';
    
    // Start manager (e.g., on call established)
    InCallManager.start({media: 'audio'});
    
    // Stop manager (e.g., on call hangup)
    InCallManager.stop();
    
    // Example with ringback for outgoing calls
    InCallManager.start({media: 'audio', ringback: '_BUNDLE_'});
    // ... when answered ...
    InCallManager.stopRingback();
  4. Configure iOS installation

    master

    You can install the module using react-native link or CocoaPods.

    Using CocoaPods: Add the following line to your Podfile, updating the path to your node_modules directory: pod 'ReactNativeIncallManager', :path => '../node_modules/react-native-incall-manager'

    Manual Linking (if react-native link fails):

    1. Drag node_modules/react-native-incall-manager/ios/RNInCallManager.xcodeproj into your Xcode project under Libraries.
    2. In Xcode, go to Build Phases -> Link Binary With Libraries and add libRNInCallManager.a from the project.
    3. In Build Settings, add $(SRCROOT)/../node_modules/react-native-incall-manager/ios/RNInCallManager to Header Search Paths.
    pod 'ReactNativeIncallManager', :path => '../node_modules/react-native-incall-manager'
  5. InCallManager Methods API Reference

    master

    The following methods are available on the InCallManager object:

    MethodAndroidiOSDescription
    start({media: string, auto: boolean, ringback: string})Starts manager. media is 'audio' or 'video'. ringback accepts non-empty string to play. Default: {media:'audio', auto: true, ringback: ''}
    stop({busytone: string})Stops manager. busytone accepts non-empty string to play. Default: {busytone: ''}
    turnScreenOn()Force turn screen on
    turnScreenOff()Force turn screen off
    setKeepScreenOn(enable: boolean)Set KeepScreenOn flag. Default: false
    setSpeakerphoneOn(enable: boolean)Toggle speaker ON/OFF once. Not forced. Default: false
    setForceSpeakerphoneOn(flag: boolean)true -> force on; false -> force off; null -> use default behavior. Default: null
    setMicrophoneMute(enable: boolean)Mute/unmute microphone. Default: false
    getAudioUriJS()(Async) Get audio Uri path
    startRingtone(ringtone: string, vibrate_pattern: array, ios_category: string, seconds: number)Play ringtone. ringtone is _DEFAULT_ or _BUNDLE_. seconds is Android-only duration
    stopRingtone()Stop playing ringtone
    stopRingback()Stop playing ringback
    setFlashOn(enable: boolean, brightness: number)Set flashlight on/off
    getIsWiredHeadsetPluggedIn()(Async) Return wired headset plugged in state
  6. Listen to InCallManager events

    master

    You can interact with hardware changes and sensor data using DeviceEventEmitter from react-native.

    Supported Events:

    • Proximity: Emitted when the proximity sensor detects changes. Data: { 'isNear': boolean }.
    • WiredHeadset: Emitted when a wired headset is plugged/unplugged. Data: { 'isPlugged': boolean, 'hasMic': boolean, 'deviceName': string }.
    • NoisyAudio: (Android only) Emitted when audio becomes noisy. Data: null.
    • MediaButton: (Android only) Emitted when external device controller buttons are pressed. Data: { 'eventText': string, 'eventCode': number }.
    • onAudioFocusChange: (Android only) Emitted on audio focus changes. Data: { 'eventText': string, 'eventCode': number }.
    import { DeviceEventEmitter } from 'react-native';
    
    DeviceEventEmitter.addListener('Proximity', function (data) {
        // data is { isNear: boolean }
        console.log(data.isNear);
    });
  7. Manage audio routing and speakerphone

    master

    Control how audio is routed through the device hardware.

    • setSpeakerphoneOn(enable): Enables or disables the speakerphone. enable is a boolean.
    • setForceSpeakerphoneOn(_flag): Forces the speakerphone state. _flag is a boolean (internally mapped to 1 for true and -1 for false).
    • setMicrophoneMute(enable): Mutes or unmutes the microphone. enable is a boolean.
    • chooseAudioRoute(route): Asynchronously selects a specific audio route. Returns a promise.
    • getIsWiredHeadsetPluggedIn(): Asynchronously checks if a wired headset is connected. Returns { isWiredHeadsetPluggedIn: boolean }.
    import InCallManager from 'react-native-incall-manager';
    
    // Turn on speakerphone
    InCallManager.setSpeakerphoneOn(true);
    
    // Mute the microphone
    InCallManager.setMicrophoneMute(true);
    
    // Check for wired headset
    const { isWiredHeadsetPluggedIn } = await InCallManager.getIsWiredHeadsetPluggedIn();
  8. Manage screen and proximity sensors

    master

    Control device hardware related to the screen and proximity.

    • setKeepScreenOn(enable): Prevents the screen from dimming or turning off during a call. enable is a boolean.
    • turnScreenOff(): Manually turns the screen off.
    • turnScreenOn(): Manually turns the screen on.
    • startProximitySensor(): Enables the proximity sensor (useful for turning off the screen when the phone is held to the ear).
    • stopProximitySensor(): Disables the proximity sensor.
    • pokeScreen(_timeout): (Android only) Wakes up the screen. _timeout is a number in milliseconds. Defaults to 3000 ms.
    import InCallManager from 'react-native-incall-manager';
    
    // Keep screen on during call
    InCallManager.setKeepScreenOn(true);
    
    // Use proximity sensor
    InCallManager.startProximitySensor();
  9. Manage audio focus (Android only)

    master

    Request or release audio focus to manage how your app interacts with other audio-playing applications. This is only supported on Android.

    • requestAudioFocus(): Asynchronously requests audio focus. Returns a promise.
    • abandonAudioFocus(): Asynchronously releases audio focus. Returns a promise.
    import InCallManager from 'react-native-incall-manager';
    
    // Request focus on Android
    await InCallManager.requestAudioFocus();
    
    // Release focus on Android
    await InCallManager.abandonAudioFocus();
  10. Control ringtones and vibration

    master

    Manage audible and haptic feedback during calls.

    startRingtone(ringtone, vibrate_pattern, ios_category, seconds)

    • ringtone (string): The URI of the ringtone. Defaults to "_DEFAULT_".
    • vibrate_pattern (Array): An array defining the vibration pattern. If provided, vibration is enabled.
    • ios_category (string): For iOS, set to 'playback' or 'default'. Defaults to 'default'.
    • seconds (number): For Android, the duration of the ringtone. Defaults to -1 (looping).

    stopRingtone()

    • Stops the ringtone and cancels any active vibration.

    startRingback(ringback)

    • Plays a ringback tone. ringback is a string URI. Defaults to "_DTMF_" if not provided.

    stopRingback()

    • Stops the ringback tone.
    import InCallManager from 'react-native-incall-manager';
    
    // Start a ringtone with a vibration pattern
    InCallManager.startRingtone(
      'my_ringtone.mp3',
      [0, 500, 200, 500], // pattern
      'playback',
      10 // seconds
    );
    
    // Stop everything
    InCallManager.stopRingtone();
  11. Start and stop an in-call session

    master

    Use start(setup) to begin managing the call state and stop(setup) to end it.

    start(setup)

    • setup.auto (boolean): If false, automatic behavior is disabled. Defaults to true.
    • setup.media (string): Set to 'video' or 'audio'. Defaults to 'audio'.
    • setup.ringback (string): An optional URI for ringback tones.

    stop(setup)

    • setup.busytone (string): An optional URI for the busy tone played when the call ends.
    import InCallManager from 'react-native-incall-manager';
    
    // Start a video call session
    InCallManager.start({
      media: 'video',
      auto: true,
      ringback: 'path/to/ringback.mp3'
    });
    
    // Stop the session with a busy tone
    InCallManager.stop({
      busytone: 'path/to/busytone.mp3'
    });