/**
- <p>
- 已经集成了录音、编码、压缩等,支持少量的录音音频格式,大概有.aac(API = 16) .amr .3gp
- <p>
- 优点:大部分以及集成,直接调用相关接口即可,代码量小
- <p>
- 缺点:无法实时处理音频;输出的音频格式不是很多,例如没有输出mp3格式文件
*/
实现:
MediaRecord:
public class MediaRecordManager {
private static MediaRecordManager INSTANCE = null;
private MediaRecorder mediaRecorder;
public static MediaRecordManager getInstance() {
if (INSTANCE == null)
synchronized (MediaRecordManager.class) {
if (INSTANCE == null)
INSTANCE = new MediaRecordManager();
}
return INSTANCE;
}
/**
* 配置MediaRecord,开始录音
*/
public void start(String savePath) {
try {
if (mediaRecorder == null) {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//音频的采集方式
mediaRecorder.setOutputFormat(MediaRecorder.AudioEncoder.AMR_NB);//音频的输出格式
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//音频的编码格式
}
//输出音频文件
mediaRecorder.setOutputFile(savePath);
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
//停止录制音频,释放资源
public void stop() {
if (mediaRecorder != null) {
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
}
}
}
MediaPlayer:
public class MediaPlayerManager {
private static MediaPlayerManager INSTANCE = null;
private MediaPlayer mediaPlayer;
public static MediaPlayerManager getInstance() {
if (INSTANCE == null)
synchronized (MediaPlayerManager.class) {
if (INSTANCE == null)
INSTANCE = new MediaPlayerManager();
}
return INSTANCE;
}
//播放音频资源
public boolean play(String sourcePath) {
return play(sourcePath, new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stop();
}
},
new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
stop();
return false;
}
});
}
private boolean play(String url, MediaPlayer.OnCompletionListener completionListener,
MediaPlayer.OnErrorListener onErrorListener) {
stop();
if (TextUtils.isEmpty(url))
return false;
try {
if (mediaPlayer == null) {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(url);
mediaPlayer.setVolume(0.7f, 0.7f);//设置左右声道的音量大小
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepare();
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(completionListener);
mediaPlayer.setOnErrorListener(onErrorListener);
return true;
}
} catch (IOException e) {
e.printStackTrace();
stop();
}
return false;
}
public void stop() {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}
}
调用:
/**
* 录制音频
*
* @param view view
*/
public void mediaRecord(View view) {
//检查文件写入的权限
int i = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (i == PackageManager.PERMISSION_GRANTED) {
mediaRecord();
} else {
//申请文件写入的权限
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 12);
}
}
private MediaRecordManager mediaRecordManager;
private String url;
private void mediaRecord() {
getSourcePath();
if (mediaRecordManager == null)
mediaRecordManager = MediaRecordManager.getInstance();
if (!TextUtils.isEmpty(url))
mediaRecordManager.start(url);
}
private void getSourcePath() {
File downLoadFile = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
if (downLoadFile != null) {
String path = downLoadFile.getPath() + File.separator + "Audio";
File file = new File(path);
file.mkdirs();
url = path + File.separator + "test.amr";
}
}
public void mediaRecordStop(View view) {
mediaRecordManager.stop();
}
private MediaPlayerManager mediaPlayerManager;
/**
* 播放音频
*
* @param view view
*/
public void mediaPlayer(View view) {
getSourcePath();
if (mediaPlayerManager == null)
mediaPlayerManager = MediaPlayerManager.getInstance();
if (!TextUtils.isEmpty(url))
mediaPlayerManager.play(url);
}
关于Android的AudioRecord和AudioTrack可以查看我的上一篇