/**
* 全程录音
*/
public class AllVideoUtil {
private static MediaRecordermediaRecorder;
private static Stringp;
public static boolean runuing =false;
public static boolean start() {
runuing =true;
if (mediaRecorder ==null)
mediaRecorder =new MediaRecorder();
try {
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);// 设置麦克风
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmss");
File destDir =new File(Environment.getExternalStorageDirectory() +"/switching/");
if (!destDir.exists()) {
destDir.mkdirs();
}
String filePath = Environment.getExternalStorageDirectory() +"/switching/" + fileName +".amr";
p = filePath;
mediaRecorder.setOutputFile(filePath);
mediaRecorder.prepare();
mediaRecorder.start();
}catch (IOException e) {
e.printStackTrace();
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder =null;
runuing =false;
}
return true;
}
public static Filestop() {//停止录音并返回录音文件
if (mediaRecorder ==null)
return null;
runuing =false;
try {
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder =null;
return new File(p);
}catch (RuntimeException e) {
mediaRecorder.reset();
mediaRecorder.release();
mediaRecorder =null;
return null;
}
}
}
/**
* 播放
*/
public class VideoUtil {
private static MediaRecordermediaRecorder;
private static Stringp;
private static boolean running =false;
private static MediaPlayermediaPlayer;
public static boolean start() {
if (running) {
running =false;
stop();
return false;
}
running =true;
if (mediaRecorder ==null)
mediaRecorder =new MediaRecorder();
try {
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);// 设置麦克风
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
String fileName = DateUtil.format(new Date(), "yyyyMMddHHmmss");
File destDir =new File(Environment.getExternalStorageDirectory() +"/switching/");
if (!destDir.exists()) {
destDir.mkdirs();
}
String filePath = Environment.getExternalStorageDirectory() +"/switching/" + fileName +".amr";
p = filePath;
mediaRecorder.setOutputFile(filePath);
mediaRecorder.prepare();
mediaRecorder.start();
}catch (IllegalStateException e) {
running =false;
}catch (IOException e) {
running =false;
}
return true;
}
public static Filestop() {
if (running) {
running =false;
try {
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder =null;
return new File(p);
}catch (RuntimeException e) {
mediaRecorder.reset();
mediaRecorder.release();
mediaRecorder =null;
return null;
}
}else {
return null;
}
}
//开始播放
public static void play(String path) {
if (mediaPlayer ==null) {
mediaPlayer =new MediaPlayer();
}
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(path);
mediaPlayer.prepare();
mediaPlayer.start();
}catch (Exception e) {
LogUtil.e(e);
}
}
/**
* 停止播放
*/
public static void stopPlay() {
if (mediaPlayer !=null &&mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer =null;
}
}
}
网友评论