Skip to content

Commit 50949c5

Browse files
committed
add getAudioUriJS() function and can be accessed from JS side
1 parent 7264689 commit 50949c5

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

android/src/main/java/com/zxcpoiu/incallmanager/InCallManagerModule.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,27 @@ public void onCompletion(MediaPlayer mp) {
11781178

11791179
}
11801180

1181+
@ReactMethod
1182+
public void getAudioUriJS(String audioType, String fileType, Promise promise) {
1183+
Uri result = null;
1184+
if (audioType.equals("ringback")) {
1185+
result = getRingbackUri(fileType);
1186+
} else if (audioType.equals("busytone")) {
1187+
result = getBusytoneUri(fileType);
1188+
} else if (audioType.equals("ringtone")) {
1189+
result = getRingtoneUri(fileType);
1190+
}
1191+
try {
1192+
if (result != null) {
1193+
promise.resolve(result.toString());
1194+
} else {
1195+
promise.reject("failed");
1196+
}
1197+
} catch (Exception e) {
1198+
promise.reject("failed");
1199+
}
1200+
}
1201+
11811202
private Uri getRingtoneUri(final String _type) {
11821203
final String fileBundle = "incallmanager_ringtone";
11831204
final String fileBundleExt = "mp3";

index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ class InCallManager {
1010
this.vibrate = false;
1111
this.recordPermission = 'unknow';
1212
this.caeraPermission = 'unknow';
13+
this.audioUriMap = {
14+
ringtone: { _BUNDLE_: null, _DEFAULT_: null},
15+
ringback: { _BUNDLE_: null, _DEFAULT_: null},
16+
busytone: { _BUNDLE_: null, _DEFAULT_: null},
17+
};
1318
this.checkRecordPermission = this.checkRecordPermission.bind(this);
1419
this.requestRecordPermission = this.requestRecordPermission.bind(this);
1520
this.checkCameraPermission = this.checkCameraPermission.bind(this);
@@ -128,6 +133,27 @@ class InCallManager {
128133
console.log("ios doesn't support pokeScreen()");
129134
}
130135
}
136+
137+
async getAudioUri(audioType, fileType) {
138+
if (typeof this.audioUriMap[audioType] === "undefined") {
139+
return null;
140+
}
141+
if (this.audioUriMap[audioType][fileType]) {
142+
return this.audioUriMap[audioType][fileType];
143+
} else {
144+
try {
145+
let result = await _InCallManager.getAudioUriJS(audioType, fileType);
146+
if (typeof result === 'string' && result.length > 0) {
147+
this.audioUriMap[audioType][fileType] = result;
148+
return result
149+
} else {
150+
return null;
151+
}
152+
} catch (err) {
153+
return null;
154+
}
155+
}
156+
}
131157
}
132158

133159
export default new InCallManager();

ios/RNInCallManager/RNInCallManager.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ class RNInCallManager: NSObject, AVAudioPlayerDelegate {
650650
// --- 1. if we use Playback, it can supports background playing (starting from foreground), but it would not obey Ring/Silent switch.
651651
// --- make sure you have enabled 'audio' tag ( or 'voip' tag ) at XCode -> Capabilities -> BackgroundMode
652652
// --- 2. if we use SoloAmbient, it would obey Ring/Silent switch in the foreground, but does not support background playing,
653-
// --- thus, then you should play ringtone again via local notification after back to home during a ring session.
653+
// --- thus, then you should play ringtone again via local notification after back to home during a ring session.
654654

655655
// we prefer 2. by default, since most of users doesn't want to interrupted by a ringtone if Silent mode is on.
656656

@@ -678,6 +678,24 @@ class RNInCallManager: NSObject, AVAudioPlayerDelegate {
678678
}
679679
}
680680

681+
@objc func getAudioUriJS(audioType: String, fileType: String, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) {
682+
var _result: NSURL? = nil
683+
if audioType == "ringback" {
684+
_result = getRingbackUri(fileType)
685+
} else if audioType == "busytone" {
686+
_result = getBusytoneUri(fileType)
687+
} else if audioType == "ringtone" {
688+
_result = getRingtoneUri(fileType)
689+
}
690+
if let result: NSURL? = _result {
691+
if let urlString = result?.absoluteString {
692+
resolve(urlString)
693+
return
694+
}
695+
}
696+
reject("error_code", "getAudioUriJS() failed", NSError(domain:"getAudioUriJS", code: 0, userInfo: nil))
697+
}
698+
681699
func getRingbackUri(_type: String) -> NSURL? {
682700
let fileBundle: String = "incallmanager_ringback"
683701
let fileBundleExt: String = "mp3"

ios/RNInCallManager/RNInCallManagerBridge.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ @interface RCT_EXTERN_REMAP_MODULE(InCallManager, RNInCallManager, NSObject)
2424
RCT_EXTERN_METHOD(requestRecordPermission:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
2525
RCT_EXTERN_METHOD(checkCameraPermission:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
2626
RCT_EXTERN_METHOD(requestCameraPermission:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
27+
RCT_EXTERN_METHOD(getAudioUriJS:(NSString *)audioType fileType:(NSString *)fileType resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
2728

2829
@end

0 commit comments

Comments
 (0)