Android录音和录像功能详解

本文使用Android系统自带Api

import android.media.MediaRecorder;
import android.media.MediaPlayer;

来进行Android录音和录像功能的实现,下面上主要代码

创建保存路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* 根据Audio种类创建文件保存路径
* @param isAudio
* @return
*/
private File getFilePath(boolean isAudio) {
File file = null;
File sampleDir = Environment.getExternalStorageDirectory();
if (!sampleDir.canWrite())
sampleDir = new File("/mnt/sdcard");

try {
file = File.createTempFile(SAMPLE_PREFIX,
isAudio ? ".amr" : ".mp4", sampleDir);
} catch (IOException e) {
}
return file;
}

录音代码

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
@SuppressLint("InlinedApi")
public void startRecorderAudio(View view) {
stop(view);
mtv.setText("开始录音...");

mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mAudioFile = getFilePath(true);

// Handle IOException
try {
mRecorder.setOutputFile(mAudioFile.getAbsolutePath());
mRecorder.prepare();
} catch (Exception exception) {
setError(INTERNAL_ERROR);
mRecorder.reset();
mRecorder.release();
mRecorder = null;
return;
}
// Handle RuntimeException if the recording couldn't start
try {
mRecorder.start();
} catch (RuntimeException exception) {
AudioManager audioMngr = (AudioManager) this
.getSystemService(Context.AUDIO_SERVICE);
boolean isInCall = ((audioMngr.getMode() == AudioManager.MODE_IN_CALL) || (audioMngr
.getMode() == AudioManager.MODE_IN_COMMUNICATION));
if (isInCall) {
setError(IN_CALL_RECORD_ERROR);
} else {
setError(INTERNAL_ERROR);
}
mRecorder.reset();
mRecorder.release();
mRecorder = null;
return;
}

}

public void stopRecorderAudio(View view) {
if (mRecorder == null)
return;

mtv.setText("停止录音...");
mRecorder.stop();
mRecorder.release();
mRecorder = null;

}

录像代码

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
@SuppressLint("NewApi")
public void startRecorderVideo(View view) {

stop(view);
mtv.setText("开始录像...");

mMediaRecorder = new MediaRecorder();
mMediaRecorder.setPreviewDisplay(mSurfaceView.getHolder().getSurface());

// mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
// 这两项需要放在setOutputFormat之后
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);

// 设置立体声
mMediaRecorder.setAudioChannels(2);
// 设置最大录像时间 单位:毫秒
mMediaRecorder.setMaxDuration(600000);
// 设置最大录制的大小 单位,字节
mMediaRecorder.setMaxFileSize(1024 * 1024);
// 音频一秒钟包含多少数据位
mMediaRecorder.setAudioEncodingBitRate(128);
// 设置选择角度,顺时针方向,因为默认是逆向90度的,这样图像就是正常显示了,
// 这里设置的是观看保存后的视频的角度
// mMediaRecorder.setOrientationHint(90);
// 设置录像的分辨率
mMediaRecorder.setVideoSize(720, 480);

// mMediaRecorder.setVideoFrameRate(20);

try {
mMediaRecorder.setOutputFile(getFilePath(false).getAbsolutePath());
mMediaRecorder.prepare();

mMediaRecorder.start(); // Recording is now started

} catch (RuntimeException exception) {

exception.printStackTrace();
mMediaRecorder.reset();
mMediaRecorder.release();
mMediaRecorder = null;
return;
} catch (Exception e) {
e.printStackTrace();
}
}

public void stopRecorderVideo(View view) {

if (mMediaRecorder == null) {
return;
}
mtv.setText("停止录像...");
mMediaRecorder.stop();
mMediaRecorder.release();
mMediaRecorder = null;

}

播放测试代码

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
public void startPlayback(View view) {
stop(view);
mtv.setText("开始播放...");
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(mAudioFile.getAbsolutePath());
mPlayer.setOnCompletionListener(this);
mPlayer.setOnErrorListener(this);
mPlayer.prepare();
mPlayer.start();
} catch (IllegalArgumentException e) {
setError(INTERNAL_ERROR);
mPlayer = null;
return;
} catch (IOException e) {
setError(SDCARD_ACCESS_ERROR);
mPlayer = null;
return;
}

}

public void stopPlayback(View view) {
if (mPlayer == null) // we were not in playback
return;
mtv.setText("停止播放...");
mPlayer.stop();
mPlayer.release();
mPlayer = null;

}