本文实例为大家分享了Android录制音频文件的具体代码,供大家参考,具体内容如下
1、这个demo中没有对多次点击同一个声音文件做详细处理,偶尔会有崩溃,用的时候需要注意。
2、按住录音按钮录音过程中,只对竖直方向处理了一下,水平方向没写;
3、没有做删除某个声音文件的操作,但是测试的时候实现了功能,需要用到的话,在MainActivity—>onItemClick中的TODO中有详细说明;
4、这只是个demo,如果要在项目中使用,先写出demo,没问题了,再引入项目,在写成demo后,在真机上运行的时候,如果出现获取录音权限,最好选择“允许”,如果拒绝,可能会崩溃。
记得打开手机运行录音的权限
先来效果图:
目录结构:
1、添加权限:
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
2、新建MediaRecorderUtils,复制以下源码:
package com.chen.voicedemo;
import android.media.MediaRecorder;
import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;
import java.io.File;
public class MediaRecorderUtils {
private static MediaRecorder recorder;
static MediaRecorderUtils mediaRecorderUtils;
static ImageView mimageView;
private String path;
public static MediaRecorderUtils getInstence(ImageView imageView) {
if (mediaRecorderUtils == null) {
mediaRecorderUtils = new MediaRecorderUtils();
}
mimageView = imageView;
return mediaRecorderUtils;
}
public String getPath() {
return path;
}
private void init() {
recorder = new MediaRecorder();// new出MediaRecorder对象
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// 设置MediaRecorder的音频源为麦克风
recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
// 设置MediaRecorder录制的音频格式
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// 设置MediaRecorder录制音频的编码为amr.
File file = new File(Utils.IMAGE_SDCARD_MADER);
if (!file.exists()) {
file.mkdirs();
}
path = Utils.IMAGE_SDCARD_MADER + Utils.getVoiceFileName() + "stock.amr";
recorder.setOutputFile(path);
// 设置录制好的音频文件保存路径
try {
recorder.prepare();// 准备录制
} catch (Exception e) {
e.printStackTrace();
}
}
public void MediaRecorderStart() {
init();
try {
recorder.start();
flag = true;
if (mimageView != null) {
updateMicStatus();
}
} catch (Exception e) {
e.printStackTrace();
Log.e("chen", "录制失败");
}
}
public void MediaRecorderStop() {
try {
recorder.stop();
recorder.release(); //释放资源
flag = false;
mimageView = null;
recorder = null;
} catch (Exception e) {
e.toString();
}
}
public void MediaRecorderDelete() {
File file = new File(path);
if (file.isFile()) {
file.delete();
}
file.exists();
}
;
private final Handler mHandler = new Handler();
private Runnable mUpdateMicStatusTimer = new Runnable() {
public void run() {
updateMicStatus();
}
};
private int BASE = 1;
private int SPACE = 1000;// 间隔取样时间
private boolean flag = true;
private void updateMicStatus() {
if (recorder != null) {
double ratio = (double) recorder.getMaxAmplitude() / BASE;
double db = 0;// 分贝
if (ratio > 1) {
db = 20 * Math.log10(ratio);
}
int i = (int) db / 10;
switch (i) {
case 1:
mimageView.setImageResource(R.drawable.rc_ic_volume_1);
break;
case 2:
mimageView.setImageResource(R.drawable.rc_ic_volume_2);
break;
case 3:
mimageView.setImageResource(R.drawable.rc_ic_volume_3);
break;
case 4:
mimageView.setImageResource(R.drawable.rc_ic_volume_4);
break;
case 5:
mimageView.setImageResource(R.drawable.rc_ic_volume_5);
break;
case 6:
mimageView.setImageResource(R.drawable.rc_ic_volume_6);
break;
case 7:
mimageView.setImageResource(R.drawable.rc_ic_volume_7);
break;
case 8:
mimageView.setImageResource(R.drawable.rc_ic_volume_8);
break;
}
if (flag) {
mHandler.postDelayed(mUpdateMicStatusTimer, SPACE);
}
}
}
}
3、创建MyChronometer,复制以下代码
package com.chen.voicedemo;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.TextView;
public class MyChronometer extends TextView {
private static final String TAG = "MyChronometer";
public interface OnMyChronometerTickListener {
void onMyChronometerTick(int time);
}
public interface OnMyChronometerTimeListener {
void OnMyChronometerTimeListener(int time);
}
private OnMyChronometerTimeListener OnMyChronometerTimeListener;
private long mBase;
private boolean mVisible;
private boolean mStarted;
private boolean mRunning;
private OnMyChronometerTickListener mOnMyChronometerTickListener;
private long now_time;
private static final int TICK_WHAT = 2;
public MyChronometer(Context context) {
this(context, null, 0);
}
public MyChronometer(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyChronometer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
mBase = SystemClock.elapsedRealtime();
updateText(mBase);
}
public void setBase(long base) {
mBase = base;
updateText(SystemClock.elapsedRealtime());
}
public void setOnMyChronometerTickListener(OnMyChronometerTickListener listener) {
mOnMyChronometerTickListener = listener;
}
public void setOnMyChronometerTimeListener(OnMyChronometerTimeListener listener) {
OnMyChronometerTimeListener = listener;
}
public void start() {
mStarted = true;
updateRunning();
}
public void stop() {
mStarted = false;
updateRunning();
now_time /= 10;
if (OnMyChronometerTimeListener != null) {
OnMyChronometerTimeListener.OnMyChronometerTimeListener((int) now_time);
}
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mVisible = false;
updateRunning();
}
@Override
protected void onWindowVisibilityChanged(int visibility) {
super.onWindowVisibilityChanged(visibility);
mVisible = visibility == VISIBLE;
updateRunning();
}
private synchronized void updateText(long now) {
long seconds = now - mBase;
seconds /= 10;
now_time = seconds;
int time_m = (int) (seconds / 100);
if (mOnMyChronometerTickListener != null) {
mOnMyChronometerTickListener.onMyChronometerTick(time_m);
}
int time_s = (int) (seconds % 100);
setText(time_m + "");
}
private void updateRunning() {
boolean running = mVisible && mStarted;
if (running != mRunning) {
if (running) {
updateText(SystemClock.elapsedRealtime());
mHandler.sendMessageDelayed(Message.obtain(mHandler, TICK_WHAT), 1000);
} else {
mHandler.removeMessages(TICK_WHAT);
}
mRunning = running;
}
}
private Handler mHandler = new Handler() {
public void handleMessage(Message m) {
if (mRunning) {
updateText(SystemClock.elapsedRealtime());
sendMessageDelayed(Message.obtain(this, TICK_WHAT), 1000);
}
}
};
@SuppressLint("NewApi")
@Override
public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
super.onInitializeAccessibilityEvent(event);
event.setClassName(MyChronometer.class.getName());
}
@SuppressLint("NewApi")
@Override
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(info);
info.setClassName(MyChronometer.class.getName());
}
}
4、创建工具类
package com.chen.voicedemo;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
public class Utils {
public static final String IMAGE_SDCARD_MADER = Environment
.getExternalStorageDirectory()
+ "/chen/voice/";
public static boolean checkVoice(Context context) {
try {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
return false;
} else {
return true;
}
} catch (Exception e) {
return true;
}
}
private static Toast toast;
public static void showToast(Context context, String msg) {
if (toast == null) {
toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
}
toast.setText(msg);
toast.show();
}
public static ArrayList<String> getVideoFiles(String root) {
if (root == null || root == "")
return null;
ArrayList<String> list = new ArrayList<>();
File file = new File(root);
File[] fileList = file.listFiles();
for (File f : fileList) {
list.add(f.getPath());
}
return list;
}
public static String getVoiceFileName() {
long getNowTimeLong = System.currentTimeMillis();
SimpleDateFormat time = new SimpleDateFormat("yyyyMMddHHmmss");
String result = time.format(getNowTimeLong);
return result;
}
}
5、MainActivity
package com.chen.voicedemo;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.ColorDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity implements View.OnTouchListener, AdapterView.OnItemClickListener {
private TextView voice;
private TextView voice_popup;
private TextView show_voice_list;
private ListView show_voices_listview;
private List<String> voiceList;
private TextView stop_show_voice;
private ImageView voice_anim;
private MediaPlayer mediaPlayer;
private Boolean flag = true;
private float int_x = 0;
private float int_y = 0;
private int maxRecordTime = 60;
private int oftenOperationTime = 500;
private MyAdapter myAdapter;
private AnimationDrawable animation;
private PopupWindow voice_popupWindow;
private ImageView voice_shengyin;
private MyChronometer mychronometer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
voiceList = new ArrayList<String>();
voice = (TextView) findViewById(R.id.voice);
voice_popup = (TextView) findViewById(R.id.voice_popup);
voice_anim = (ImageView) findViewById(R.id.voice_anim);
voice_anim.setImageResource(R.drawable.lcs_voice_receive);
show_voice_list = (TextView) findViewById(R.id.show_voice_list);
show_voice_list.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
voiceList = Utils.getVideoFiles(Utils.IMAGE_SDCARD_MADER);
if (voiceList.size()>0){
myAdapter.notifyDataSetChanged();
}else{
Utils.showToast(MainActivity.this, "没有文件");
}
}
});
show_voices_listview = (ListView) findViewById(R.id.show_voices);
show_voices_listview.setOnItemClickListener(this);
myAdapter = new MyAdapter();
stop_show_voice = (TextView) findViewById(R.id.stop_show_voice);
stop_show_voice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("chen", "点击了停止播放按钮");
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.release();// 释放资源
}
mediaPlayer = null;
}
if (animation != null && animation.isRunning()) {
animation.stop();
}
voice_anim.setImageResource(R.drawable.lcs_voice_receive);
}
});
show_voices_listview.setAdapter(myAdapter);
voice.setOnTouchListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//TODO 以下4行,是用来做测试,点击item,手机SD卡上对应路径下的声音文件就会被删除。如果录制声音失败,或者不满足条件,可以把以下4行写成一个工具方法调用,删除不满意的文件。这里不做详细演示
//File f_delete=new File(voiceList.get(position));
//f_delete.delete();
//voiceList.remove(voiceList.get(position));
//myAdapter.notifyDataSetChanged();
//TODO 以上4行,是用来做测试,点击item,手机SD卡上对应路径下的声音文件就会被删除。
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
if (mp != null) {
mp.start();
voice_anim.setImageResource(R.drawable.voice_anim);
}
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
if (mp.isPlaying()) {
mp.release();// 释放资源
}
animation = (AnimationDrawable) voice_anim.getDrawable();
if (animation != null && animation.isRunning()) {
animation.stop();
}
voice_anim.setImageResource(R.drawable.lcs_voice_receive);
}
});
mediaPlayer.setDataSource(voiceList.get(position));
// 缓冲
mediaPlayer.prepare();
} catch (Exception e) {
Utils.showToast(MainActivity.this, "语音异常,加载失败");
}
}
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return voiceList.size() == 0 ? 0 : voiceList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = new TextView(MainActivity.this);
tv.setText(voiceList.get(position));
tv.setTextSize(20);
return tv;
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.voice) {
//检查权限
if (!Utils.checkVoice(this)) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Utils.showToast(this, "录音权限未打开,请打开录音权限!");
}
return true;
}
//避免短时间里频繁操作
if (!getTimeTF(SystemClock.elapsedRealtime()) && event.getAction() == MotionEvent.ACTION_DOWN) {
Utils.showToast(this, "操作过于频繁");
return true;
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
setTime(SystemClock.elapsedRealtime());
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
int_x = event.getRawX();
int_y = event.getRawY();
VoicePopupWindow();
mychronometer.setBase(SystemClock.elapsedRealtime());
mychronometer.start();
MediaRecorderUtils.getInstence(voice_shengyin).MediaRecorderStart();
flag = true;
mychronometer.setOnMyChronometerTickListener(new MyChronometer.OnMyChronometerTickListener() {
@Override
public void onMyChronometerTick(int time) {
if (time == maxRecordTime || time > maxRecordTime) {
mychronometer.setText("60");
setVoiceToUp();
}
}
});
break;
case MotionEvent.ACTION_MOVE:
if (flag) {
if (Math.abs(int_y) - Math.abs(event.getRawY()) > 100.0 && flag) {
voice_popupWindow.dismiss();
mychronometer.stop();
MediaRecorderUtils.getInstence(voice_shengyin).MediaRecorderStop();
MediaRecorderUtils.getInstence(voice_shengyin).MediaRecorderDelete();
flag = false;
}
}
break;
case MotionEvent.ACTION_CANCEL:
if (flag) {
voice_popupWindow.dismiss();
mychronometer.stop();
MediaRecorderUtils.getInstence(voice_shengyin).MediaRecorderStop();
}
break;
case MotionEvent.ACTION_UP:
if (flag) {
setVoiceToUp();
}
break;
}
return true;
}
return false;
}
private long base_time = 0;
private void setTime(long time) {
base_time = time;
}
private boolean getTimeTF(long time) {
int data = (int) (time - base_time) / oftenOperationTime;
if (data > 1) {
return true;
} else {
return false;
}
}
public void VoicePopupWindow() {
View view = LayoutInflater.from(this).inflate(R.layout.voice_popupwindow, null);
voice_popupWindow = new PopupWindow(this);
voice_popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
voice_popupWindow.setHeight(WindowManager.LayoutParams.MATCH_PARENT);
voice_shengyin = (ImageView) view.findViewById(R.id.voice_shengyin);
mychronometer = (MyChronometer) view.findViewById(R.id.mychronometer);
voice_popupWindow.setContentView(view);
voice_popupWindow.setFocusable(true);
ColorDrawable dw = new ColorDrawable(0x00000000);
voice_popupWindow.setBackgroundDrawable(dw);
voice_popupWindow.showAsDropDown(voice_popup);
}
private void setVoiceToUp() {
flag = false;
voice_popupWindow.dismiss();
mychronometer.stop();
MediaRecorderUtils.getInstence(voice_shengyin).MediaRecorderStop();
int time = Integer.parseInt(mychronometer.getText().toString());
if (time != 0) {
File file = new File(MediaRecorderUtils.getInstence(voice_shengyin).getPath());
if (file.length() > 0) {
voiceList = Utils.getVideoFiles(Utils.IMAGE_SDCARD_MADER);
myAdapter.notifyDataSetChanged();
} else {
Utils.showToast(this, "录音失败,请检查权限");
}
} else {
Utils.showToast(this, "录音时间太短");
}
}
}
6、activity_main布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/voice_popup"
android:layout_width="match_parent"
android:layout_height="1dip"/>
<ListView
android:id="@+id/show_voices"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/voice_anim"
android:layout_width="60dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:background="#00ff00"/>
<TextView
android:id="@+id/stop_show_voice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"
android:background="#00ff00"
android:padding="10dp"
android:text="停止播放"
android:textColor="#000000"
android:textSize="20sp"
/>
<TextView
android:id="@+id/show_voice_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"
android:layout_toLeftOf="@id/stop_show_voice"
android:background="#00ff00"
android:padding="10dp"
android:text="列表"
android:textColor="#000000"
android:textSize="20sp"
/>
</RelativeLayout>
<TextView
android:id="@+id/voice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#00ff00"
android:padding="10dp"
android:text="开始录音"
android:textColor="#000000"
android:textSize="25sp"/>
</LinearLayout>
7、voice_popupwindow布局代码:录音的时候,会出现以下图片中的popupwindow
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@android:color/black"
android:orientation="vertical"
android:paddingBottom="40dip"
android:paddingLeft="60dip"
android:paddingRight="60dip"
android:paddingTop="40dip">
<ImageView
android:id="@+id/voice_shengyin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/rc_ic_volume_1"/>
<com.chen.voicedemo.MyChronometer
android:id="@+id/mychronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:textColor="@android:color/white"/>
</LinearLayout>
</RelativeLayout>
8、还有一个动画布局,播放声音的时候,有个动画效果
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/volume_animation"
android:oneshot="false" >
<item
android:drawable="@drawable/rc_ic_voice_receive_play1"
android:duration="100"/>
<item
android:drawable="@drawable/rc_ic_voice_receive_play2"
android:duration="200"/>
<item
android:drawable="@drawable/rc_ic_voice_receive_play3"
android:duration="300"/>
</animation-list>
附录:用到的图片资源说明:如果手上没有这样的图片,可以随便用其他图片代替,有效果,就算成功