i am recording the sound with audiorecord in 
android and at the same time i want to get notified about the amplitude of 
the input voice  I want to send peak values at interval of times as input 
to my service . Is it possible .

                        Also wanted to know how to get notified when audio 
file is finished while playing the media file with audiotrack. 
I










Here is my code 



import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.util.Log;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 
 * @author Dany
 */
public class RecordSystem {

    private boolean isRecording;
    private String fileLocation;

    public RecordSystem(String fileLocation) {
        this.fileLocation = fileLocation;
    }

    public void stopRecord() {
        isRecording = false;
    }

    public void startRecord() {
        isRecording = true;
        int frequency = 11025;
        int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_STEREO;
        int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;

        File file = new File(fileLocation);

        // delete any previous recording
        if (file.exists()) {
            file.delete();
        }

        // create the new file on SD
        try {
            file.createNewFile();
        } catch (IOException e) {
            throw new IllegalStateException("Failed to create "
                    + file.toString());
        }

        try {
            // create a DataOuputStream to write the audio data into the 
saved
            // file
            OutputStream os = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(os);
            DataOutputStream dos = new DataOutputStream(bos);

            int bufferSize = AudioRecord.getMinBufferSize(frequency,
                    channelConfiguration, audioEncoding);

            // create a new AudioRecord object to record the audio
            // uses the permission 'android.permission.RECORD_AUDIO' in the
            // manifest
            AudioRecord audioRecord = new AudioRecord(
                    MediaRecorder.AudioSource.MIC, frequency,
                    channelConfiguration, audioEncoding, bufferSize);

            short[] buffer = new short[bufferSize];
            audioRecord.startRecording();

            while (isRecording) {
                // grab the buffered input (mic) and write it to a file on 
the
                // SD
                int bufferReadResult = audioRecord.read(buffer, 0, 
bufferSize);
                for (int i = 0; i < bufferReadResult; i++) {
                    dos.writeShort(buffer[i]);
                    Log.i("result",""+bufferReadResult);
                }
            }

            audioRecord.stop();
            audioRecord.release();
            dos.flush();
            dos.close();

        } catch (Throwable t) {
            Log.e("AudioRecord", "Recording Failed");
        }
    }
}



/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package com.news.sample;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.util.Log;

public class PlaySystem {

    private boolean isPlaying;
    private String fileLocation;

    public PlaySystem(String fileLocation) {
        this.fileLocation = fileLocation;
    }

    public void stopPlay() {
        isPlaying = false;
    }

    public void startPlay(int playFrequency) {
        isPlaying = true;
        int frequency = 11025;
        int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
        int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;

        File file = new File(fileLocation);

        try {
            InputStream is = new FileInputStream(file);
            BufferedInputStream bis = new BufferedInputStream(is);
            DataInputStream dis = new DataInputStream(bis);

            int bufferSize = AudioRecord.getMinBufferSize(frequency,
                    channelConfiguration, audioEncoding);

            // initialize an audiotrack with a different frequency then
            // originally used for the recording
            // this wil automatically manipulate the 'pitch' of the sound
            AudioTrack audioTrack = new 
AudioTrack(AudioManager.STREAM_MUSIC,
                    playFrequency, channelConfiguration, audioEncoding,
                    bufferSize, AudioTrack.MODE_STREAM);

            // start playback
            audioTrack.play();

            short[] buffer = new short[bufferSize];

            while (isPlaying) {
                for (int i = 0; i < bufferSize; i++) {
                    // write the 'short' buffer blocks to the audiotrack
                    try {
                        short s = dis.readShort();
                        buffer[i] = s;
                    } catch (EOFException eofe) {
                        isPlaying = false;
                    }
                }
                audioTrack.write(buffer, 0, bufferSize);
            }

            audioTrack.stop();
            dis.close();

        } catch (Throwable t) {
            Log.e("AudioPlay", "Playing Failed");
        }
    }
}


-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to