This commit is contained in:
square group sky 2019-11-27 16:06:07 +08:00
parent f0dd009533
commit 013dcaa4ce
2 changed files with 279 additions and 0 deletions

View File

@ -0,0 +1,255 @@
package net.micode.notes.audiodemo;
import android.Manifest;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.lqr.adapter.LQRAdapterForRecyclerView;
import com.lqr.adapter.LQRViewHolder;
import com.lqr.adapter.LQRViewHolderForRecyclerView;
import com.lqr.adapter.OnItemClickListener;
import com.lqr.audio.AudioPlayManager;
import com.lqr.audio.AudioRecordManager;
import com.lqr.audio.IAudioPlayListener;
import com.lqr.audio.IAudioRecordListener;
import com.lqr.recyclerview.LQRRecyclerView;
import net.micode.notes.R;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Optional;
import kr.co.namee.permissiongen.PermissionGen;
/*
录音主程序
*/
public class MainActivity extends AppCompatActivity {
private static final int MAX_VOICE_TIME = 20;
private static final String AUDIO_DIR_NAME = "community_audio";
//录音背景
@BindView(R.id.root)
LinearLayout mRoot;
//放置录音项的背景
@BindView(R.id.rvMsg)
LQRRecyclerView mRvMsg;
//录音按钮
@BindView(R.id.btnVoice)
RecordAudioButton mBtnVoice;
private Context mContext;
private File mAudioDir;
private LQRAdapterForRecyclerView<File> mAdapter;
private RecordVoicePopWindow mRecordVoicePopWindow;
private List<File> mData = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
request();
initVoice();
initData();
}
//开放权限
private void request() {
PermissionGen.with(this)
.addRequestCode(100)
.permissions(Manifest.permission.RECORD_AUDIO
, Manifest.permission.WRITE_EXTERNAL_STORAGE
, Manifest.permission.WAKE_LOCK
, Manifest.permission.READ_EXTERNAL_STORAGE)
.request();
}
//初始化声音
private void initVoice() {
mAudioDir = new File(Environment.getExternalStorageDirectory(), AUDIO_DIR_NAME);
if (!mAudioDir.exists()) {
mAudioDir.mkdirs();
}
//利用AudioRecordManager类获取设置录音位置
AudioRecordManager.getInstance(mContext).setAudioSavePath(mAudioDir.getAbsolutePath());
//利用AudioRecordManager类设置最大录音时间
AudioRecordManager.getInstance(this).setMaxVoiceDuration(MAX_VOICE_TIME);
//实现按钮录音
mBtnVoice.setOnVoiceButtonCallBack(new RecordAudioButton.OnVoiceButtonCallBack() {
@Override
//开始录音
public void onStartRecord() {
AudioRecordManager.getInstance(mContext).startRecord();
}
@Override
//停止录音
public void onStopRecord() {
AudioRecordManager.getInstance(mContext).stopRecord();
}
@Override
//将要取消录音
public void onWillCancelRecord() {
AudioRecordManager.getInstance(mContext).willCancelRecord();
}
//继续录音
@Override
public void onContinueRecord() {
AudioRecordManager.getInstance(mContext).continueRecord();
}
});
AudioRecordManager.getInstance(this).setAudioRecordListener(new IAudioRecordListener() {
@Override
//初始化提示视图
public void initTipView() {
if (mRecordVoicePopWindow == null) {
mRecordVoicePopWindow = new RecordVoicePopWindow(mContext);
}
mRecordVoicePopWindow.showAsDropDown(mRoot);
}
//设置超时提示视图
@Override
public void setTimeoutTipView(int counter) {
if (mRecordVoicePopWindow != null) {
mRecordVoicePopWindow.showTimeOutTipView(counter);
}
}
//设置记录视图
@Override
public void setRecordingTipView() {
if (mRecordVoicePopWindow != null) {
mRecordVoicePopWindow.showRecordingTipView();
}
}
//设置录音过短视图
@Override
public void setAudioShortTipView() {
if (mRecordVoicePopWindow != null) {
mRecordVoicePopWindow.showRecordTooShortTipView();
}
}
//设置取消录音视图
@Override
public void setCancelTipView() {
if (mRecordVoicePopWindow != null) {
mRecordVoicePopWindow.showCancelTipView();
}
}
//取消提示视图
@Override
public void destroyTipView() {
if (mRecordVoicePopWindow != null) {
mRecordVoicePopWindow.dismiss();
}
}
@Override
public void onStartRecord() {
}
@Override
public void onFinish(Uri audioPath, int duration) {
File file = new File(audioPath.getPath());
if (file.exists()) {
Toast.makeText(getApplicationContext(), "录制成功", Toast.LENGTH_SHORT).show();
loadData();
}
}
@Override
public void onAudioDBChanged(int db) {
if (mRecordVoicePopWindow != null) {
mRecordVoicePopWindow.updateCurrentVolume(db);
}
}
});
}
private void initData() {
loadData();
setAdapter();
}
private void loadData() {
if (mAudioDir.exists()) {
mData.clear();
File[] files = mAudioDir.listFiles();
for (File file : files) {
if (file.getAbsolutePath().endsWith("voice")) {
mData.add(file);
}
}
setAdapter();
}
}
//设置适配器
private void setAdapter() {
if (mAdapter == null) {
mAdapter = new LQRAdapterForRecyclerView<File>(this, mData, R.layout.community_adapter_chat_list_right_voice) {
@Override
public void convert(LQRViewHolderForRecyclerView helper, File item, int position) {
//这里就不考虑语音长度了实际开发中用到的Sdk有提供保存语音信息的bean
}
};
mAdapter.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(LQRViewHolder helper, ViewGroup parent, View itemView, int position) {
//AudioPlayManager类停止播放
AudioPlayManager.getInstance().stopPlay();
File item = mData.get(position);
final VoiceImageView ivAudio = helper.getView(R.id.iv_voice);
Uri audioUri = Uri.fromFile(item);
Log.e("LQR", audioUri.toString());
//AudioPlayManager类开始播放
AudioPlayManager.getInstance().startPlay(mContext, audioUri, new IAudioPlayListener() {
@Override
public void onStart(Uri var1) {
ivAudio.startPlay();
}
@Override
public void onStop(Uri var1) {
ivAudio.stopPlay();
}
@Override
public void onComplete(Uri var1) {
ivAudio.stopPlay();
}
});
}
});
/*设置一个新的适配器以根据需要提供子视图
当适配器被更改时所有现有的视图都会被回收回池中
如果池中有只有一个适配器它将被清除
*/
mRvMsg.setAdapter(mAdapter);
} else
{
//刷新数据
mAdapter.notifyDataSetChangedWrapper();
}
}
}

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.lqr.recyclerview.LQRRecyclerView
android:id="@+id/rvMsg"
android:layout_width="match_parent"
android:layout_height="530dp" />
<net.micode.notes.audiodemo.RecordAudioButton
android:id="@+id/btnVoice"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="10dp"
android:background="@drawable/community_btn_chat_press_normal"
android:text="按住 说话"
android:textColor="#7e7e7e" />
</LinearLayout>