Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MediaRecorder以循环方式录制音频_Java_Android_Mediarecorder - Fatal编程技术网

Java MediaRecorder以循环方式录制音频

Java MediaRecorder以循环方式录制音频,java,android,mediarecorder,Java,Android,Mediarecorder,我正在开发一个声音识别系统。我正在使用python上开发的tensorflow模型将MFCC值转换为标签。我正在使用MediaRecorder类来录制音频,我在循环中这样做,这样我就可以不断地获取麦克风音频,然后从模型中获取标签。以下是记录循环: temp = 0; while (true) { audioPath = getApplicationContext().getFilesDir().getAbsolutePath(); audioPath +=

我正在开发一个声音识别系统。我正在使用python上开发的tensorflow模型将MFCC值转换为标签。我正在使用MediaRecorder类来录制音频,我在循环中这样做,这样我就可以不断地获取麦克风音频,然后从模型中获取标签。以下是记录循环:

temp = 0;
    while (true) {
        audioPath = getApplicationContext().getFilesDir().getAbsolutePath();
        audioPath += "/Recording" + temp + ".3gp";

        audioFile = new File(audioPath);
        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mediaRecorder.setOutputFile(audioPath);
        try {
            mediaRecorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mediaRecorder.start();
        sleep(2000);
        if (!isRunning) {
            mediaRecorder.stop();
            return;
        }
        try {
            int amplitude = mediaRecorder.getMaxAmplitude();
            Log.d("volume", Integer.toString(amplitude));
            //finished = false;
            avgVolumeTask task = new avgVolumeTask();
            task.execute(amplitude);
        } catch (Exception e) {
            Log.d("Exception in startMediaRecorder()", e.toString());
        }
        mediaRecorder.stop();
        mediaRecorder.release();

        soundRecognition task2 = new soundRecognition();
        task2.execute();
        audioFile.delete();
        temp++;
    }
这是声音识别方法:

private class soundRecognition extends AsyncTask<Integer, Integer, Long> {
    @Override
    protected Long doInBackground(Integer... level) {
        float[] mfccValues = null;
        Interpreter tflite = null;
        float[][] labelProbArray = null;
        try {
            mfccValues = computeMFCC();
            labelList = loadLabelList();
            labelProbArray = new float[1][labelList.size()];
            tflite = new Interpreter(loadModel());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        }
        tflite.run(mfccValues, labelProbArray);

        for (int i = 0; i < labelProbArray[0].length; i++) {
            float value = labelProbArray[0][i];
            //if (i == 1f){
                //Log.d("Output at " + Integer.toString(i) + ": ", Float.toString(value));
                //doAlert(i);
            //}
        }

        return null;
    }
}
私有类声音识别扩展异步任务{
@凌驾
受保护的长doInBackground(整数…级别){
float[]mfccValues=null;
解释器tflite=null;
float[]labelProbArray=null;
试一试{
mfccValues=computeMFCC();
labelList=loadLabelList();
labelProbArray=新浮点[1][labelList.size()];
tflite=新的解释器(loadModel());
}捕获(IOE异常){
e、 printStackTrace();
}捕获(不支持的数据文件异常e){
e、 printStackTrace();
}
运行(mfccValues,labelProbArray);
对于(int i=0;i
computeMFCC方法如下所示:

public float[] computeMFCC() throws IOException, UnsupportedAudioFileException {

    FileInputStream in2 = new FileInputStream(audioPath);
    int i;
    // InputStream to byte array
    byte[] buf = IOUtils.toByteArray(in2);
    in2.close();
    i = Integer.MAX_VALUE;

    // byte array to short array
    short[] shortArr = new short[buf.length / 2];
    ByteBuffer.wrap(buf).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shortArr);

    int count = 0;
    while (count <= shortArr.length) {                    // Still have data to process.
        for (int n = 0; n < nSubframePerBuf; n++) {            // Process audio signal in ArrayList and shift by one subframe each time
            int k = 0;
            for (i = (n * frameShift); i < (n + 1) * frameShift; i++) {
                subx[k] = shortArr[i];
                k++;
            }
            subframeList.add(subx);                            // Add the current subframe to the subframe list. Later, a number of
        }
        count++;
    }
    // Need at least nSubframePerMfccFrame to get one analysis frame
    x = extractOneFrameFromList(nSubframePerMfccFrame);

    MFCC mfcc = new MFCC(samplePerFrm, 16000, numMfcc);
    double[] mfccVals = mfcc.doMFCC(x);
    float[] floatArray = new float[mfccVals.length];
    for (i = 0 ; i < mfccVals.length; i++)
    {
        floatArray[i] = (float) mfccVals[i];
    }
    return floatArray;
}
public float[]computeMFCC()引发IOException,UnsupportedAudioFileException{
FileInputStream in2=新的FileInputStream(音频路径);
int i;
//InputStream到字节数组
字节[]buf=IOUtils.toByteArray(in2);
in2.close();
i=整数。最大值;
//字节数组到短数组
short[]shortArr=新的short[buf.length/2];
ByteBuffer.wrap(buf).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shortArr);
整数计数=0;

while(count在while循环中进行睡眠(2000)可能很棘手。 最好是检查毫秒,直到2000毫秒过去