This commit is contained in:
2020-08-26 19:56:45 +08:00
parent 7ff6b2ec35
commit 3ece010fc1
208 changed files with 14753 additions and 637 deletions

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 11c893940831146979c7e7598efdb32f
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 8bf198c8bbb1346a89fe2d9429fdef63
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 3d4c11bdd31bb45a2b28d13a49d734a4
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,37 @@
package com.coolape.tianrun;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
public class CLOutgoingCallListener extends BroadcastReceiver {
String TAG = "unity";
static Boolean isOutgoingCall = false;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
Log.d(TAG, "开始拨号,但并不知道是否接通电话");
if (U3dPlugin.isNeedRecordOutCall) {
isOutgoingCall = true;
U3dPlugin.onBegainOutgoingCall();
}
} else if (intent.getAction().equals(
TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);// 注册监听器
if (telephonyManager.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
if (U3dPlugin.isNeedRecordOutCall && isOutgoingCall) {
U3dPlugin.onEndgoingCall();
isOutgoingCall = false;
}
Log.d(TAG, "挂电话");
}
}
}
}

View File

@@ -0,0 +1,32 @@
fileFormatVersion: 2
guid: 7c47c9be3131645a7b1255d79145cd79
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Android: Android
second:
enabled: 1
settings: {}
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,149 @@
package com.coolape.tianrun;
import java.util.Calendar;
import com.android.internal.telephony.ITelephony;
import com.newland.PhoneUtils;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Message;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
/**
*
* @author JD 功能:打电话,录音,通话时间
*
*/
public class CLTeleInterface {
private String TAG = "TeleInterface";
private Context activity;
// private Handler handler;
private Calendar calendar;
private String teleStartTime;
private String teleEndTime;
private TelephonyManager telephonyManager;
public static int TELE_START_TIME = 5;
public static int TELE_END_TIME = 6;
public String getTeleStartTime() {
return teleStartTime;
}
public String getTeleEndTime() {
return teleEndTime;
}
public PhoneListener listener;
/**
* 构造函数
*
* @param activity
* @param handler
* 自定义handler接收消息 msg.what 5:电话拨通时间 6:电话挂断时间
*/
// public TeleInterface(Context activity, Handler handler) {
public CLTeleInterface(Context activity) {
this.activity = activity;
// this.handler = handler;
}
/**
* 拨打电话
*
* @param phoneNum
* 需要拨打号码
*/
public void Call(String phoneNum) {
if (phoneNum.length() != 0) {
Intent phoneIntent = new Intent("android.intent.action.CALL",
Uri.parse("tel:" + phoneNum));
activity.startActivity(phoneIntent);
} else {
Toast.makeText(activity, "不能输入为空", Toast.LENGTH_LONG).show();
}
}
/**
* 来电监听注册
*/
public void teleListen() {
telephonyManager = (TelephonyManager) activity
.getSystemService(Context.TELEPHONY_SERVICE);// 注册监听器
if(listener == null) {
listener = new PhoneListener();
}
telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);// 监听电话状态
}
/**
* 挂断电话
*
* @throws Exception
*/
public void endCall() throws Exception {
ITelephony iTelephony = PhoneUtils.getITelephony(telephonyManager);
iTelephony.endCall();// 自动挂断电话
}
private final class PhoneListener extends PhoneStateListener {
private String incomeNumber=""; // 来电号码
private boolean isComingCall = false;
// private MediaRecorder mediaRecorder;
// private File root_file, file;
@Override
public void onCallStateChanged(int state, String incomingNumber) {
try {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING: // 来电
Log.d(TAG, "来电============");
this.incomeNumber = incomingNumber;
Log.d(TAG, "incomingNumber==" + incomingNumber);
isComingCall = true;
break;
case TelephonyManager.CALL_STATE_OFFHOOK: // 接通电话
Log.d(TAG, "接通电话============");
calendar = Calendar.getInstance();
teleStartTime = calendar.getTime().toString();
Message msg_start = new Message();
msg_start.what = TELE_START_TIME;
msg_start.obj = teleStartTime;
Log.d(TAG, "StartTime=====" + teleStartTime);
if (U3dPlugin.isNeedRecordOutCall) {
// isOutgoingCall = true;
U3dPlugin.onBegainOutgoingCall();
}
break;
case TelephonyManager.CALL_STATE_IDLE: // 挂掉电话
if(isComingCall) {
U3dPlugin.onEndincomeCall(incomeNumber);
// incomeNumber = "";
isComingCall = false;
} else {
if (U3dPlugin.isNeedRecordOutCall) {
U3dPlugin.onEndgoingCall();
// isOutgoingCall = false;
}
}
Log.d(TAG, "挂掉电话===================");
break;
}
super.onCallStateChanged(state, incomingNumber);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

View File

@@ -0,0 +1,32 @@
fileFormatVersion: 2
guid: 41b4e79026479440ca9684104f56b83b
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Android: Android
second:
enabled: 1
settings: {}
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,123 @@
package com.coolape.tianrun;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.net.Uri;
public class MyMediaPlayer {
public static volatile MediaPlayer player = null;
static String onPrepareOrgs = "";
public void prepare(Context context, String audioSource, String orgs) {
onPrepareOrgs = orgs;
try {
Uri uri = Uri.parse(audioSource);
if (player == null) {
player = MediaPlayer.create(context, uri);
player.setLooping(false);
} else {
if (player.isPlaying()) {
stop();
}
player.reset();
if (audioSource.startsWith("http")) {
player.setDataSource(context, uri);
} else {
player.setDataSource(audioSource);
}
player.prepareAsync();
}
player.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer player) {
player.setLooping(false);
int len = player.getDuration();
U3dPlugin.UnitySendMessage("onMediaPrepared", len + "",
onPrepareOrgs);
}
});
player.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer player) {
pause();
U3dPlugin.UnitySendMessage("onMediaComplet", "true",
onPrepareOrgs);
}
});
player.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
U3dPlugin.UnitySendMessage("onMediaError", "false",
onPrepareOrgs);
return false;
}
});
player.setOnSeekCompleteListener(new OnSeekCompleteListener() {
@Override
public void onSeekComplete(MediaPlayer arg0) {
// TODO Auto-generated method stub
U3dPlugin.UnitySendMessage("onMediaSeek", "true",
onPrepareOrgs);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
public int getProgress() {
if (player != null) {
return player.getCurrentPosition();
} else {
return 0;
}
}
public void pause() {
if (player != null && player.isPlaying()){
player.pause();
}
}
public void play() {
if (player != null && !player.isPlaying()){
player.start();
}
}
public void stop() {
if (player != null)
return;
if (player.isPlaying()) {
player.stop();
}
try {
// mp.prepareAsync();
// player.prepare();
player.reset();
// player.seekTo(0);
} catch (Exception e) {
e.printStackTrace();
}
}
public void seek(int progress) {
if (player != null)
player.seekTo(progress);
}
public void destroy() {
if (player != null) {
player.stop();
player.release();
}
player = null;
}
}

View File

@@ -0,0 +1,32 @@
fileFormatVersion: 2
guid: 454367b24531b47b59738a9ba52727ee
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Android: Android
second:
enabled: 1
settings: {}
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,236 @@
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();
}
}
}

View File

@@ -0,0 +1,32 @@
fileFormatVersion: 2
guid: cab5480656f834957a7195cdc9f55ea4
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Android: Android
second:
enabled: 1
settings: {}
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 9faff43252f784dd0acb3a936a2db671
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: c58eb358ced9748b8913d328d4a96636
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,153 @@
package com.czt.mp3recorder;
import android.media.AudioRecord;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import com.czt.mp3recorder.util.LameUtil;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class DataEncodeThread extends HandlerThread implements AudioRecord.OnRecordPositionUpdateListener {
private StopHandler mHandler;
private static final int PROCESS_STOP = 1;
private byte[] mMp3Buffer;
private FileOutputStream mFileOutputStream;
private static class StopHandler extends Handler {
private DataEncodeThread encodeThread;
public StopHandler(Looper looper, DataEncodeThread encodeThread) {
super(looper);
this.encodeThread = encodeThread;
}
@Override
public void handleMessage(Message msg) {
if (msg.what == PROCESS_STOP) {
//处理缓冲区中的数据
while (encodeThread.processData() > 0);
// Cancel any event left in the queue
removeCallbacksAndMessages(null);
encodeThread.flushAndRelease();
getLooper().quit();
}
}
}
/**
* Constructor
* @param file file
* @param bufferSize bufferSize
* @throws FileNotFoundException file not found
*/
public DataEncodeThread(File file, int bufferSize) throws FileNotFoundException {
super("DataEncodeThread");
this.mFileOutputStream = new FileOutputStream(file);
mMp3Buffer = new byte[(int) (7200 + (bufferSize * 2 * 1.25))];
}
@Override
public synchronized void start() {
super.start();
mHandler = new StopHandler(getLooper(), this);
}
private void check() {
if (mHandler == null) {
throw new IllegalStateException();
}
}
public void sendStopMessage() {
check();
mHandler.sendEmptyMessage(PROCESS_STOP);
}
public Handler getHandler() {
check();
return mHandler;
}
@Override
public void onMarkerReached(AudioRecord recorder) {
// Do nothing
}
@Override
public void onPeriodicNotification(AudioRecord recorder) {
processData();
}
/**
* 从缓冲区中读取并处理数据使用lame编码MP3
* @return 从缓冲区中读取的数据的长度
* 缓冲区中没有数据时返回0
*/
private int processData() {
if (mTasks.size() > 0) {
Task task = mTasks.remove(0);
short[] buffer = task.getData();
int readSize = task.getReadSize();
int encodedSize = LameUtil.encode(buffer, buffer, readSize, mMp3Buffer);
if (encodedSize > 0){
try {
mFileOutputStream.write(mMp3Buffer, 0, encodedSize);
} catch (IOException e) {
e.printStackTrace();
}
}
return readSize;
}
return 0;
}
/**
* Flush all data left in lame buffer to file
*/
private void flushAndRelease() {
//将MP3结尾信息写入buffer中
final int flushResult = LameUtil.flush(mMp3Buffer);
if (flushResult > 0) {
try {
mFileOutputStream.write(mMp3Buffer, 0, flushResult);
} catch (IOException e) {
e.printStackTrace();
}finally{
if (mFileOutputStream != null) {
try {
mFileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
LameUtil.close();
}
}
}
private List<Task> mTasks = Collections.synchronizedList(new ArrayList<Task>());
public void addTask(short[] rawData, int readSize){
mTasks.add(new Task(rawData, readSize));
}
private class Task{
private short[] rawData;
private int readSize;
public Task(short[] rawData, int readSize){
this.rawData = rawData.clone();
this.readSize = readSize;
}
public short[] getData(){
return rawData;
}
public int getReadSize(){
return readSize;
}
}
}

View File

@@ -0,0 +1,32 @@
fileFormatVersion: 2
guid: 3f6f8444c067b489ab5f68e3b42557cb
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Android: Android
second:
enabled: 1
settings: {}
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,192 @@
package com.czt.mp3recorder;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.util.Log;
import com.czt.mp3recorder.util.LameUtil;
import java.io.File;
import java.io.IOException;
public class MP3Recorder {
//=======================AudioRecord Default Settings=======================
private static int DEFAULT_AUDIO_SOURCE = MediaRecorder.AudioSource.MIC;
/**
* 以下三项为默认配置参数。Google Android文档明确表明只有以下3个参数是可以在所有设备上保证支持的。
*/
private static final int DEFAULT_SAMPLING_RATE = 44100;//模拟器仅支持从麦克风输入8kHz采样率
private static final int DEFAULT_CHANNEL_CONFIG = AudioFormat.CHANNEL_IN_MONO;
/**
* 下面是对此的封装
* private static final int DEFAULT_AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
*/
private static final PCMFormat DEFAULT_AUDIO_FORMAT = PCMFormat.PCM_16BIT;
//======================Lame Default Settings=====================
private static final int DEFAULT_LAME_MP3_QUALITY = 7;
/**
* 与DEFAULT_CHANNEL_CONFIG相关因为是mono单声所以是1
*/
private static final int DEFAULT_LAME_IN_CHANNEL = 1;
/**
* Encoded bit rate. MP3 file will be encoded with bit rate 32kbps
*/
private static final int DEFAULT_LAME_MP3_BIT_RATE = 32;
//==================================================================
/**
* 自定义 每160帧作为一个周期通知一下需要进行编码
*/
private static final int FRAME_COUNT = 160;
private AudioRecord mAudioRecord = null;
private int mBufferSize;
private short[] mPCMBuffer;
private DataEncodeThread mEncodeThread;
private boolean mIsRecording = false;
private File mRecordFile;
/**
* Default constructor. Setup recorder with default sampling rate 1 channel,
* 16 bits pcm
* @param recordFile target file
*/
public MP3Recorder(File recordFile) {
mRecordFile = recordFile;
}
public void setRecordFile(File recordFile) {
mRecordFile = recordFile;
}
public void setAudioSource(int audioSource) {
DEFAULT_AUDIO_SOURCE = audioSource;
}
/**
* Start recording. Create an encoding thread. Start record from this
* thread.
*
* @throws IOException initAudioRecorder throws
*/
public void start() throws IOException {
if (mIsRecording) {
return;
}
mIsRecording = true; // 提早防止init或startRecording被多次调用
initAudioRecorder();
mAudioRecord.startRecording();
new Thread() {
@Override
public void run() {
//设置线程权限
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
while (mIsRecording) {
int readSize = mAudioRecord.read(mPCMBuffer, 0, mBufferSize);
if (readSize > 0) {
mEncodeThread.addTask(mPCMBuffer, readSize);
calculateRealVolume(mPCMBuffer, readSize);
}
}
// release and finalize audioRecord
mAudioRecord.stop();
mAudioRecord.release();
mAudioRecord = null;
// stop the encoding thread and try to wait
// until the thread finishes its job
mEncodeThread.sendStopMessage();
}
/**
* 此计算方法来自samsung开发范例
*
* @param buffer buffer
* @param readSize readSize
*/
private void calculateRealVolume(short[] buffer, int readSize) {
double sum = 0;
for (int i = 0; i < readSize; i++) {
// 这里没有做运算的优化,为了更加清晰的展示代码
sum += buffer[i] * buffer[i];
}
if (readSize > 0) {
double amplitude = sum / readSize;
mVolume = (int) Math.sqrt(amplitude);
}
}
}.start();
}
private int mVolume;
/**
* 获取真实的音量。 [算法来自三星]
* @return 真实音量
*/
public int getRealVolume() {
return mVolume;
}
/**
* 获取相对音量。 超过最大值时取最大值。
* @return 音量
*/
public int getVolume(){
if (mVolume >= MAX_VOLUME) {
return MAX_VOLUME;
}
return mVolume;
}
private static final int MAX_VOLUME = 2000;
/**
* 根据资料假定的最大值。 实测时有时超过此值。
* @return 最大音量值。
*/
public int getMaxVolume(){
return MAX_VOLUME;
}
public void stop(){
mIsRecording = false;
}
public boolean isRecording() {
return mIsRecording;
}
/**
* Initialize audio recorder
*/
private void initAudioRecorder() throws IOException {
mBufferSize = AudioRecord.getMinBufferSize(DEFAULT_SAMPLING_RATE,
DEFAULT_CHANNEL_CONFIG, DEFAULT_AUDIO_FORMAT.getAudioFormat());
int bytesPerFrame = DEFAULT_AUDIO_FORMAT.getBytesPerFrame();
/* Get number of samples. Calculate the buffer size
* (round up to the factor of given frame size)
* 使能被整除,方便下面的周期性通知
* */
int frameSize = mBufferSize / bytesPerFrame;
if (frameSize % FRAME_COUNT != 0) {
frameSize += (FRAME_COUNT - frameSize % FRAME_COUNT);
mBufferSize = frameSize * bytesPerFrame;
}
/* Setup audio recorder */
mAudioRecord = new AudioRecord(DEFAULT_AUDIO_SOURCE,
DEFAULT_SAMPLING_RATE, DEFAULT_CHANNEL_CONFIG, DEFAULT_AUDIO_FORMAT.getAudioFormat(),
mBufferSize);
mPCMBuffer = new short[mBufferSize];
/*
* Initialize lame buffer
* mp3 sampling rate is the same as the recorded pcm sampling rate
* The bit rate is 32kbps
*
*/
LameUtil.init(DEFAULT_SAMPLING_RATE, DEFAULT_LAME_IN_CHANNEL, DEFAULT_SAMPLING_RATE, DEFAULT_LAME_MP3_BIT_RATE, DEFAULT_LAME_MP3_QUALITY);
// Create and run thread used to encode data
// The thread will
mEncodeThread = new DataEncodeThread(mRecordFile, mBufferSize);
mEncodeThread.start();
mAudioRecord.setRecordPositionUpdateListener(mEncodeThread, mEncodeThread.getHandler());
mAudioRecord.setPositionNotificationPeriod(FRAME_COUNT);
}
}

View File

@@ -0,0 +1,32 @@
fileFormatVersion: 2
guid: 134eee28cbbda46bd8c5032af2a8fb0a
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Android: Android
second:
enabled: 1
settings: {}
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,22 @@
package com.czt.mp3recorder;
import android.media.AudioFormat;
public enum PCMFormat {
PCM_8BIT (1, AudioFormat.ENCODING_PCM_8BIT),
PCM_16BIT (2, AudioFormat.ENCODING_PCM_16BIT);
private int bytesPerFrame;
private int audioFormat;
PCMFormat(int bytesPerFrame, int audioFormat) {
this.bytesPerFrame = bytesPerFrame;
this.audioFormat = audioFormat;
}
public int getBytesPerFrame() {
return bytesPerFrame;
}
public int getAudioFormat() {
return audioFormat;
}
}

View File

@@ -0,0 +1,32 @@
fileFormatVersion: 2
guid: 6cfce2307b2fa48d6ae51da8e719e1e3
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Android: Android
second:
enabled: 1
settings: {}
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 5796cc6b4b0a2492da182ee538742752
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,74 @@
package com.czt.mp3recorder.util;
public class LameUtil {
static{
System.loadLibrary("mp3lame");
}
/**
* Initialize LAME.
*
* @param inSamplerate
* input sample rate in Hz.
* @param inChannel
* number of channels in input stream.
* @param outSamplerate
* output sample rate in Hz.
* @param outBitrate
* brate compression ratio in KHz.
* @param quality
* <p>quality=0..9. 0=best (very slow). 9=worst.</p>
* <p>recommended:</p>
* <p>2 near-best quality, not too slow</p>
* <p>5 good quality, fast</p>
* 7 ok quality, really fast
*/
public native static void init(int inSamplerate, int inChannel,
int outSamplerate, int outBitrate, int quality);
/**
* Encode buffer to mp3.
*
* @param bufferLeft
* PCM data for left channel.
* @param bufferRight
* PCM data for right channel.
* @param samples
* number of samples per channel.
* @param mp3buf
* result encoded MP3 stream. You must specified
* "7200 + (1.25 * buffer_l.length)" length array.
* @return <p>number of bytes output in mp3buf. Can be 0.</p>
* <p>-1: mp3buf was too small</p>
* <p>-2: malloc() problem</p>
* <p>-3: lame_init_params() not called</p>
* -4: psycho acoustic problems
*/
public native static int encode(short[] bufferLeft, short[] bufferRight,
int samples, byte[] mp3buf);
/**
* Flush LAME buffer.
*
* REQUIRED:
* lame_encode_flush will flush the intenal PCM buffers, padding with
* 0's to make sure the final frame is complete, and then flush
* the internal MP3 buffers, and thus may return a
* final few mp3 frames. 'mp3buf' should be at least 7200 bytes long
* to hold all possible emitted data.
*
* will also write id3v1 tags (if any) into the bitstream
*
* return code = number of bytes output to mp3buf. Can be 0
* @param mp3buf
* result encoded MP3 stream. You must specified at least 7200
* bytes.
* @return number of bytes output to mp3buf. Can be 0.
*/
public native static int flush(byte[] mp3buf);
/**
* Close LAME.
*/
public native static void close();
}

View File

@@ -0,0 +1,32 @@
fileFormatVersion: 2
guid: 091a5e5f71553434695297dd6e4e2b16
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Android: Android
second:
enabled: 1
settings: {}
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 2948157a0f46743fe821133054dff30d
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,93 @@
package com.newland;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import com.android.internal.telephony.ITelephony;
import android.app.Service;
import android.content.Context;
import android.telecom.TelecomManager;
import android.telephony.TelephonyManager;
import android.util.Log;
public class PhoneUtils {
/**
* 从TelephonyManager中实例化ITelephony并返回
*/
static public ITelephony getITelephony(TelephonyManager telMgr)
throws Exception {
Method getITelephonyMethod = telMgr.getClass().getDeclaredMethod(
"getITelephony");
getITelephonyMethod.setAccessible(true);// 私有化函数也能使用
return (ITelephony) getITelephonyMethod.invoke(telMgr);
}
public static int getSDKVersionNumber() {
int sdkVersion;
try {
sdkVersion = Integer.valueOf(android.os.Build.VERSION.SDK_INT);
} catch (NumberFormatException e) {
sdkVersion = 0;
}
return sdkVersion;
}
//判断手机是否处于正在通话中
public static boolean phoneIsInUse(Context context) {
int sdkVer = getSDKVersionNumber();
if(sdkVer >= 23) {
return phoneIsInUse6_0(context);
} else {
return phoneIsInUse5_0(context);
}
}
//Android如何判断手机是否处于正在通话中Android 6.0之前具体版本没追溯用的方法在Android 5.1上好用)
public static boolean phoneIsInUse5_0(Context context) {
boolean phoneInUse = false;
TelephonyManager mTelephonyManager = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
Class<TelephonyManager> c = TelephonyManager.class;
Method getITelephonyMethod = null;
try {
getITelephonyMethod = c.getDeclaredMethod("getITelephony",
(Class[]) null);
getITelephonyMethod.setAccessible(true);
ITelephony iTelephony = (ITelephony) getITelephonyMethod.invoke(
mTelephonyManager, (Object[]) null);
phoneInUse = !iTelephony.isIdle();
} catch (Exception e) {
e.printStackTrace();
}
return phoneInUse;
}
//Android 6.0之后用以上方法不好用了遍寻源码及网上资料得已下方法在Android 6.0上好用):
public static boolean phoneIsInUse6_0(Context context){
TelecomManager tm = (TelecomManager)context.getSystemService(Context.TELECOM_SERVICE);
return tm.isInCall();
}
static public void printAllInform(Class clsShow) {
try {
// 取得所有方法
Method[] hideMethod = clsShow.getDeclaredMethods();
int i = 0;
for (; i < hideMethod.length; i++) {
Log.e("method name", hideMethod[i].getName());
}
// 取得所有常量
Field[] allFields = clsShow.getFields();
for (i = 0; i < allFields.length; i++) {
Log.e("Field name", allFields[i].getName());
}
} catch (SecurityException e) {
// throw new RuntimeException(e.getMessage());
e.printStackTrace();
} catch (IllegalArgumentException e) {
// throw new RuntimeException(e.getMessage());
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,32 @@
fileFormatVersion: 2
guid: f2f1aac6d559f41b0a60fbfb671ffce2
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Android: Android
second:
enabled: 1
settings: {}
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant: