Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use this library with live recording #21

Open
rk8339811 opened this issue May 5, 2021 · 2 comments
Open

How to use this library with live recording #21

rk8339811 opened this issue May 5, 2021 · 2 comments

Comments

@rk8339811
Copy link

rk8339811 commented May 5, 2021

Hi,

I am working on an Audio recording function. I want the recorded Audio to be saved into the internal cache directory of my app so that I can later process it and send it to my server. I have taken the RECORD_AUDIO_PERMISSION in my Android Manifest.

Below is the code I am using for recording audio and save it to a file.

    String uuid = UUID.randomUUID().toString();
    fileName = getExternalCacheDir().getAbsolutePath() + "/" + uuid + ".3gp";


    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setOutputFile(fileName);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);


    try {
        recorder.prepare();
        recorder.start();
    } catch (IOException e) {}

The above code to work fine but I am facing another issue. I want to create a Waveform effect for my app for which I am using this library. I know the below code can be used to pass on bytes to the visualizer.

//get reference to visualizer
 mVisualizer = findViewById(R.id.blast);

 //TODO: get the raw audio bytes
 
 //pass the bytes to visualizer
 mVisualizer.setRawAudioBytes(bytes);

Now, my question is how can I get the Bytes in real-time of the Audio which is being recorded and being saved? Should I read the file and extract recent bytes from it at regular intervals or is there any other method to achieve this?

Any help would be appreciated

Thanks.

@vikashrathore081
Copy link

private fun convertFileToBytes(file: File):ByteArray {
try {
val b = ByteArray(1024)
val os= ByteArrayOutputStream()
val fis=FileInputStream(file)
var read:Int=0
read=fis.read(b)
while ((fis.read(b).also { read = it })!=-1){
os.write(b,0,read)
}
fis.close()
os.close()
return os.toByteArray()

    } catch (ex: Exception) {
        ex.printStackTrace()
    }
    return byteArrayOf(1,2,3)
}

@christina-bakirtzi
Copy link

Were you able to solve this? I have the same issue, but even by adding vikashrathore081 's code I can't seem to make it work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants