Transform recorded audio into a byte array

Asked

Viewed 47 times

1

Hello!

I’m developing an app and I want the following:

After the user records the audio, he clicks on a button that displays the byte array of the same. In fact, I need to see this vector, to then use it for another functionality (display the frequency spectrum through a FFT).

To develop the Glide library is apparently being used. It serves for displaying images, only I’m not sure if it serves for audio files either. Could you help me with this?

Messages from error in Logcat that I’m having:

02-06 14:28:39.510 27044-27044/? W/Glide: Load failed for /storage/emulated/0/e2180ce1-ec46-4a6f-8617-606964ded2deaudio_record.3gp with size [80x80]
    class com.bumptech.glide.load.engine.GlideException: Failed to load resource
      Cause (1 of 3): class com.bumptech.glide.load.engine.GlideException: Failed LoadPath{FileInputStream->Object->Drawable}, LOCAL
        Cause (1 of 3): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{FileInputStream->GifDrawable->Drawable}
        Cause (2 of 3): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{FileInputStream->Bitmap->Drawable}
        Cause (3 of 3): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{FileInputStream->BitmapDrawable->Drawable}
      Cause (2 of 3): class com.bumptech.glide.load.engine.GlideException: Failed LoadPath{ParcelFileDescriptor->Object->Drawable}, LOCAL
        Cause (1 of 2): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{ParcelFileDescriptor->Bitmap->Drawable}
        Cause (2 of 2): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{ParcelFileDescriptor->BitmapDrawable->Drawable}
      Cause (3 of 3): class com.bumptech.glide.load.engine.GlideException: Failed LoadPath{AssetFileDescriptor->Object->Drawable}, LOCAL
        Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{AssetFileDescriptor->Bitmap->Drawable}
02-06 14:28:45.739 27044-27044/? W/Glide: Load failed for /storage/emulated/0/e2180ce1-ec46-4a6f-8617-606964ded2deaudio_record.3gp with size [80x80]
    class com.bumptech.glide.load.engine.GlideException: Failed to load resource
      Cause (1 of 3): class com.bumptech.glide.load.engine.GlideException: Failed LoadPath{FileInputStream->Object->Drawable}, LOCAL
        Cause (1 of 3): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{FileInputStream->GifDrawable->Drawable}
        Cause (2 of 3): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{FileInputStream->Bitmap->Drawable}
        Cause (3 of 3): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{FileInputStream->BitmapDrawable->Drawable}
      Cause (2 of 3): class com.bumptech.glide.load.engine.GlideException: Failed LoadPath{ParcelFileDescriptor->Object->Drawable}, LOCAL
        Cause (1 of 2): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{ParcelFileDescriptor->Bitmap->Drawable}
        Cause (2 of 2): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{ParcelFileDescriptor->BitmapDrawable->Drawable}
      Cause (3 of 3): class com.bumptech.glide.load.engine.GlideException: Failed LoadPath{AssetFileDescriptor->Object->Drawable}, LOCAL
        Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{AssetFileDescriptor->Bitmap->Drawable}
02-06 14:28:45.740 27044-27044/? W/Glide: Load failed for /storage/emulated/0/e2180ce1-ec46-4a6f-8617-606964ded2deaudio_record.3gp with size [80x80]
    class com.bumptech.glide.load.engine.GlideException: Failed to load resource
      Cause (1 of 3): class com.bumptech.glide.load.engine.GlideException: Failed LoadPath{FileInputStream->Object->Drawable}, LOCAL
        Cause (1 of 3): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{FileInputStream->GifDrawable->Drawable}
        Cause (2 of 3): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{FileInputStream->Bitmap->Drawable}
        Cause (3 of 3): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{FileInputStream->BitmapDrawable->Drawable}
      Cause (2 of 3): class com.bumptech.glide.load.engine.GlideException: Failed LoadPath{ParcelFileDescriptor->Object->Drawable}, LOCAL
        Cause (1 of 2): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{ParcelFileDescriptor->Bitmap->Drawable}
        Cause (2 of 2): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{ParcelFileDescriptor->BitmapDrawable->Drawable}
      Cause (3 of 3): class com.bumptech.glide.load.engine.GlideException: Failed LoadPath{AssetFileDescriptor->Object->Drawable}, LOCAL
        Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{AssetFileDescriptor->Bitmap->Drawable}

My problem is being specifically in trying to understand these error messages that are popping up in the "Logcat window".

Below is my code, for better understanding and possible explanations of why errors are occurring in the Logcat window, as reported above.

public class Main2Activity extends AppCompatActivity {

//Declare variables
Button btnRecord, btnStopRecord, btnPlay, btnStop;
//String pathSave ="";
private static String pathSave;
MediaRecorder mediaRecorder;
MediaPlayer mediaPlayer;

final int REQUEST_PERMISSION_CODE=1000;

@Override
@SuppressWarnings("deprecation")
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    //Request Runtime permission
    if(!checkPermissionFromDevice()){
        requestPermission();
    }


    //Init View
    btnPlay = (Button) findViewById(R.id.btnPlay);
    btnRecord = (Button) findViewById(R.id.btnStartRecord);
    btnStop = (Button) findViewById(R.id.btnStop);
    btnStopRecord = (Button) findViewById(R.id.btnStopRecord);



    btnRecord.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick (View view){

            if (checkPermissionFromDevice())
            {

                pathSave = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+ UUID.randomUUID().toString()+"audio_record.3gp";
                setupMediaRecorder();
                try{
                    mediaRecorder.prepare();
                    mediaRecorder.start();
                }  catch (IOException e){
                    e.printStackTrace();
                }

                btnPlay.setEnabled(false);
                btnStop.setEnabled(false);

                Toast.makeText(Main2Activity.this, "Recording...", Toast.LENGTH_SHORT).show();

                //return pathSave;

            }

            else{

                requestPermission();
            }
        }
    });


   // final String pathSave = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+UUID.randomUUID().toString()+"audio_record.3gp";

   // Intent intent = new Intent(Main2Activity.this, Main4Activity.class);

   // intent.putExtra("key",pathSave);
   // startActivity(intent);

   // System.out.println(pathSave);
   // String myTag=null;
   // Log.d(myTag, "Salvo");

    btnStopRecord.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mediaRecorder.stop();
            btnStopRecord.setEnabled(false);
            btnPlay.setEnabled(true);
            btnRecord.setEnabled(true);
            btnStop.setEnabled(false);
        }
    });

    btnPlay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            btnStop.setEnabled(true);
            btnStopRecord.setEnabled(false);
            btnRecord.setEnabled(false);

            mediaPlayer = new MediaPlayer();
            try{
                mediaPlayer.setDataSource(pathSave);
                mediaPlayer.prepare();
            } catch (IOException e){
                e.printStackTrace();
            }

            mediaPlayer.start();
            Toast.makeText(Main2Activity.this, "Playing...", Toast.LENGTH_SHORT).show();

        }
    });

    btnStop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            btnStopRecord.setEnabled(false);
            btnRecord.setEnabled(true);
            btnStop.setEnabled(false);
            btnPlay.setEnabled(true);

            if (mediaPlayer != null){

                mediaPlayer.stop();
                mediaPlayer.release();
                setupMediaRecorder();
            }
        }
    });


}

private void setupMediaRecorder(){
    mediaRecorder = new MediaRecorder();
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
    mediaRecorder.setOutputFile(pathSave);
}

private void requestPermission() {
    ActivityCompat.requestPermissions(this, new  String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.RECORD_AUDIO
    } ,REQUEST_PERMISSION_CODE);

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode)
    {
        case REQUEST_PERMISSION_CODE:
        {
            if(grantResults.length>0 && grantResults[0]== PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText( this, "Permission Denied", Toast.LENGTH_SHORT).show();
            }
        }
        break;
    }
}

private boolean checkPermissionFromDevice(){
    int write_external_storage_result = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    int record_audio_result = ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO);
    return write_external_storage_result == PackageManager.PERMISSION_GRANTED &&
            record_audio_result == PackageManager.PERMISSION_GRANTED;

}



String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+UUID.randomUUID().toString()+"audio_record.3gp";

public byte[] convert(String path) throws IOException {

    FileInputStream fis = new FileInputStream(path);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] b = new byte[1024];

    for (int readNum; (readNum = fis.read(b)) != -1; ) {
        bos.write(b, 0, readNum);
    }

    byte[] bytes = bos.toByteArray();

    //String msgDecode = new String(bytes); // tentando converter byte em String
    //System.out.println("Vetor de bytes [" + msgDecode + "]"); // Exibir

    String decode = Arrays.toString(bytes);
    Log.d("mytag", decode);

    return bytes;

    }

}

Thank you!

  • decoded the audio in PCM format ?

  • My audio file is being recorded and then saved in ". 3gp format"

  • if u do not decode the file has no way to extract the data in PCM format to create a FFT later

  • @ederwander By "decode" the file, you mean "convert" it to PCM, correct? That is, you need to convert this file of type ". 3gp" in "PCM" so it is possible to apply an FFT.

  • exactly .. vc needs the data in PCM format and that you can only decode the audio before ...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.