Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9fbab82

Browse files
author
chenyouwei
committed
feat: UI完善,代码整理
1 parent b6dfb96 commit 9fbab82

File tree

17 files changed

+843
-77
lines changed

17 files changed

+843
-77
lines changed

‎app/src/main/AndroidManifest.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
<activity android:name=".FmodActivity"/>
2929
<activity android:name="com.hugh.audiohome.AudioFunHomeActivity" />
30+
<activity android:name="com.hugh.webrtcdemo.RtcActivity"/>
3031
</application>
3132

3233
</manifest>

‎app/src/main/java/com/hugh/MainActivity.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
import com.hugh.audiofun.FmodActivity;
99
import com.hugh.audiofun.R;
10-
import com.hugh.libwebrtc.RtcActivity;
11-
import com.hugh.sound.SoundTouchActivity;
10+
import com.hugh.webrtcdemo.RtcActivity;
11+
import com.hugh.sound.SoundTouchExActivity;
1212

1313
import androidx.annotation.Nullable;
1414

@@ -34,7 +34,7 @@ public void onClick(View v) {
3434
findViewById(R.id.btn_go_soundtouch).setOnClickListener(new View.OnClickListener() {
3535
@Override
3636
public void onClick(View v) {
37-
startActivity(new Intent(MainActivity.this, SoundTouchActivity.class));
37+
startActivity(new Intent(MainActivity.this, SoundTouchExActivity.class));
3838
}
3939
});
4040

‎app/src/main/java/com/hugh/audiofun/FmodActivity.java‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
package com.hugh.audiofun;
22

33
import android.app.Activity;
4-
import android.content.Intent;
54
import android.os.Bundle;
65
import android.util.Log;
76
import android.view.View;
87
import android.widget.ListView;
98
import android.widget.TextView;
109

11-
import com.hugh.audiohome.AudioFunHomeActivity;
12-
import com.hugh.libwebrtc.RtcActivity;
13-
import com.hugh.libwebrtc.other.RtcFileActivity;
1410
import com.hugh.sound.SoundTouch;
15-
import com.hugh.sound.SoundTouchActivity;
1611
import com.zhl.commonadapter.BaseViewHolder;
1712
import com.zhl.commonadapter.CommonAdapter;
1813

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
package com.hugh.component.audio;
2+
3+
import android.media.AudioFormat;
4+
import android.media.AudioRecord;
5+
import android.media.MediaRecorder;
6+
import android.util.Log;
7+
8+
import java.io.FileNotFoundException;
9+
import java.io.FileOutputStream;
10+
import java.io.IOException;
11+
12+
/**
13+
* Created by chenyw on 2020年8月10日.
14+
*/
15+
public class AudioRecordManager {
16+
private static final String TAG = "AudioRecordManager";
17+
/*录音管理类实例*/
18+
public static AudioRecordManager sInstance;
19+
/*录音实例*/
20+
protected AudioRecord mAudioRecord;
21+
/*录音线程*/
22+
private Thread mRecordThread;
23+
/*音频采集的输入源,麦克风*/
24+
private static int AUDIO_SOURCE = MediaRecorder.AudioSource.MIC;
25+
/*音频采集的采样频率*/
26+
public static int SAMPLE_RATE_IN_HZ = 44100;
27+
/*音频采集的声道数,此处为单声道,后期处理可以转换成双声道的立体声,如果这里是MONO声道的话,会有变声的情况*/
28+
private static int CHANNEL_CONFIGURATION = AudioFormat.CHANNEL_IN_STEREO;
29+
/*音频采集的格式,数据位宽16位*/
30+
private static final int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
31+
/*音频缓冲区大小*/
32+
private int mBufferSizeInBytes = 0;
33+
/*是否正在录音*/
34+
private boolean mIsRecording = false;
35+
/*文件输出流*/
36+
private FileOutputStream mFileOutputStream;
37+
/*文件输出路径*/
38+
private String mOutputFilePath;
39+
40+
/*单例模式*/
41+
private AudioRecordManager(){}
42+
public static AudioRecordManager getInstance(){
43+
if (null == sInstance){
44+
synchronized (AudioRecordManager.class){
45+
if (null == sInstance){
46+
sInstance = new AudioRecordManager();
47+
return sInstance;
48+
}
49+
}
50+
}
51+
return sInstance;
52+
}
53+
54+
/**
55+
* 初始化配置
56+
*/
57+
public void initConfig() throws Exception {
58+
59+
if (null != mAudioRecord) mAudioRecord.release();
60+
61+
//使用44.1kHz的采样率初始化录音器
62+
try {
63+
mBufferSizeInBytes = AudioRecord.getMinBufferSize(SAMPLE_RATE_IN_HZ,CHANNEL_CONFIGURATION,AUDIO_FORMAT);
64+
mAudioRecord = new AudioRecord(AUDIO_SOURCE,SAMPLE_RATE_IN_HZ,CHANNEL_CONFIGURATION,AUDIO_FORMAT,mBufferSizeInBytes);
65+
} catch (IllegalArgumentException e) {
66+
e.printStackTrace();
67+
Log.e(TAG,"采样率44.1kHZ的AudioRecord初始化失败");
68+
}
69+
70+
/* //44.1kHz采样率没有成功的话,再使用16kHz的采样率初始化录音器
71+
if (mAudioRecord == null || mAudioRecord.getState() != AudioRecord.STATE_INITIALIZED){
72+
try {
73+
SAMPLE_RATE_IN_HZ = 16000;
74+
mBufferSizeInBytes = AudioRecord.getMinBufferSize(SAMPLE_RATE_IN_HZ,CHANNEL_CONFIGURATION,AUDIO_FORMAT);
75+
mAudioRecord = new AudioRecord(AUDIO_SOURCE,SAMPLE_RATE_IN_HZ,CHANNEL_CONFIGURATION,AUDIO_FORMAT,mBufferSizeInBytes);
76+
} catch (IllegalArgumentException e) {
77+
e.printStackTrace();
78+
Log.e(TAG,"采样率16kHZ的AudioRecord初始化失败");
79+
}
80+
}*/
81+
82+
//都失败的话,抛出异常
83+
if (mAudioRecord == null || mAudioRecord.getState() != AudioRecord.STATE_INITIALIZED) throw new Exception();
84+
85+
}
86+
87+
/**
88+
* 开始录音
89+
*/
90+
public void startRecord(String filePath) throws Exception {
91+
92+
if (mAudioRecord != null && mAudioRecord.getState() == AudioRecord.STATE_INITIALIZED){
93+
try {
94+
mAudioRecord.startRecording();
95+
} catch (Exception e) {
96+
throw new Exception();
97+
}
98+
}else {
99+
throw new Exception();
100+
}
101+
102+
mIsRecording = true;
103+
mRecordThread = new Thread(new RecordRunnable(),"RecordThread");
104+
105+
try {
106+
this.mOutputFilePath = filePath;
107+
mRecordThread.start();
108+
} catch (Exception e) {
109+
e.printStackTrace();
110+
throw new Exception();
111+
}
112+
}
113+
114+
/**
115+
* 结束录音
116+
*/
117+
public void stopRecord(){
118+
try {
119+
if (mAudioRecord != null){
120+
121+
mIsRecording = false;
122+
123+
//关闭线程
124+
try {
125+
if (mRecordThread != null){
126+
mRecordThread.join();
127+
mRecordThread = null;
128+
}
129+
} catch (InterruptedException e) {
130+
e.printStackTrace();
131+
}
132+
133+
//关闭录音,释放资源
134+
releaseAudioRecord();
135+
}
136+
} catch (Exception e) {
137+
e.printStackTrace();
138+
}
139+
}
140+
141+
/**
142+
* 关闭录音,释放资源
143+
*/
144+
private void releaseAudioRecord(){
145+
if (mAudioRecord.getState() == AudioRecord.STATE_INITIALIZED) {
146+
mAudioRecord.stop();
147+
mAudioRecord.release();
148+
mAudioRecord = null;
149+
}
150+
}
151+
152+
153+
class RecordRunnable implements Runnable{
154+
@Override
155+
public void run() {
156+
try {
157+
mFileOutputStream = new FileOutputStream(mOutputFilePath);
158+
byte[] audioDataArray = new byte[mBufferSizeInBytes];
159+
while (mIsRecording){
160+
int audioDataSize = getAudioRecordBufferSize(mBufferSizeInBytes,audioDataArray);
161+
if (audioDataSize > 0) {
162+
mFileOutputStream.write(audioDataArray);
163+
}else {
164+
mIsRecording = false;
165+
}
166+
}
167+
} catch (FileNotFoundException e) {
168+
e.printStackTrace();
169+
} catch (IOException e) {
170+
e.printStackTrace();
171+
}finally {
172+
try {
173+
if (null != mFileOutputStream){
174+
mFileOutputStream.close();
175+
mFileOutputStream = null;
176+
}
177+
} catch (IOException e) {
178+
e.printStackTrace();
179+
}
180+
}
181+
}
182+
}
183+
184+
/**
185+
* 读取缓存区的大小
186+
* @param bufferSizeInBytes
187+
* @param audioDataArray
188+
* @return
189+
*/
190+
private int getAudioRecordBufferSize(int bufferSizeInBytes, byte[] audioDataArray) {
191+
if (mAudioRecord != null){
192+
int size = mAudioRecord.read(audioDataArray,0,bufferSizeInBytes);
193+
return size;
194+
}else {
195+
return 0;
196+
}
197+
}
198+
199+
/**
200+
* 是否正在录音
201+
*/
202+
public boolean isRecording(){
203+
return mIsRecording;
204+
}
205+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /