-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4955dfd
commit 238a50b
Showing
18 changed files
with
767 additions
and
4 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
android/src/main/java/com/voximplant/reactnative/AudioFileManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright (c) 2011-2020, Zingaya, Inc. All rights reserved. | ||
*/ | ||
|
||
package com.voximplant.reactnative; | ||
|
||
import com.voximplant.sdk.hardware.IAudioFile; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class AudioFileManager { | ||
private static AudioFileManager mInstance = null; | ||
private HashMap<String, IAudioFile> mAudioFiles = new HashMap<>(); | ||
|
||
static synchronized AudioFileManager getInstance() { | ||
if (mInstance == null) { | ||
mInstance = new AudioFileManager(); | ||
} | ||
return mInstance; | ||
} | ||
|
||
void addAudioFile(String fileId, IAudioFile audioFile) { | ||
if (fileId != null && audioFile != null) { | ||
mAudioFiles.put(fileId, audioFile); | ||
} | ||
} | ||
|
||
IAudioFile getAudioFile(String fileId) { | ||
if (fileId == null) { | ||
return null; | ||
} | ||
return mAudioFiles.get(fileId); | ||
} | ||
|
||
void removeAudioFile(String fileId) { | ||
if (fileId != null) { | ||
mAudioFiles.remove(fileId); | ||
} | ||
} | ||
|
||
String getFileIdForAudioFile(IAudioFile audioFile) { | ||
for(Map.Entry<String, IAudioFile> entry : mAudioFiles.entrySet()) { | ||
if (entry.getValue().equals(audioFile)) { | ||
return entry.getKey(); | ||
} | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
android/src/main/java/com/voximplant/reactnative/VIAudioFileModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* Copyright (c) 2011-2020, Zingaya, Inc. All rights reserved. | ||
*/ | ||
|
||
package com.voximplant.reactnative; | ||
|
||
import com.facebook.react.bridge.Arguments; | ||
import com.facebook.react.bridge.Callback; | ||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReactContextBaseJavaModule; | ||
import com.facebook.react.bridge.ReactMethod; | ||
import com.facebook.react.bridge.ReadableMap; | ||
import com.facebook.react.bridge.WritableMap; | ||
import com.facebook.react.modules.core.DeviceEventManagerModule; | ||
import com.voximplant.sdk.Voximplant; | ||
import com.voximplant.sdk.hardware.IAudioFile; | ||
import com.voximplant.sdk.hardware.IAudioFileListener; | ||
|
||
import java.util.UUID; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import static com.voximplant.reactnative.Constants.EVENT_AUDIO_FILE_STARTED; | ||
import static com.voximplant.reactnative.Constants.EVENT_AUDIO_FILE_STOPPED; | ||
import static com.voximplant.reactnative.Constants.EVENT_NAME_AUDIO_FILE_STARTED; | ||
import static com.voximplant.reactnative.Constants.EVENT_NAME_AUDIO_FILE_STOPPED; | ||
import static com.voximplant.reactnative.Constants.EVENT_PARAM_AUDIO_FILE_ID; | ||
import static com.voximplant.reactnative.Constants.EVENT_PARAM_NAME; | ||
import static com.voximplant.reactnative.Constants.EVENT_PARAM_RESULT; | ||
|
||
public class VIAudioFileModule extends ReactContextBaseJavaModule implements IAudioFileListener { | ||
private ReactApplicationContext mReactContext; | ||
private Callback mCallback; | ||
|
||
public VIAudioFileModule(ReactApplicationContext reactContext) { | ||
super(reactContext); | ||
mReactContext = reactContext; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "VIAudioFileModule"; | ||
} | ||
|
||
@ReactMethod | ||
public void initWithFile(ReadableMap params, Callback callback) { | ||
if (params == null) { | ||
callback.invoke(null, "Invalid arguments"); | ||
return; | ||
} | ||
String name = params.getString("name"); | ||
String usage = params.getString("usage"); | ||
int rawId = mReactContext.getResources().getIdentifier(name, "raw", mReactContext.getPackageName()); | ||
IAudioFile audioFile = Voximplant.createAudioFile(mReactContext, rawId, Utils.convertStringToAudioFileUsage(usage)); | ||
if (audioFile == null) { | ||
callback.invoke(null, "Internal error"); | ||
return; | ||
} | ||
audioFile.setAudioFileListener(this); | ||
String fileId = UUID.randomUUID().toString(); | ||
AudioFileManager.getInstance().addAudioFile(fileId, audioFile); | ||
callback.invoke(fileId, null); | ||
} | ||
|
||
@ReactMethod | ||
public void loadFile(ReadableMap params, Callback callback) { | ||
if (params == null) { | ||
callback.invoke(null, "Invalid arguments"); | ||
return; | ||
} | ||
String url = params.getString("url"); | ||
String usage = params.getString("usage"); | ||
IAudioFile audioFile = Voximplant.createAudioFile(url, Utils.convertStringToAudioFileUsage(usage)); | ||
if (audioFile == null) { | ||
callback.invoke(null, "Internal error"); | ||
return; | ||
} | ||
audioFile.setAudioFileListener(this); | ||
String fileId = UUID.randomUUID().toString(); | ||
AudioFileManager.getInstance().addAudioFile(fileId, audioFile); | ||
mCallback = callback; | ||
} | ||
|
||
@ReactMethod | ||
public void play(String fileId, boolean loop) { | ||
IAudioFile audioFile = AudioFileManager.getInstance().getAudioFile(fileId); | ||
if (audioFile != null) { | ||
audioFile.play(loop); | ||
} | ||
} | ||
|
||
@ReactMethod | ||
public void stop(String fileId) { | ||
IAudioFile audioFile = AudioFileManager.getInstance().getAudioFile(fileId); | ||
if (audioFile != null) { | ||
audioFile.stop(false); | ||
} | ||
} | ||
|
||
@ReactMethod | ||
public void releaseResources(String fileId) { | ||
IAudioFile audioFile = AudioFileManager.getInstance().getAudioFile(fileId); | ||
if (audioFile != null) { | ||
audioFile.release(); | ||
AudioFileManager.getInstance().removeAudioFile(fileId); | ||
} | ||
} | ||
|
||
|
||
@Override | ||
public void onStart(IAudioFile audioFile) { | ||
String fileId = AudioFileManager.getInstance().getFileIdForAudioFile(audioFile); | ||
if (fileId != null) { | ||
WritableMap params = Arguments.createMap(); | ||
params.putString(EVENT_PARAM_NAME, EVENT_NAME_AUDIO_FILE_STARTED); | ||
params.putString(EVENT_PARAM_AUDIO_FILE_ID, fileId); | ||
params.putBoolean(EVENT_PARAM_RESULT, true); | ||
sendEvent(EVENT_AUDIO_FILE_STARTED, params); | ||
} | ||
} | ||
|
||
@Override | ||
public void onStop(IAudioFile audioFile) { | ||
String fileId = AudioFileManager.getInstance().getFileIdForAudioFile(audioFile); | ||
if (fileId != null) { | ||
WritableMap params = Arguments.createMap(); | ||
params.putString(EVENT_PARAM_NAME, EVENT_NAME_AUDIO_FILE_STOPPED); | ||
params.putString(EVENT_PARAM_AUDIO_FILE_ID, fileId); | ||
params.putBoolean(EVENT_PARAM_RESULT, true); | ||
sendEvent(EVENT_AUDIO_FILE_STOPPED, params); | ||
} | ||
} | ||
|
||
@Override | ||
public void onPrepared(IAudioFile audioFile) { | ||
if (mCallback != null) { | ||
String fileId = AudioFileManager.getInstance().getFileIdForAudioFile(audioFile); | ||
mCallback.invoke(fileId, null); | ||
mCallback = null; | ||
} | ||
} | ||
|
||
private void sendEvent(String eventName, @Nullable WritableMap params) { | ||
mReactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.