最精简的android录音app

本文重度参考了此文,只是

1、将线性布局改为相对布局

2、将原文播放录音的自定义类改为安卓自带MediaPlayer而已

3、加上运行效果截图敲打


main.xml(布局文件)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".RecordVoice">

    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="start" />
    <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="stop" />
    <Button
        android:id="@+id/play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/start"
        android:layout_centerHorizontal="true"
        android:text="play" />

</RelativeLayout>

RecordVoice (具体录音实现)

package com.xxgroup.xx.speechclient;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import java.io.File;
import java.io.IOException;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.media.MediaRecorder;
import android.media.MediaPlayer;


public class RecordVoice extends ActionBarActivity implements OnClickListener {

    private Button btnStart;
    private Button btnStop;
    private Button btnPlay;

    private File recAudioFile;
    private MediaRecorder mRecorder;
    private MediaPlayer mPlayer;
    private static final String mSpeechPath = "/sdcard/RunJi/speech.amr";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record_voice);

        btnStart = (Button) findViewById(R.id.start);
        btnStop = (Button) findViewById(R.id.stop);
        btnPlay = (Button) findViewById(R.id.play);

        btnStart.setOnClickListener(this);
        btnStop.setOnClickListener(this);
        btnPlay.setOnClickListener(this);

        recAudioFile = new File(mSpeechPath);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start:
                startRecorder();
                break;
            case R.id.stop:
                stopRecorder();
                break;
            case R.id.play:
                playRecord();
                break;
            default:
                break;
        }
    }
    private void startRecorder() {
        mRecorder = new MediaRecorder();
        if (recAudioFile.exists()) {
            recAudioFile.delete();
        }

        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        mRecorder.setOutputFile(recAudioFile.getAbsolutePath());
        try {
            mRecorder.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        mRecorder.start();
    }

    private void stopRecorder(){
        if (recAudioFile!=null) {
            mRecorder.stop();
            mRecorder.release();
        }
    }

    private void playRecord(){
        mPlayer = new MediaPlayer();
        if (recAudioFile!=null) {
            try {
                mPlayer.setDataSource(mSpeechPath);
                mPlayer.prepare();
            }catch (IOException e){
                e.printStackTrace();
            }
            mPlayer.start();
        }
    }
}
在AndroidManifest.xml里 添加录音权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xxgroup.xx.speechclient" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".RecordVoice"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

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

</manifest>

运行效果图



好的,下面是一个简单的录音APP的实现步骤: 1. 在 Android Studio 中创建一个空白项目,并在布局文件中添加一个按钮和一个文本框用于显示录音时长。 2. 添加权限 在 AndroidManifest.xml 文件中添加以下权限: ``` <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ``` 3. 创建录音机类 创建一个名为 `SoundRecorder` 的 Java 类,该类用于管理录音的开始、停止、暂停和继续等操作。 ```java public class SoundRecorder { private MediaRecorder mRecorder; private String mFileName; private long mStartTime; public SoundRecorder(String fileName) { mFileName = fileName; mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mRecorder.setOutputFile(mFileName); } public void start() { try { mRecorder.prepare(); mRecorder.start(); mStartTime = System.currentTimeMillis(); } catch (IOException e) { Log.e("SoundRecorder", "prepare() failed"); } } public void pause() { mRecorder.pause(); } public void resume() { mRecorder.resume(); } public void stop() { mRecorder.stop(); mRecorder.release(); mRecorder = null; } public int getDuration() { return (int) (System.currentTimeMillis() - mStartTime) / 1000; } } ``` 4. 在 Activity 中调用录音机类 在 Activity 中创建一个 `SoundRecorder` 实例,并在按钮的点击事件中调用录音机的开始、停止、暂停和继续等方法,并在文本框中显示录音时长。 ```java public class MainActivity extends AppCompatActivity { private Button mRecordButton; private TextView mDurationTextView; private SoundRecorder mRecorder; private boolean mIsRecording = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRecordButton = findViewById(R.id.record_button); mDurationTextView = findViewById(R.id.duration_text_view); mRecordButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mIsRecording) { mRecorder.stop(); mIsRecording = false; mRecordButton.setText("Record"); } else { mRecorder = new SoundRecorder(getExternalCacheDir().getAbsolutePath() + "/recording.3gp"); mRecorder.start(); mIsRecording = true; mRecordButton.setText("Stop"); new CountDownTimer(Long.MAX_VALUE, 1000) { @Override public void onTick(long millisUntilFinished) { mDurationTextView.setText(String.format("%02d:%02d", mRecorder.getDuration() / 60, mRecorder.getDuration() % 60)); } @Override public void onFinish() { } }.start(); } } }); } } ``` 这样你就可以在 Android Studio 中创建一个简单的录音APP了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值