Android—-录音系列教程

释放双眼,带上耳机,听听看~!

在github上试水,找到个录音功能,亲测可用,分享给大家

话不多少,直接贴代码

第一步:

/**录音**/
implementation 'com.github.adrielcafe:AndroidAudioRecorder:0.3.0'
/**权限检测**/
implementation 'com.yanzhenjie:permission:2.0.0-rc12'
/**弹出对话框**/
implementation 'com.yanzhenjie.alertdialog:alertdialog:1.0.1'

引入上方依赖

maven不能忘记配置,切这里用了1.8的表达式,也配置一下

第二步:

<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />

权限配置这3项

第三步:

package com.example.administrator.audiorecorder;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.yanzhenjie.alertdialog.AlertDialog;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RequestExecutor;
import com.yanzhenjie.permission.Setting;
import java.util.List;
import cafe.adriel.androidaudiorecorder.AndroidAudioRecorder;
import cafe.adriel.androidaudiorecorder.model.AudioChannel;
import cafe.adriel.androidaudiorecorder.model.AudioSampleRate;
import cafe.adriel.androidaudiorecorder.model.AudioSource;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestFilePermission();
}
/**
* 第三方权限方法
**/
private void requestFilePermission() {
requestPermission(this,  Permission.WRITE_EXTERNAL_STORAGE,
Permission.RECORD_AUDIO,Permission.WRITE_EXTERNAL_STORAGE);
}
public void requestPermission(final Activity activity,  final String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
AndPermission.with(activity)
.runtime()
.permission(permissions)
.rationale(new Rationale<List<String>>() {
@Override
public void showRationale(Context context, List<String> data, final RequestExecutor executor) {
List<String> permissionNames = Permission.transformText(context, data);
String message = "请授权该下的权限" + "n" + permissionNames;
AlertDialog.newBuilder(context)
.setCancelable(false)
.setTitle("开启权限")
.setMessage(message)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
executor.execute();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
executor.cancel();
}
})
.show();
}
}).onGranted(new Action<List<String>>() {
@Override
public void onAction(List<String> data) {
doOther();
}
})
.onDenied(data -> {
if (AndPermission.hasAlwaysDeniedPermission(activity, permissions)) {
AndPermission.with(activity)
.runtime()
.setting()
.onComeback(new Setting.Action() {
@Override
public void onAction() {
if (AndPermission.hasPermissions(activity, permissions)) {
doOther();
}
}
})
.start();
}
})
.start();
} else {
doOther();
}
}
private void doOther(){
String filePath = Environment.getExternalStorageDirectory() + "/recorded_audio.wav";
int color = getResources().getColor(R.color.colorPrimaryDark);
int requestCode = 0;
AndroidAudioRecorder.with(this)
// Required
.setFilePath(filePath)
.setColor(color)
.setRequestCode(requestCode)
// Optional
.setSource(AudioSource.MIC)
.setChannel(AudioChannel.STEREO)
.setSampleRate(AudioSampleRate.HZ_48000)
.setAutoStart(true)
.setKeepDisplayOn(true)
// Start recording
.record();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
// Great! User has recorded and saved the audio file
} else if (resultCode == RESULT_CANCELED) {
// Oops! User has canceled the recording
}
}
}
}

上面是主界面代码。 逻辑是初始化之后,执行动态权限获取,然后一路通过后,跳转到路音界面

布局里什么都不用配置。

 

人已赞赏
Android文章

Android----天女散花系列教程

2020-3-19 20:38:41

Android文章

svn: E720003: Unable to create pristine install stream

2020-3-19 21:02:25

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索