0

使用 Tarsos 提供的 WriterProcessor 时无法写入文件。该文件存在,并且没有显示错误。但是文件是空的,播放的时候没有声音。

    final Float srf = 44100.0F;

    dispatcher = AudioDispatcherFactory.fromPipe(mRecordFile.getPath(), srf.intValue(), bufferSize, 0);

    mOutputLowFile = new File(v.getContext().getFilesDir(), mOutputLowFileName);
    RandomAccessFile outputFile = null;
    try {
        outputFile = new RandomAccessFile( mOutputLowFile, "rw");
    } catch (FileNotFoundException e) {
        Log.i(TAG, "onFFT: FileNotFoundException: " + e);
        e.printStackTrace();
    }
    TarsosDSPAudioFormat outputFormat = new TarsosDSPAudioFormat(44100, 16, 1, true, false);
    WriterProcessor writer = new WriterProcessor(outputFormat, outputFile);

    dispatcher.addAudioProcessor(writer);

    final AudioDispatcher finalDispatcher1 = dispatcher;
    Thread recordingThread = new Thread(new Runnable() {
        @Override
        public void run() {
            finalDispatcher1.run();
        }
    }, "recordingThread Thread");

    recordingThread.start();

outputFile 是 1Ko 大,而原始文件是 3 Ko。

4

1 回答 1

1

这是我正在使用的代码,它可以工作 - 在管道中我还放置了一个 PitchProcessor(此代码主要来自https://stackoverflow.com/a/45867328/4872992):

dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(SAMPLE_RATE, 1024, 0);
    AudioProcessor p = new PitchProcessor(PitchProcessor.PitchEstimationAlgorithm.FFT_YIN, 22050, 1024, pdh);
    dispatcher.addAudioProcessor(p);
    filePath = "/sdcard/recording_test.pcm";
    RandomAccessFile outputFile = new RandomAccessFile(filePath, "rw");
    TarsosDSPAudioFormat outputFormat = new TarsosDSPAudioFormat(SAMPLE_RATE, 16, 1, true, false);
    WriterProcessor writer = new WriterProcessor(outputFormat, outputFile);
    dispatcher.addAudioProcessor(writer);
    recordingThread = new Thread(dispatcher, "Audio Dispatcher)");
    recordingThread.start();
于 2019-07-31T02:57:49.490 回答