如何让手机的代码更容易?

时间:2016-11-23 16:55:51

标签: android

我正在尝试录制音频。不幸的是,代码非常密集,以至于手机在录制时会跳过样本。有没有人对如何简化代码并使处理器更容易有任何建议?我希望得到帮助的一点是将for循环简化为更简单的东西。

recordingThread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");

                try {
                    file.createNewFile();

                    OutputStream outputStream = new FileOutputStream(file);
                    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
                    DataOutputStream dataOutputStream = new DataOutputStream(bufferedOutputStream);

                    short[] audioData = new short[bufferSize];

                    audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                            RECORDER_SAMPLERATE,
                            RECORDER_CHANNELS,
                            RECORDER_AUDIO_ENCODING,
                            bufferSize);

                    audioRecord.startRecording();

                    recordingThread = new Thread(new Runnable() {

                        @Override
                        public void run() {
                            writeAudioDataToFile();
                        }
                    }, "AudioRecorder Thread");

                    recordingThread.start();

                    while (!recordbuttonstatus) {
                        int numberOfShort = audioRecord.read(audioData, 0, bufferSize);

                        for (int i = 0; i < numberOfShort; i++) {
                            dataOutputStream.writeShort(audioData[i]);
                        }
                    }

                    dataOutputStream.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
                recordingThread2.start();

0 个答案:

没有答案