How to record audio on android wear
Asked Answered
S

1

7

There is some way to record audio on android wear? I used AudioRecord API and it crashes the application.

Am I doing something wrong?

        short[] audioData = new short[minBufferSize];

        AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                11025,
                AudioFormat.CHANNEL_CONFIGURATION_MONO,
                AudioFormat.ENCODING_PCM_16BIT,
                minBufferSize);

        audioRecord.startRecording();

        while(recording){
            int numberOfShort = audioRecord.read(audioData, 0, minBufferSize);
            for(int i = 0; i < numberOfShort; i++){
                dataOutputStream.writeShort(audioData[i]);
            }
        }

        audioRecord.stop();
Staghound answered 4/2, 2015 at 12:28 Comment(4)
"it crashes the application" -- use LogCat to examine the Java stack trace associated with the crash: #23353673Riga
Unfortunately the LogCat not show any error, the application just crashes without any error messages. I'm not sure if the android wear supports audio recorder, I want to know if any one made it work.Staghound
Tried it a few month ago, I had a problem with encoding which prevented the media recorder to be started. Haven't tried it again since, but maybe if you try to get rid of encoding it might work, as someone suggested me at the time.Rubeola
Could you please share your full code? did you use createNewFile() method for creating new file? also how do you solve EACCES problem by using getExternalStorageDirectory? thanks.Watts
T
6

AudioRecord is supported and works on Android Wear. If you are testing on an emulator it is possiblly not supporting the sample rate you are supplying, 11025, try another such as 8000.

After constructing AudioRecord, you should check the state to confirm initialization was successful before trying to record.

audioRecord.getState() == AudioRecord.STATE_INITIALIZED

Also ensure you have the correct permissions in the manifest. You will need android.permission.RECORD_AUDIO as a minimum and also any permissions for the location you are writing the file.

Tisiphone answered 7/2, 2015 at 1:23 Comment(2)
I forgot to put audio permissions in manifest. Now it works, thanks.Staghound
How can you access the recorded audio?Haruspicy

© 2022 - 2024 — McMap. All rights reserved.