|
| 1 | +# react-native-incall-manager |
| 2 | +Handling media-routes/sensors/events during a audio/video chat on React Native |
| 3 | + |
| 4 | +## Purpose: |
| 5 | +The purpose of this module is to handle actions/events during a phone call (audio/video) on `react-native`, ex: |
| 6 | +* manage devices events like wired-headset plugged, proximity sensors and expose to javascript. |
| 7 | +* automatically route audio to proper device based on events and platform API. |
| 8 | +* ( not implemented yet ) toggle flash light on/off, force microphone mute |
| 9 | + |
| 10 | +This module is desinged to work with [react-native-webrtc](https://github.com/oney/react-native-webrtc) |
| 11 | +you can find demo here: https://github.com/oney/RCTWebRTCDemo |
| 12 | + |
| 13 | +## Installation: |
| 14 | + |
| 15 | +**from npm package**: `npm install react-native-incall-manager` |
| 16 | +**from git package**: `npm install git://github.com/zxcpoiu/react-native-incall-manager.git` |
| 17 | + |
| 18 | +=================================================== |
| 19 | +####android: |
| 20 | + |
| 21 | +After install, you can use `rnpm` (`npm install rnpm -g`) to link android. |
| 22 | +use `rnpm link react-native-incall-manager` to link or manually if you like. |
| 23 | + |
| 24 | +=================================================== |
| 25 | + |
| 26 | +####ios: |
| 27 | + |
| 28 | +since ios part written in swift and it doesn't support static library yet. |
| 29 | +before that, you should add this project manually: |
| 30 | + |
| 31 | +- **Add files in to your project:** |
| 32 | + |
| 33 | + 1. Open your project in xcode |
| 34 | + 2. find your_project directory under your project's xcodeproject root. ( it's a sub-directoory, not root xcodeproject itself ) |
| 35 | + 3. you can do either: |
| 36 | + * directly drag your node_modules/react-native-incall-manager/ios/RNInCallManager/ into it. |
| 37 | + * right click on your_project directory, `add files` to your project and add `node_modules/react-native-incall-manager/ios/RNInCallManager/` |
| 38 | + 4. on the pou-up window, uncheck `Copy items if needed` and select `Added folders: Create groups` then add it. you will see a new directory named `RNInCallmanager under your_project` directory. |
| 39 | + |
| 40 | +- **Setup Objective-C Bridging Header:** |
| 41 | + 1. click your `project's xcodeproject root`, go to `build setting` and search `Objective-C Bridging Header` |
| 42 | + 2. set you header location, the default path is: `ReactNativeProjectRoot/ios/`, in this case, you should set `../node_modules/react-native-incall-manager/ios/RNInCallManager/RNInCallManager-Bridging-Header.h` |
| 43 | + |
| 44 | +## Usage: |
| 45 | + |
| 46 | +This module implement a basic handle logic automatically, just: |
| 47 | + |
| 48 | +```javascript |
| 49 | +import InCallManager from 'react-native-incall-manager'; |
| 50 | + |
| 51 | +// --- start manager when the chat start based on logics of your app |
| 52 | +// On Call Established: |
| 53 | +InCallManager.start({media: 'audio'}); // audio/video, default: audio |
| 54 | + |
| 55 | +// ... it will also register and emit events ... |
| 56 | + |
| 57 | +// --- On Call Hangup: |
| 58 | +InCallManager.stop(); |
| 59 | +// ... it will also remote event listeners ... |
| 60 | +``` |
| 61 | + |
| 62 | + |
| 63 | +and interact with events if you want: |
| 64 | +see API section. |
| 65 | + |
| 66 | +```javascript |
| 67 | +import { DeviceEventEmitter } from 'react-native'; |
| 68 | + |
| 69 | +DeviceEventEmitter.addListener('Proximity', function (data) { |
| 70 | + // --- do something with events |
| 71 | +}); |
| 72 | + |
| 73 | +``` |
| 74 | + |
| 75 | +## Automatic Basic Behavior: |
| 76 | + |
| 77 | +**on start:** |
| 78 | +* store current settings, set KeepScreenOn flag = true, and register some event listeners. |
| 79 | +* if media type is `audio`, route voice to earpiece, otherwise route to speaker. |
| 80 | +* when proximity detect user closed to screen, turn off screen to avoid accident touch and route voice to earpiece. |
| 81 | +* when newly external device plugged, such as wired-headset, route audio to external device. |
| 82 | + |
| 83 | +**on stop:** |
| 84 | + |
| 85 | +* set KeepScreenOn flag = false, remote event listeners, restore original user settings. |
| 86 | + |
| 87 | +## Custom Behavior: |
| 88 | + |
| 89 | +you can custom behavior use API/events exposed by this module. see `API` section. |
| 90 | + |
| 91 | +note: ios only supports `auto` currently. |
| 92 | + |
| 93 | +## API: |
| 94 | + |
| 95 | +**Methods** |
| 96 | + |
| 97 | +| Method | android | ios | description | |
| 98 | +| :--- | :---: | :---: | :--- | |
| 99 | +| start(`{media: ?string, auto: ?boolean}`) | :smile: | :smile: | start incall manager.</br>default: `{media:'audio', auto: true}` | |
| 100 | +| stop() | :smile: | :smile: | stop incall manager | |
| 101 | +| turnScreenOn() | :smile: | :rage: | force turn screen on | |
| 102 | +| turnScreenOff() | :smile: | :rage: | force turn screen off | |
| 103 | +| setKeepScreenOn(`enable: ?boolean`) | :smile: | :smile: | set KeepScreenOn flag = true or false</br>default: false | |
| 104 | +| setSpeakerphoneOn(`enable: ?boolean`) | :smile: | :rage: | toggle speaker ON/OFF once. but not force</br>default: false | |
| 105 | +| setForceSpeakerphoneOn(`enable: ?boolean`) | :smile: | :smile: | if set to true, will ignore all logic and force audio route to speaker</br>default: false | |
| 106 | +| setMicrophoneMute(`enable: ?boolean`) | :smile: | :rage: | mute/unmute micophone</br>default: false | |
| 107 | + |
| 108 | +**Events** |
| 109 | + |
| 110 | +| Event | android | ios | description | |
| 111 | +| :--- | :---: | :---: | :--- | |
| 112 | +| 'Proximity' | :smile: | :smile: | proximity sensor detected changes.<br>data: `{'isNear': boolean}` | |
| 113 | +| 'WiredHeadset'| :smile: | :rage: | fire when wired headset plug/unplug<br>data: `{'isPlugged': boolean, 'hasMic': boolean, 'deviceName': string }` | |
| 114 | +| 'NoisyAudio' | :smile: | :rage: | see [andriod doc](http://developer.android.com/reference/android/media/AudioManager.html#ACTION_AUDIO_BECOMING_NOISY).<br>data: `null` | |
| 115 | +| 'MediaButton' | :smile: | :rage: | when external device controler pressed button. see [android doc](http://developer.android.com/reference/android/content/Intent.html#ACTION_MEDIA_BUTTON) <br>data: `{'eventText': string, 'eventCode': number }` | |
| 116 | +| 'onAudioFocusChange' | :smile: | :rage: | see [andriod doc](http://developer.android.com/reference/android/media/AudioManager.OnAudioFocusChangeListener.html#onAudioFocusChange(int)) <br>data: `{'eventText': string, 'eventCode': number }` | |
| 117 | + |
| 118 | +**NOTE: platform OS always has the final decision, so some toggle api may not work in some case |
| 119 | +be care when customize your own behavior** |
| 120 | + |
| 121 | +## LICENSE: |
| 122 | + |
| 123 | +**[ICS License](https://opensource.org/licenses/ISC)** ( functionality equivalent to **MIT License** ) |
| 124 | + |
| 125 | +## Contributing: |
| 126 | + |
| 127 | +I'm not expert neither on ios nor android, any suggestions, pull request, corrections are really appreciated and welcome. |
0 commit comments