大家好,本篇文章主要讲的是Android一个类实现录音与播放实例,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
前言
最近混合开发的项目 在做语音识别时 h5拿不到麦克风的权限
几经周折 开发任务又落到了原生开发这里
先写一个demo实现录音和播放功能 然后由web端同事调用交互方法
实现效果
代码实现
public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG = "MainActivity";
//语音文件保存路径
private String FileName = null;
//界面控件
private Button startRecord;
private Button startPlay;
private Button stopRecord;
private Button stopPlay;
//语音操作对象
private MediaPlayer mPlayer = null;
private MediaRecorder mRecorder = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermission();//请求麦克风权限
//开始录音
startRecord = findViewById(R.id.startRecord);
//绑定监听器
startRecord.setOnClickListener(new startRecordListener());
//结束录音
stopRecord = findViewById(R.id.stopRecord);
stopRecord.setOnClickListener(new stopRecordListener());
//开始播放
startPlay = findViewById(R.id.startPlay);
//绑定监听器
startPlay.setOnClickListener(new startPlayListener());
//结束播放
stopPlay = findViewById(R.id.stopPlay);
stopPlay.setOnClickListener(new stopPlayListener());
//设置sdcard的路径
FileName = Environment.getExternalStorageDirectory().getAbsolutePath();
FileName += "/test.mp3";
}
private void requestPermission() {
PermissionGen.with(this)
.addRequestCode(100)
.permissions(Manifest.permission.RECORD_AUDIO,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.WAKE_LOCK)
.request();
}
//开始录音
class startRecordListener implements View.OnClickListener {
@Override
public void onClick(View v) {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(FileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
mRecorder.start();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed ---" + e.getMessage());
}
}
}
//停止录音
class stopRecordListener implements View.OnClickListener {
@Override
public void onClick(View v) {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
}
//播放录音
class startPlayListener implements View.OnClickListener {
@Override
public void onClick(View v) {
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(FileName);
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
Log.e(LOG_TAG, "播放失败");
}
}
}
//停止播放录音
class stopPlayListener implements View.OnClickListener {
@Override
public void onClick(View v) {
mPlayer.release();
mPlayer = null;
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/startRecord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="100dp"
android:text="开始录音"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/stopRecord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="结束录音"
app:layout_constraintBottom_toTopOf="@id/startPlay"
app:layout_constraintTop_toBottomOf="@id/startRecord" />
<Button
android:id="@+id/startPlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="开始播放"
app:layout_constraintBottom_toTopOf="@id/stopPlay"
app:layout_constraintTop_toBottomOf="@id/stopRecord" />
<Button
android:id="@+id/stopPlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="结束播放"
app:layout_constraintTop_toBottomOf="@id/startPlay" />
</androidx.constraintlayout.widget.ConstraintLayout>
静态权限
<!--录音和写磁盘权限-->
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
动态权限
PermissionGen.with(this)
.addRequestCode(100)
.permissions(Manifest.permission.RECORD_AUDIO,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.WAKE_LOCK)
.request();
总结
到此这篇关于Android一个类实现录音与播放实例的文章就介绍到这了,更多相关Android实现录音与播放内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
沃梦达教程
本文标题为:Android一个类实现录音与播放实例


基础教程推荐
猜你喜欢
- Android Compose自定义TextField实现自定义的输入框 2023-05-13
- iOS Crash常规跟踪方法及Bugly集成运用详细介绍 2023-01-18
- IOS获取系统相册中照片的示例代码 2023-01-03
- Flutter进阶之实现动画效果(三) 2022-10-28
- iOS开发 全机型适配解决方法 2023-01-14
- iOS开发使用XML解析网络数据 2022-11-12
- iOS中如何判断当前网络环境是2G/3G/4G/5G/WiFi 2023-06-18
- MVVMLight项目Model View结构及全局视图模型注入器 2023-05-07
- Android实现短信验证码输入框 2023-04-29
- Android开发Compose集成高德地图实例 2023-06-15