forked from guange2015/ios-amr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioRecord.java
121 lines (98 loc) · 3.47 KB
/
AudioRecord.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package cn.hugeox.poke.record;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Log;
import org.cocos2dx.lib.Cocos2dxActivity;
import java.io.File;
import java.io.IOException;
/**
* Created by guange on 13/12/2016.
*/
public class AudioRecord {
private static final String TAG = AudioRecord.class.getName();
private MediaRecorder mRecorder = null;
private MediaPlayer mPlayer = null;
private String audioFilePath = null;
private static AudioRecord audioRecord = null;
public static AudioRecord getInstance(){
if(audioRecord==null){
audioRecord = new AudioRecord();
}
return audioRecord;
}
private AudioRecord(){
}
public void startRecording() {
if(mRecorder!=null){
Log.e(TAG, "startRecording: 正在录音中");
return ;
}
try {
String fileName = ""+System.currentTimeMillis();
new File(this.getAmrFilePath(fileName)).deleteOnExit();
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setAudioChannels(1);
// 设置麦克风
// mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);// 设置输出文件格式
// mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);// 设置编码格式
mRecorder.setOutputFile(this.getAmrFilePath(fileName));// 使用绝对路径进行保存文件
mRecorder.prepare();
mRecorder.start();
audioFilePath = this.getAmrFilePath(fileName);
} catch (IOException e) {
Log.e(TAG, "prepare() failed");
}
}
public String stopRecording() {
if(mRecorder!=null){
mRecorder.stop();
mRecorder.release();
mRecorder = null;
return this.audioFilePath;
}
return "";
}
public void startPlaying(String fileName) {
Log.d(TAG, "startPlaying: "+fileName);
try {
mPlayer = new MediaPlayer();
mPlayer.setDataSource(fileName);//获取绝对路径来播放音频
mPlayer.prepare();
mPlayer.start();
mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
stopPlaying();
return false;
}
});
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
stopPlaying();
}
});
} catch (IOException e) {
Log.e(TAG, "prepare() failed");
}
}
public void stopPlaying() {
if(mPlayer!=null){
mPlayer.stop();
mPlayer.release();
mPlayer = null;
}
}
public String getAmrFilePath(String fileName) {
return Cocos2dxActivity
.getContext()
.getExternalFilesDir("audios")
.getAbsolutePath() + "/"+fileName+".amr";
}
}