Record and Save .Wav with Android AudioRecorder

Hi i am trying to record audio and and trying to save it as .wav format. i am able to record, saving and playing using AudioTracker but i am not able to play .wav format file in my mobile.
Here is my code can any one help to achieve it.
Thanks in advance....

public class TestRecord : Activity
{
Button startRec, stopRec, playBack;
AudioRecord audRecorder;
AudioTrack audioTrack;
volatile bool _isRecording = false;

    private byte[] audioBuffer;
    private int audioData;

    string wavPath = Android.OS.Environment.ExternalStorageDirectory.Path+"/"+System.String.Format("AudioRecorder.wav");

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        SetContentView (Resource.Layout.Main);

        RequestedOrientation = Android.Content.PM.ScreenOrientation.Portrait;

        startRec = FindViewById<Button> (Resource.Id.startrec);
        stopRec = FindViewById<Button> (Resource.Id.stoprec);
        playBack = FindViewById<Button> (Resource.Id.playback);

        startRec.Click += (sender, e) => 
        {
            _isRecording = true;
            new Thread ( delegate()
                {
                    RecordAudio();
                }).Start();             
            startRec.Enabled = false;
            stopRec.Enabled = true;
            playBack.Enabled = true;
        };
        stopRec.Click += (sender, e) =>
        {
            if(_isRecording == true)
            {
                _isRecording = false;
                audRecorder.Stop ();
                audRecorder.Release();
                startRec.Enabled = false;
                stopRec.Enabled = false;    
            }
        };
        playBack.Click += (sender, e) => 
        {   
            byte[] fileData = File.ReadAllBytes (wavPath);
            new Thread ( delegate()
                {
                    PlayAudioTrack(fileData);
                }).Start(); 
            startRec.Enabled = false;
            stopRec.Enabled = false;
            playBack.Enabled = false;
        };
    }

    public void RecordAudio()
    {
        System.IO.Stream outputStream = System.IO.File.Open (wavPath, FileMode.Create);
        BinaryWriter bWriter = new BinaryWriter(outputStream);

        audioBuffer = new byte[8000];

        audRecorder = new AudioRecord(
            // Hardware source of recording.
            AudioSource.Mic,
            // Frequency
            11025,
            // Mono or stereo
            ChannelIn.Mono,
            // Audio encoding
            Android.Media.Encoding.Pcm16bit,
            // Length of the audio clip.
            audioBuffer.Length
        );

        long totalAudioLen = 0;
        long totalDataLen = totalAudioLen + 36;
        long longSampleRate = 11025;
        int channels = 2;
        long byteRate = 16 * longSampleRate * channels/8;

        totalAudioLen = audioBuffer.Length;
        totalDataLen = totalAudioLen + 36;

        WriteWaveFileHeader(
            bWriter, 
            totalAudioLen, 
            totalDataLen, 
            longSampleRate, 
            channels, 
            byteRate);

        audRecorder.StartRecording();

        while (_isRecording == true) 
        {
            try
            {
                /// Keep reading the buffer while there is audio input.
                audioData = audRecorder.Read(audioBuffer, 0, audioBuffer.Length);

                bWriter.Write(audioBuffer);
            } 
            catch (System.Exception ex) 
            {
                System.Console.Out.WriteLine(ex.Message);
                break;
            }
        }
        outputStream.Close();
        bWriter.Close();
    }

    void PlayAudioTrack(byte[] audBuffer)
    {
        audioTrack = new AudioTrack(
            // Stream type
            Android.Media.Stream.Music,
            // Frequency
            11025,
            // Mono or stereo
            ChannelOut.Mono,
            // Audio encoding
            Android.Media.Encoding.Pcm16bit,
            // Length of the audio clip.
            audBuffer.Length,
            // Mode. Stream or static.
            AudioTrackMode.Stream);

        audioTrack.Play();
        audioTrack.Write(audBuffer, 0, audBuffer.Length);
    }

    private void WriteWaveFileHeader(
        BinaryWriter bWriter, long totalAudioLen,
        long totalDataLen, long longSampleRate, int channels,
        long byteRate) 
    {

        byte[] header = new byte[44];

        header[0] = (byte)'R'; // RIFF/WAVE header
        header[1] = (byte)'I';
        header[2] = (byte)'F';
        header[3] = (byte)'F';
        header[4] = (byte) (totalDataLen & 0xff);
        header[5] = (byte) ((totalDataLen >> 8) & 0xff);
        header[6] = (byte) ((totalDataLen >> 16) & 0xff);
        header[7] = (byte) ((totalDataLen >> 24) & 0xff);
        header[8] = (byte)'W';
        header[9] = (byte)'A';
        header[10] = (byte)'V';
        header[11] = (byte)'E';
        header[12] = (byte)'f'; // 'fmt ' chunk
        header[13] = (byte)'m';
        header[14] = (byte)'t';
        header[15] = (byte)' ';
        header[16] = 16; // 4 bytes: size of 'fmt ' chunk
        header[17] = 0;
        header[18] = 0;
        header[19] = 0;
        header[20] = 1; // format = 1
        header[21] = 0;
        header[22] = (byte) channels;
        header[23] = 0;
        header[24] = (byte) (longSampleRate & 0xff);
        header[25] = (byte) ((longSampleRate >> 8) & 0xff);
        header[26] = (byte) ((longSampleRate >> 16) & 0xff);
        header[27] = (byte) ((longSampleRate >> 24) & 0xff);
        header[28] = (byte) (byteRate & 0xff);
        header[29] = (byte) ((byteRate >> 8) & 0xff);
        header[30] = (byte) ((byteRate >> 16) & 0xff);
        header[31] = (byte) ((byteRate >> 24) & 0xff);
        header[32] = (byte) (2 * 16 / 8); // block align
        header[33] = 0;
        header[34] = 16; // bits per sample
        header[35] = 0;
        header[36] = (byte)'d';
        header[37] = (byte)'a';
        header[38] = (byte)'t';
        header[39] = (byte)'a';
        header[40] = (byte) (totalAudioLen & 0xff);
        header[41] = (byte) ((totalAudioLen >> 8) & 0xff);
        header[42] = (byte) ((totalAudioLen >> 16) & 0xff);
        header[43] = (byte) ((totalAudioLen >> 24) & 0xff);

        bWriter.Write(header, 0, 44);
    }
}

Posts

  • JonathanDibbleJonathanDibble GBMember ✭✭

    You are unable to play the wav file because the header is incorrect. You are writing the header before you have finished writing the data. You cannot know the length of the data, so therefore the header is wrong.

Sign In or Register to comment.