-
Notifications
You must be signed in to change notification settings - Fork 7
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
1212895
commit 57c3199
Showing
26 changed files
with
842 additions
and
77 deletions.
There are no files selected for viewing
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
136 changes: 136 additions & 0 deletions
136
android/src/main/java/com/voximplant/flutter_voximplant/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,136 @@ | ||
package com.voximplant.flutter_voximplant; | ||
|
||
import android.content.Context; | ||
import android.os.Handler; | ||
import android.os.Looper; | ||
|
||
import com.voximplant.sdk.Voximplant; | ||
import com.voximplant.sdk.hardware.IAudioFile; | ||
import com.voximplant.sdk.hardware.IAudioFileListener; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
import io.flutter.plugin.common.BinaryMessenger; | ||
import io.flutter.plugin.common.MethodCall; | ||
import io.flutter.plugin.common.MethodChannel; | ||
|
||
public class AudioFileManager { | ||
private final Map<String, AudioFileModule> mAudioFileModules; | ||
private Handler mHandler = new Handler(Looper.getMainLooper()); | ||
private final BinaryMessenger mMessenger; | ||
private final Context mAppContext; | ||
|
||
AudioFileManager(BinaryMessenger messenger, Context context) { | ||
this.mMessenger = messenger; | ||
this.mAudioFileModules = new HashMap<>(); | ||
this.mAppContext = context; | ||
} | ||
|
||
void handleMethodCall(MethodCall call, MethodChannel.Result result) { | ||
if (call.arguments == null) { | ||
mHandler.post(() -> result.error(VoximplantErrors.ERROR_INVALID_ARGUMENTS, call.method + ": Invalid arguments", null)); | ||
return; | ||
} | ||
switch (call.method) { | ||
case "initWithFile": | ||
initWithFile(call, result); | ||
break; | ||
case "loadFile": | ||
loadFile(call, result); | ||
break; | ||
case "releaseResources": | ||
releaseResources(call, result); | ||
break; | ||
default: | ||
handleInModule(call, result); | ||
break; | ||
} | ||
} | ||
|
||
void initWithFile(MethodCall call, MethodChannel.Result result) { | ||
String fileName = call.argument("name"); | ||
if (fileName == null) { | ||
mHandler.post(() -> result.error(VoximplantErrors.ERROR_INVALID_ARGUMENTS, call.method + ": name is null", null)); | ||
return; | ||
} | ||
String fileUsage = call.argument("usage"); | ||
if (fileUsage == null) { | ||
mHandler.post(() -> result.error(VoximplantErrors.ERROR_INVALID_ARGUMENTS, call.method + ": usage is null", null)); | ||
return; | ||
} | ||
int rawId = mAppContext.getResources().getIdentifier(fileName, "raw", mAppContext.getPackageName()); | ||
IAudioFile audioFile = Voximplant.createAudioFile(mAppContext, rawId, Utils.convertStringToAudioFileUsage(fileUsage)); | ||
if (audioFile == null) { | ||
mHandler.post(() -> result.error(VoximplantErrors.ERROR_INVALID_ARGUMENTS, call.method + ": failed to locate audio file", null)); | ||
return; | ||
} | ||
String fileId = UUID.randomUUID().toString(); | ||
AudioFileModule module = new AudioFileModule(mMessenger, audioFile, fileId, null); | ||
mAudioFileModules.put(fileId, module); | ||
mHandler.post(() -> result.success(fileId)); | ||
} | ||
|
||
void loadFile(MethodCall call, MethodChannel.Result result) { | ||
String fileUrl = call.argument("url"); | ||
if (fileUrl == null) { | ||
mHandler.post(() -> result.error(VoximplantErrors.ERROR_INVALID_ARGUMENTS, call.method + ": url is null", null)); | ||
return; | ||
} | ||
String fileUsage = call.argument("usage"); | ||
if (fileUsage == null) { | ||
mHandler.post(() -> result.error(VoximplantErrors.ERROR_INVALID_ARGUMENTS, call.method + ": usage is null", null)); | ||
return; | ||
} | ||
IAudioFile audioFile = Voximplant.createAudioFile(fileUrl, Utils.convertStringToAudioFileUsage(fileUsage)); | ||
if (audioFile == null) { | ||
mHandler.post(() -> result.error(VoximplantErrors.ERROR_INVALID_ARGUMENTS, call.method + ": failed to load audio file", null)); | ||
return; | ||
} | ||
String fileId = UUID.randomUUID().toString(); | ||
AudioFileModule module = new AudioFileModule(mMessenger, audioFile, fileId, result); | ||
mAudioFileModules.put(fileId, module); | ||
} | ||
|
||
void releaseResources(MethodCall call, MethodChannel.Result result) { | ||
String fileId = call.argument("fileId"); | ||
if (fileId == null) { | ||
mHandler.post(() -> result.error(VoximplantErrors.ERROR_INVALID_ARGUMENTS, call.method + ": fileId is null", null)); | ||
return; | ||
} | ||
if (!mAudioFileModules.containsKey(fileId)) { | ||
mHandler.post(() -> result.error(VoximplantErrors.ERROR_INVALID_ARGUMENTS, call.method + ": could'nt find audioFile", null)); | ||
return; | ||
} | ||
AudioFileModule module = mAudioFileModules.get(fileId); | ||
if (module == null) { | ||
mHandler.post(() -> result.error(VoximplantErrors.ERROR_INVALID_ARGUMENTS, call.method + ": could'nt find audioFile", null)); | ||
return; | ||
} | ||
if (module.mAudioFile != null) { | ||
module.mAudioFile.setAudioFileListener(null); | ||
module.mAudioFile.release(); | ||
} | ||
mAudioFileModules.remove(fileId); | ||
mHandler.post(() -> result.success(null)); | ||
} | ||
|
||
void handleInModule(MethodCall call, MethodChannel.Result result) { | ||
String fileId = call.argument("fileId"); | ||
if (fileId == null) { | ||
mHandler.post(() -> result.error(VoximplantErrors.ERROR_INVALID_ARGUMENTS, call.method + ": fileId is null", null)); | ||
return; | ||
} | ||
if (!mAudioFileModules.containsKey(fileId)) { | ||
mHandler.post(() -> result.error(VoximplantErrors.ERROR_INVALID_ARGUMENTS, call.method + ": could'nt find audioFile", null)); | ||
return; | ||
} | ||
AudioFileModule module = mAudioFileModules.get(fileId); | ||
if (module == null) { | ||
mHandler.post(() -> result.error(VoximplantErrors.ERROR_INVALID_ARGUMENTS, call.method + ": could'nt find audioFile", null)); | ||
return; | ||
} | ||
module.handleMethodCall(call, result); | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
android/src/main/java/com/voximplant/flutter_voximplant/AudioFileModule.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,118 @@ | ||
package com.voximplant.flutter_voximplant; | ||
|
||
import android.os.Handler; | ||
import android.os.Looper; | ||
|
||
import com.voximplant.sdk.hardware.IAudioFile; | ||
import com.voximplant.sdk.hardware.IAudioFileListener; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import io.flutter.plugin.common.BinaryMessenger; | ||
import io.flutter.plugin.common.EventChannel; | ||
import io.flutter.plugin.common.MethodCall; | ||
import io.flutter.plugin.common.MethodChannel; | ||
|
||
public class AudioFileModule implements EventChannel.StreamHandler, IAudioFileListener { | ||
final IAudioFile mAudioFile; | ||
private final String mFileId; | ||
private EventChannel mEventChannel; | ||
private EventChannel.EventSink mEventSink; | ||
private Handler mHandler = new Handler(Looper.getMainLooper()); | ||
private MethodChannel.Result mLoadFileCompletion; | ||
private MethodChannel.Result mPlayCompletion; | ||
private MethodChannel.Result mStopCompletion; | ||
|
||
AudioFileModule(BinaryMessenger messenger, IAudioFile file, String fileId, MethodChannel.Result loadFileCompletion) { | ||
mLoadFileCompletion = loadFileCompletion; | ||
mEventChannel = new EventChannel(messenger, "plugins.voximplant.com/audio_file_events_" + fileId); | ||
mEventChannel.setStreamHandler(this); | ||
mFileId = fileId; | ||
mAudioFile = file; | ||
mAudioFile.setAudioFileListener(this); | ||
} | ||
|
||
void handleMethodCall(MethodCall call, MethodChannel.Result result) { | ||
switch (call.method) { | ||
case "play": | ||
play(call, result); | ||
break; | ||
case "stop": | ||
stop(result); | ||
break; | ||
default: | ||
result.notImplemented(); | ||
break; | ||
} | ||
} | ||
|
||
void play(MethodCall call, MethodChannel.Result result) { | ||
Object looped = call.argument("looped"); | ||
if (looped instanceof Boolean) { | ||
if (mAudioFile != null) { | ||
mPlayCompletion = result; | ||
mAudioFile.play((Boolean)looped); | ||
} | ||
} else { | ||
mHandler.post(() -> result.error(VoximplantErrors.ERROR_INVALID_ARGUMENTS, call.method + ": looped is null", null)); | ||
} | ||
} | ||
|
||
void stop(MethodChannel.Result result) { | ||
if (mAudioFile != null) { | ||
mStopCompletion = result; | ||
mAudioFile.stop(false); | ||
} | ||
} | ||
|
||
@Override | ||
public void onStart(IAudioFile audioFile) { | ||
if (mPlayCompletion != null) { | ||
mHandler.post(() -> { | ||
mPlayCompletion.success(null); | ||
mPlayCompletion = null; | ||
}); | ||
} | ||
} | ||
|
||
@Override | ||
public void onStop(IAudioFile audioFile) { | ||
if (mStopCompletion != null) { | ||
mHandler.post(() -> { | ||
mStopCompletion.success(null); | ||
mStopCompletion = null; | ||
}); | ||
} else if (mFileId != null) { | ||
Map<String, Object> params = new HashMap<>(); | ||
params.put("name", "didStopPlaying"); | ||
sendEvent(params); | ||
} | ||
} | ||
|
||
@Override | ||
public void onPrepared(IAudioFile audioFile) { | ||
if (mLoadFileCompletion != null) { | ||
mHandler.post(() -> { | ||
mLoadFileCompletion.success(mFileId); | ||
mLoadFileCompletion = null; | ||
}); | ||
} | ||
} | ||
|
||
@Override | ||
public void onListen(Object arguments, EventChannel.EventSink events) { | ||
mEventSink = events; | ||
} | ||
|
||
@Override | ||
public void onCancel(Object arguments) { | ||
mEventSink = null; | ||
} | ||
|
||
private void sendEvent(Map<String, Object> event) { | ||
if (mEventSink != null) { | ||
mHandler.post(() -> mEventSink.success(event)); | ||
} | ||
} | ||
} |
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.