Files
tianrunCRM/Assets/trCRM/Plugins/Android/java/com/coolape/tianrun/U3dPlugin.java
2020-08-26 19:56:45 +08:00

237 lines
5.5 KiB
Java

package com.coolape.tianrun;
import java.io.File;
import java.util.concurrent.ExecutionException;
import org.json.JSONException;
import org.json.JSONObject;
import android.media.MediaRecorder;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import com.czt.mp3recorder.MP3Recorder;
import com.newland.PhoneUtils;
import com.unity3d.player.UnityPlayer;
public class U3dPlugin {
static String TAG = "U3d";
public CLTeleInterface teleInterface;
public static String u3dListener = "";
public static String recordFileName = "";
public static Boolean isNeedRecordOutCall = false;
public static Boolean isConnectedRecordOutCall = false;
public static Boolean isWaiting4IncomeCall = false;
public static CLOutgoingCallListener outgoingCallRecver = new CLOutgoingCallListener();
public static String onEndIncomeCallOrgs = "";
public static String onEndCallOrgs = "";
public static U3dPlugin self;
// static MediaRecorder mediaRecorder;
private static MP3Recorder mRecorder;
private static File file;
public static MyMediaPlayer mediaPlayer;
public static int DefaultAudioSource = -1;
public void init(String _u3dListener) {
self = this;
u3dListener = _u3dListener;
mediaPlayer = new MyMediaPlayer();
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
teleInterface = new CLTeleInterface(UnityPlayer.currentActivity);
teleInterface.teleListen();
}
});
}
public void onDestroy() {
if (mRecorder != null) {
mRecorder.stop();
mRecorder = null;
}
file = null;
}
public void waitingIncomeCall(String _onEndIncomeCallOrgs) {
onEndIncomeCallOrgs = _onEndIncomeCallOrgs;
// isWaiting4IncomeCall = true;
}
public void begainCall(String _recordFileName, String _onEndCallOrgs) {
onEndCallOrgs = _onEndCallOrgs;
recordFileName = _recordFileName;
isNeedRecordOutCall = true;
}
public static void onBegainOutgoingCall() {
if (isNeedRecordOutCall) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
recordCall(recordFileName);
}
});
}
}
public static void onEndincomeCall(String incomingPhoneNo) {
// if (isWaiting4IncomeCall) {
// isWaiting4IncomeCall = false;
UnitySendMessage("onEndincomeCall", incomingPhoneNo, onEndIncomeCallOrgs);
// }
}
public static void onEndgoingCall() {
if (isNeedRecordOutCall) {
endRecordCall();
UnitySendMessage("onEndOutGoingCall", "0", onEndCallOrgs);
recordFileName = "";
isNeedRecordOutCall = false;
isConnectedRecordOutCall = false;
}
}
public static void recordCall(String fileName) {
if (fileName == null || fileName.isEmpty())
return;
try {
file = new File(fileName);
if (!file.exists()) {
file.createNewFile();
}
if (mRecorder == null) {
mRecorder = new MP3Recorder(file);
}
mRecorder.setRecordFile(file);
// int sdkVer = PhoneUtils.getSDKVersionNumber();
if (DefaultAudioSource < 0) {
// if (sdkVer >= 23) {
// DefaultAudioSource = MediaRecorder.AudioSource.MIC;
// } else {
DefaultAudioSource = MediaRecorder.AudioSource.VOICE_CALL;
// }
}
// 获得声音数据源
mRecorder.setAudioSource(DefaultAudioSource);
mRecorder.start();
Log.d(TAG, "开始录音!");
} catch (Exception e) {
e.printStackTrace();
try {
endRecordCall();
if (mRecorder == null) {
mRecorder = new MP3Recorder(file);
}
file = new File(fileName);
if (!file.exists()) {
file.createNewFile();
}
if (mRecorder == null) {
mRecorder = new MP3Recorder(file);
}
mRecorder.setRecordFile(file);
DefaultAudioSource = MediaRecorder.AudioSource.MIC;
// int sdkVer = PhoneUtils.getSDKVersionNumber();
mRecorder.setAudioSource(DefaultAudioSource);
mRecorder.start();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
public static void endRecordCall() {
// if (mediaRecorder != null) {
// mediaRecorder.stop();
// mediaRecorder.release();
// mediaRecorder = null;
// }
try {
if (mRecorder != null) {
mRecorder.stop();
// mRecorder = null;
file = null;
Log.d(TAG, "结束录音!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
static String audioSource;
static String orgs;
public void prepareMedia(String _audioSource, String _orgs) {
audioSource = _audioSource;
orgs = _orgs;
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
mediaPlayer.prepare(UnityPlayer.currentActivity, audioSource,
orgs);
}
});
}
public void mediaPlay() {
mediaPlayer.play();
}
public void mediaStop() {
mediaPlayer.stop();
}
public void mediaPause() {
mediaPlayer.pause();
}
static int seekPosition = 0;
public void mediaSeek(int progress) {
seekPosition = progress;
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
mediaPlayer.seek(seekPosition);
}
});
}
public void mediaDestroy() {
mediaPlayer.destroy();
}
public int getMediaProgress() {
return mediaPlayer.getProgress();
}
public static void UnitySendMessage(String CallbackFunc, String retCode,
String orgs) {
if (u3dListener.isEmpty()) {
return;
}
try {
JSONObject jsonObj = new JSONObject();
jsonObj.put("code", retCode);
jsonObj.put("orgs", orgs);
UnityPlayer.UnitySendMessage(u3dListener, CallbackFunc,
jsonObj.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
}