Android一个类实现录音与播放实例
目录
- 前言
- 实现效果
- 代码实现
- XML
- 静态权限
- 动态权限
- 总结
前言
最近混合开发的项目 在做语音识别时 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实现录音与播放内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)