Z
Z
Zohei2017-09-30 18:43:37
Java
Zohei, 2017-09-30 18:43:37

Android AudioRecord. How to properly split an audio recording into chunks of 30 minutes?

Greetings.
I make a delayed call to the recording stop method, after which I start the recording again.
The problem is that then the forced stop of the recording does not work. The recorder continues to write on.

public void startRecording() {
        recorder.startRecording();
        isRecording = true;

        recordingThread = new Thread(new Runnable() {
            @Override
            public void run() {
                writeDataToFile();
            }
        }, "AudioRecorder Thread");
        recordingThread.start();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                stopRecording();

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        startRecording();
                    }
                }, 100);
            }
        }, 1000*60*30); //11s
    }

private void writeDataToFile() {
.....
            while (isRecording) {
                read = recorder.read(data, 0, bufferSize);
            }
            os.close();
    }

public void stopRecording() {
        if (null != recorder) {
            isRecording = false;
            recorder.stop();
            recordingThread = null;
        }
        copyWaveFile(tempFilename, fileName);
        deleteTempFile();
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
Zohei, 2017-10-01
@Zohei

figured it out. I ’ll answer myself ....
FAudioRecord has an onPeriodicNotification method that fires every n frames /

public void startRecording() {
        if (null == recorder) {
            InitRecord();
        }
        recorder.startRecording();
        isRecording = true;
        recordingThread = new Thread(new Runnable() {
            @Override
            public void run() {
                writeDataToTempFile();
            }
        }, "AudioRecorder Thread");
        recordingThread.start();
    }

recorder.setPositionNotificationPeriod(10000000); //20mb
        recorder.setRecordPositionUpdateListener(
                new AudioRecord.OnRecordPositionUpdateListener() {
                    @Override
                    public void onPeriodicNotification(AudioRecord recorder) {
                        String getFileName = GetFileTOwriteSound().getAbsolutePath();
                        copyWaveFile(getTempFilename(), getFileName);
                        try {
                            os.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        deleteTempFile();
                        try {
                            os = new FileOutputStream(filename);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                        //Toast.makeText(mcontext, "notify", Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onMarkerReached(AudioRecord recorder) {}
                }, handler);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question