Android studio 在android studio中通过录音机检测语音

Android studio 在android studio中通过录音机检测语音,android-studio,detect,voice,audiorecord,Android Studio,Detect,Voice,Audiorecord,嗯,我想实现一个功能,比如当应用程序启动时,记录器将开始录制,当用户保持沉默时,在用户说话之前不会发生任何事情。然后,它将保存用户语音的PCM文件,然后停止录制 以上是我发现的与我类似的问题,但是这个链接的答案不起作用。我不知道如何修改它,因为我不理解代码的概念 请帮帮我~好吧,我解决了我的问题,这是我的解决方案。 我修改了来自此url的代码: private static final String TAG=“MainActivity”; 专用静态int记录仪\u采样器=44100; 专用静

嗯,我想实现一个功能,比如当应用程序启动时,记录器将开始录制,当用户保持沉默时,在用户说话之前不会发生任何事情。然后,它将保存用户语音的PCM文件,然后停止录制

以上是我发现的与我类似的问题,但是这个链接的答案不起作用。我不知道如何修改它,因为我不理解代码的概念


请帮帮我~

好吧,我解决了我的问题,这是我的解决方案。 我修改了来自此url的代码:

private static final String TAG=“MainActivity”;
专用静态int记录仪\u采样器=44100;
专用静态int-RECORDER_CHANNELS=立体声中的AudioFormat.CHANNEL_;
专用静态整数记录器\u音频\u编码=AudioFormat.ENCODING\u PCM\u 16位;
专用按钮btn、btn\U转换、btn\U播放;
私用TextView热膨胀阀;
布尔值isRecording=false;
私有文件;
私人录音;
int bufferSizeInBytes=0;
Context=MainActivity.this;
//路径
最终字符串路径=Environment.getExternalStorageDirectory().getAbsolutePath()+“/final.pcm”;
最终字符串输出路径=path.replace(“.pcm”和“.wav”);
公共无效自动记录(){
//获取成功创建录音对象所需的最小缓冲区大小。
bufferSizeInBytes=AudioRecord.getMinBufferSize(记录器\u采样器,
录像机(u)频道,,
录音机\u音频\u编码
);
//初始化录音机。
AudioRecord audioRecorder=新的录音(MediaRecorder.AudioSource.MIC,
采样器,
录像机(u)频道,,
录音机\u音频\u编码,
缓冲区大小单位
);
//开始录音。
txv.setText(简称“Ing”);
录音机。开始录音();
isRecording=true;
//用于自动停止
int numberOfReadBytes=0;
字节audioBuffer[]=新字节[bufferSizeInBytes];
布尔记录=假;
float tempFloatBuffer[]=新浮点[3];
int tempIndex=0;
//创建文件
file=新文件(Environment.getExternalStorageDirectory().getAbsolutePath()+“/final.pcm”);
Log.d(标记“录制:文件路径:”+file.toString());
if(file.exists()){
Log.d(标记“文件存在,删除文件”);
delete();
}
试一试{
Log.d(标记“文件已创建”);
createNewFile();
}捕获(IOE异常){
Log.d(标记“未创建文件:”+e.getMessage());
抛出新的IllegalStateException(“未创建文件:+file.toString());
}
//启动媒体扫描,并将新内容放入路径阵列中,以
//让扫描仪知道您要查看的位置和文件
MediaScannerConnection.scanFile(上下文,新字符串[]{file.toString()},null,null);
//输出流
OutputStream os=null;
DataOutputStream dos=null;
试一试{
os=新文件输出流(文件);
BufferedOutputStream bos=新的BufferedOutputStream(os);
dos=新数据输出流(bos);
}catch(filenotfounde异常){
e、 printStackTrace();
}
//而数据来自麦克风。
while(true)
{
浮点值=0.0f;
短样本=0;
numberOfReadBytes=audioRecorder.read(音频缓冲区,0,缓冲区大小为字节);
//分析声音。
对于(int i=0;i
private static final String TAG = "MainActivity";


private static int RECORDER_SAMPLERATE = 44100;
private static int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_STEREO;
private static int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;

private Button btn, btn_convert, btn_play;
private TextView txv;

boolean isRecording = false;
private File file;
private AudioRecord audioRecord;
int bufferSizeInBytes = 0;
Context context = MainActivity.this;

// path
final String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/final.pcm" ;
final String outpath = path.replace(".pcm", ".wav");

public void autoRecording(){
    // Get the minimum buffer size required for the successful creation of an AudioRecord object.
    bufferSizeInBytes = AudioRecord.getMinBufferSize( RECORDER_SAMPLERATE,
            RECORDER_CHANNELS,
            RECORDER_AUDIO_ENCODING
    );
    // Initialize Audio Recorder.
    AudioRecord audioRecorder = new AudioRecord( MediaRecorder.AudioSource.MIC,
            RECORDER_SAMPLERATE,
            RECORDER_CHANNELS,
            RECORDER_AUDIO_ENCODING,
            bufferSizeInBytes
    );
    // Start Recording.
    txv.setText("Ing");
    audioRecorder.startRecording();
    isRecording = true;

    // for auto stop
    int numberOfReadBytes   = 0;
    byte audioBuffer[]      = new  byte[bufferSizeInBytes];
    boolean recording       = false;
    float tempFloatBuffer[] = new float[3];
    int tempIndex           = 0;

    // create file

    file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/final.pcm");
    Log.d(TAG, "recording: file path:" + file.toString());

    if (file.exists()){
        Log.d(TAG,"file exist, delete file");
        file.delete();
    }
    try {
        Log.d(TAG,"file created");
        file.createNewFile();
    } catch (IOException e) {
        Log.d(TAG,"didn't create the file:" + e.getMessage());
        throw new IllegalStateException("did not create file:" + file.toString());
    }

    // initiate media scan and put the new things into the path array to
    // make the scanner aware of the location and the files you want to see
    MediaScannerConnection.scanFile(context, new String[] {file.toString()}, null, null);

    // output stream
    OutputStream os = null;
    DataOutputStream dos = null;
    try {
        os = new FileOutputStream(file);
        BufferedOutputStream bos = new BufferedOutputStream(os);
        dos = new DataOutputStream(bos);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }


    // While data come from microphone.
    while( true )
    {
        float totalAbsValue = 0.0f;
        short sample        = 0;

        numberOfReadBytes = audioRecorder.read( audioBuffer, 0, bufferSizeInBytes );

        // Analyze Sound.
        for( int i=0; i<bufferSizeInBytes; i+=2 )
        {
            sample = (short)( (audioBuffer[i]) | audioBuffer[i + 1] << 8 );
            totalAbsValue += (float)Math.abs( sample ) / ((float)numberOfReadBytes/(float)2);
        }

        // read in file
        for (int i = 0; i < numberOfReadBytes; i++) {
            try {
                dos.writeByte(audioBuffer[i]);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // Analyze temp buffer.
        tempFloatBuffer[tempIndex%3] = totalAbsValue;
        float temp                   = 0.0f;
        for( int i=0; i<3; ++i )
            temp += tempFloatBuffer[i];

        if( (temp >=0 && temp <= 2100) && recording == false )  // the best number for close to device: 3000
        {                                                       // the best number for a little bit distance : 2100
            Log.i("TAG", "1");
            tempIndex++;
            continue;
        }

        if( temp > 2100 && recording == false )
        {
            Log.i("TAG", "2");
            recording = true;
        }

        if( (temp >= 0 && temp <= 2100) && recording == true )
        {

            Log.i("TAG", "final run");
            //isRecording = false;

            txv.setText("Stop Record.");
            //*/
            tempIndex++;
            audioRecorder.stop();
            try {
                dos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
        }
    }
}