android 简单实现录音功能

发布于:2024-12-06 ⋅ 阅读:(32) ⋅ 点赞:(0)
  1. 一.xml页面布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <!-- 显示数字 "1" 的 TextView -->
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="1"
        android:textSize="24sp"
        android:gravity="center"
        android:paddingBottom="16dp"/>

    <!-- 开始录音按钮 -->
    <Button
        android:id="@+id/startRecord"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="开始录音"
        android:textSize="18sp"
        android:padding="12dp"
        android:layout_marginBottom="8dp"/>

    <!-- 停止录音按钮 -->
    <Button
        android:id="@+id/stopRecord"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="停止录音"
        android:textSize="18sp"
        android:padding="12dp"
        android:layout_marginBottom="8dp"/>

    <!-- 开始播放按钮 -->
    <Button
        android:id="@+id/startPlay"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="开始播放"
        android:textSize="18sp"
        android:padding="12dp"
        android:layout_marginBottom="8dp"/>

    <!-- 停止播放按钮 -->
    <Button
        android:id="@+id/stopPlay"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="停止播放"
        android:textSize="18sp"
        android:padding="12dp"
        android:layout_marginBottom="8dp"/>

</LinearLayout>
  1. 2.对应的java页面
  2. 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
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // 初始化控件
            startRecord = findViewById(R.id.startRecord);
            startPlay = findViewById(R.id.startPlay);
            stopRecord = findViewById(R.id.stopRecord);
            stopPlay = findViewById(R.id.stopPlay);
    
            // 设置按钮监听器
            startRecord.setOnClickListener(new startRecordListener());
            stopRecord.setOnClickListener(new stopRecordListener());
            startPlay.setOnClickListener(new startPlayListener());
            stopPlay.setOnClickListener(new stopPlayListener());
    
            // 设置语音文件保存路径
            FileName = getExternalFilesDir(Environment.DIRECTORY_MUSIC).getAbsolutePath() + "/audiorecordtest.3gp";
    
            // 请求权限
            requestPermissions();
        }
    
        // 请求录音和存储权限
        private void requestPermissions() {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED
                    || ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
    
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        1);
            }
        }
    
        // 开始录音
        class startRecordListener implements View.OnClickListener {
            @Override
            public void onClick(View v) {
                // 检查权限
                if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
                    Log.e(LOG_TAG, "没有录音权限!");
                    return;
                }
    
                mRecorder = new MediaRecorder();
                try {
                    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                    mRecorder.setOutputFile(FileName);
                    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                    mRecorder.prepare();
                    mRecorder.start();
                } catch (IOException e) {
                    Log.e(LOG_TAG, "录音准备或启动失败:" + e.getMessage());
                }
            }
        }
    
        // 停止录音
        class stopRecordListener implements View.OnClickListener {
            @Override
            public void onClick(View v) {
                if (mRecorder != null) {
                    try {
                        mRecorder.stop();
                    } catch (RuntimeException e) {
                        Log.e(LOG_TAG, "停止录音失败:" + e.getMessage());
                    } finally {
                        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, "播放失败:" + e.getMessage());
                }
            }
        }
    
        // 停止播放
        class stopPlayListener implements View.OnClickListener {
            @Override
            public void onClick(View v) {
                if (mPlayer != null) {
                    mPlayer.release();
                    mPlayer = null;
                }
            }
        }
    
        @Override
        public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    
            if (requestCode == 1) {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.d(LOG_TAG, "权限已授予");
                } else {
                    Log.e(LOG_TAG, "权限被拒绝");
                }
            }
        }
    }最后不要忘记加对应的请求权限
    

网站公告

今日签到

点亮在社区的每一天
去签到