第一种
import java.io.IOException;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Log;
/**
* 录音 https://github.com/l123456789jy/Lazy
*/
public class RecorderControl {
//录音
private MediaRecorder mRecorder = null;
/** 采样率 */
private static int SAMPLE_RATE_IN_HZ = 8000;
//播放录音
private MediaPlayer mPlayer = null;
private boolean playState = false; // 录音的播放状态
private String mFileName;
public MediaPlayer getmPlayer() {
return mPlayer;
}
public void setmPlayer(MediaPlayer mPlayer) {
this.mPlayer = mPlayer;
}
public RecorderControl(){
}
public RecorderControl(String name) {
if(mRecorder == null){
mRecorder = new MediaRecorder();
}
if(mPlayer == null){
mPlayer = new MediaPlayer();
}
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/menmen";
// mFileName += "/" + name + ".3gp";
// mFileName += "/" + name + ".mp3";
mFileName += "/" + name + ".arm";
}
/*
* 开始录音
*/
public void startRecording() {
// 实例化MediaRecorder
if(mRecorder == null){
mRecorder = new MediaRecorder();
}
// 设置音频源为MIC
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// 设置输出文件的格式
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
// mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// 设置输出文件的名称
mRecorder.setOutputFile(mFileName);
//设置音频的编码格式
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
//设置采样率
mRecorder.setAudioSamplingRate(SAMPLE_RATE_IN_HZ);
try {
//得到设置的音频来源,编码器,文件格式等等内容,在start()之前调用
mRecorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
//开始录音
mRecorder.start();
} catch (Exception e) {
mRecorder = null;
mRecorder = new MediaRecorder();
}
}
/**
*
* @return String
*/
/*
* 停止录音
*/
public String stopRecording(){
try {
mRecorder.stop();
} catch (Exception e) {
//释放资源
mRecorder = null;
mRecorder = new MediaRecorder();
}
//释放资源
mRecorder.release();
mRecorder = null;
return mFileName;
}
/**
*
* @param Filename Filename
* @param completion completion
*/
//
public void startPlaying(String Filename,OnCompletionListener completion){
if(!playState){
if(mPlayer == null){
mPlayer = new MediaPlayer();
}
try {
mPlayer.setDataSource(Filename);
mPlayer.prepare();
playState = true;
mPlayer.start();
mPlayer.setOnCompletionListener(completion);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else{
if(mPlayer.isPlaying()){
mPlayer.stop();
playState = false;
// startPlaying(Filename);
} else {
playState = false;
}
}
}
/**
* 播放完后释放资源
*/
public void playingFinish(){
Log.i("spoort_list", "RecorderControl 播放结束释放资源");
if(playState){
playState = false;
}
mPlayer.release();
mPlayer = null;
}
/**
* 停止播放
* @return boolean
*/
public boolean stopPlaying(){
if(mPlayer!=null){
// if(mPlayer!=null&&mPlayer.isPlaying()){
Log.i("spoort_list", "RecorderControl mPlayer.stop()");
mPlayer.stop();
mPlayer.release();
mPlayer = null;
playState = false;
return true;
}else{
Log.i("spoort_list", "RecorderControl mPlayer.stop() is null");
return false;
}
}
//当文件播放结束后调用此方法
// OnCompletionListener completion = new OnCompletionListener() {
// @Override
// public void onCompletion(MediaPlayer mp) {
//// if(playState){
//// playState = false;
//// }
// Log.i("spoort_list", "RecorderControl 播放结束");
// mPlayer.release();
// mPlayer = null;
// }
// };
/**
* 获取音量的大小
* @return double
*/
public double getAmplitude() {
if(mRecorder!=null){
return (mRecorder.getMaxAmplitude());
}else
return 0;
}
}
第二种
import android.media.MediaRecorder;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
/**
* Created by cooffee on 15/10/19.
*/
public class AudioManager {
private MediaRecorder mMediaRecorder;
private String mDir;
private String mCurrentFilePath;
private static AudioManager mInstance;
private boolean isPrepared = false;
private AudioManager(String dir) {
mDir = dir;
}
public String getCurrentFilePath() {
return mCurrentFilePath;
}
/**
* 回调准备完毕
*/
public interface AudioStateListener {
void wellPrepared();
}
private AudioStateListener mListener;
public void setOnAudioStateListner(AudioStateListener listner) {
mListener = listner;
}
public static AudioManager getInstance(String dir) {
if (null == mInstance) {
synchronized (AudioManager.class) {
if (null == mInstance) {
mInstance = new AudioManager(dir);
}
}
}
return mInstance;
}
public void prepareAudio() {
Log.d("LONG", "preparedAudio");
try {
isPrepared = false;
File dir = new File(mDir);
if (!dir.exists()) {
dir.mkdirs();
}
String fileName = generateFileName();
File file = new File(dir, fileName);
Log.d("LONG", "the file name is " + fileName);
mCurrentFilePath = file.getAbsolutePath();
mMediaRecorder = new MediaRecorder();
// 设置输出文件
mMediaRecorder.setOutputFile(file.getAbsolutePath()); Log.d("LONG", "1");
// 设置MediaRecorder的音频源为麦克风
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); Log.d("LONG", "2");
// 设置音频格式 AMR_NB
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB); Log.d("LONG", "3");
// 设置音频的编码为AMR
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); Log.d("LONG", "4");
mMediaRecorder.prepare(); Log.d("LONG", "5");
mMediaRecorder.start(); Log.d("LONG", "6");
// 准备结束
isPrepared = true; Log.d("LONG", "7");
if (mListener != null) {
Log.d("LONG", "AudioStateListener is not null");
mListener.wellPrepared();
} else {
Log.d("LONG", "lisetner null");
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 随机生成文件的名称
*
* @return
*/
private String generateFileName() {
return UUID.randomUUID().toString() + ".amr";
}
public int getVoiceLevel(int maxLevel) {
if (isPrepared) {
// mMediaRecorder.getMaxAmplitude() 1-32767
try {
return maxLevel * mMediaRecorder.getMaxAmplitude() / 14800; //32768 + 1
} catch (Exception e) {
// 忽略产生的异常
}
}
return 1;
}
public void release() {
mMediaRecorder.stop();
mMediaRecorder.release();
mMediaRecorder = null;
}
/**
* 释放资源 同时删除音频文件
*/
public void cancel() {
release();
if (mCurrentFilePath != null) {
File file = new File(mCurrentFilePath);
file.delete();
mCurrentFilePath = null;
}
}
}
网友评论