android录音和播放录音

android中使用MediaRecorder录音, 然后使用MediaPlayer播放录音.

[TOC]

From

android录音和播放录音

Source

activity_main.xml

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
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.qefee.pj.testrecord.MainActivity">



<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">


<Button
android:id="@+id/startRecordButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/startRecord"/>


<Button
android:id="@+id/stopRecordButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/stopRecord"/>


<Button
android:id="@+id/playRecordButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/playRecord"/>


<Button
android:id="@+id/stopPlayRecordButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/stopPlayRecord"/>


<Button
android:id="@+id/recordInfoButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/recordInfo"/>


<TextView
android:id="@+id/recordInfoTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/recordInfo"/>

</LinearLayout>
</RelativeLayout>

MainActivity.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.qefee.pj.testrecord;

import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.File;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";

/**
* 开始录音按钮.
*/

Button startRecordButton;
/**
* 停止录音按钮.
*/

Button stopRecordButton;
/**
* 播放录音按钮.
*/

Button playRecordButton;
/**
* 停止播放录音按钮.
*/

Button stopPlayRecordButton;
/**
* 录音信息按钮.
*/

Button recordInfoButton;
/**
* 录音信息TextView.
*/

TextView recordInfoTextView;

/**
* 播放器对象.
*/

private MediaPlayer mediaPlayer;
/**
* 录音器对象.
*/

private MediaRecorder mediaRecorder;

/**
* 录音文件名.
*/

private String recordFileName;
/**
* 录音文件.
*/

private File recordFile;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

recordFileName = "r.3gp";
recordFile = new File(getExternalCacheDir(), recordFileName);

startRecordButton = (Button) findViewById(R.id.startRecordButton);
stopRecordButton = (Button) findViewById(R.id.stopRecordButton);
playRecordButton = (Button) findViewById(R.id.playRecordButton);
stopPlayRecordButton = (Button) findViewById(R.id.stopPlayRecordButton);
recordInfoButton = (Button) findViewById(R.id.recordInfoButton);

recordInfoTextView = (TextView) findViewById(R.id.recordInfoTextView);

startRecordButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setOutputFile(recordFile.getAbsolutePath());
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.prepare();
mediaRecorder.start();

Log.i(TAG, "onClick: 录音开始");
} catch (IOException e) {
Log.e(TAG, "onClick: 录音失败", e);
} catch (IllegalStateException e) {
Log.e(TAG, "onClick: 录音失败", e);
}
}
});

stopRecordButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if (mediaRecorder != null) {
try {
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;

Log.i(TAG, "onClick: 录音停止");
} catch (IllegalStateException e) {
Log.e(TAG, "onClick: 录音状态不正确", e);
}
} else {
Log.i(TAG, "onClick: 录音器没有准备好");
}
}
});

playRecordButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if (recordFile.exists()) {
try {
mediaPlayer = new MediaPlayer();

mediaPlayer.setDataSource(recordFile.getAbsolutePath());
mediaPlayer.prepare();
mediaPlayer.start();

Log.i(TAG, "onClick: 播放录音开始");
} catch (IOException e) {
Log.e(TAG, "onClick: 播放录音失败", e);
}
} else {
Log.i(TAG, "onClick: 没有录音文件");
}


}
});

stopPlayRecordButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;

Log.i(TAG, "onClick: 停止播放录音");
} else {
Log.i(TAG, "onClick: 播放器没有准备好");
}
}
});

recordInfoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer != null) {
int milliseconds = mediaPlayer.getDuration();

recordInfoTextView.setText(String.valueOf(milliseconds));

Log.i(TAG, "onClick: 获取录音信息");
} else {
Log.i(TAG, "onClick: 播放器没有准备好");
}
}
});
}
}

strings.xml

1
2
3
4
5
6
7
8
9
<resources>
<string name="app_name">TestRecord</string>

<string name="startRecord">开始录音</string>
<string name="stopRecord">停止录音</string>
<string name="playRecord">播放录音</string>
<string name="stopPlayRecord">停播录音</string>
<string name="recordInfo">录音信息</string>
</resources>

AndroidManifest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.qefee.pj.testrecord">


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

reference

[Android] 录音与播放录音实现

Fork me on GitHub