Hi,
I'm using two methods to send audio to bluetooth headset:
1)
Thread thread = new Thread()
{
public boolean recording;
public int frequency;
@Override
public void run() {
Log.i("Audio", "Running Audio Thread");
AudioRecord recorder = null;
AudioTrack track = null;
short buffer[] = new short[160];
/*
* Initialize buffer to hold continuously recorded audio
data, start recording, and start
* playback.
*/
try
{
int N =
AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_IN_MONO,AudioFormat.ENCODING_PCM_16BIT);
recorder = new AudioRecord(AudioSource.MIC, 8000,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, N);
track = new AudioTrack(AudioManager.STREAM_MUSIC, 8000,
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, N, AudioTrack.MODE_STREAM);
recorder.startRecording();
track.play();
/*
* Loops until something outside of this thread stops it.
* Reads the data from the recorder and writes it to the
audio track for playback.
*/
while(!stopped)
{
Log.i("Map", "Writing new data to buffer");
N = recorder.read(buffer,0,buffer.length);
track.write(buffer, 0, buffer.length);
}
}
catch(Throwable x)
{
Log.w("Audio", "Error reading voice audio", x);
}
/*
* Frees the thread's resources after the loop completes so
that it can be run again
*/
finally
{
recorder.stop();
recorder.release();
track.stop();
track.release();
}
}
};
thread.start();
2) Using two threads to read and play data simultaneously:
class RecordThread implements Runnable {
Thread runner;
AudioRecord recorder;
int N;
short buffer[];
public RecordThread(int N) {
recorder = new AudioRecord(AudioSource.MIC, 8000,
AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, N);
recorder.startRecording();
runner = new Thread(this);
Log.v("mictmp", "Audio started");
runner.start(); // (2) Start the thread.
}
public void run() {
N =
recorder.read(SharedMemory.buffer,0,SharedMemory.buffer.length);
}
}
class TrackThread implements Runnable {
Thread runner;
AudioTrack track;
int N;
short buffer[];
public TrackThread(int N) {
track = new AudioTrack(AudioManager.STREAM_MUSIC, 8000,
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, N, AudioTrack.MODE_STREAM);
track.play();
runner = new Thread(this);
Log.v("mictmp", "Track started");
runner.start(); // (2) Start the thread.
}
public void run() {
track.write(SharedMemory.buffer,0,SharedMemory.buffer.length);
}
}
public class mictmp extends Activity {
/** Called when the activity is first created. */
private boolean stopped = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try
{
int N =
AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_IN_MONO,AudioFormat.ENCODING_PCM_16BIT);
SharedMemory m = new SharedMemory();
RecordThread rt = new RecordThread(N);
rt.run();
TrackThread tt = new TrackThread(N);
tt.run();
}
catch(Throwable x)
{
Log.w("Audio", "Error reading voice audio", x);
}
}
But in both the cases there is a delay of about 0.5-1 second. Can
someone help me figure out the reason or suggest me how to reduce this
delay?
Aditya Singal
On Nov 8, 7:00 pm, Aditya Singal <[email protected]> wrote:
> Hi,
> I wish to know howAudiodata is transmittedoverBluetoothchannel.
> What I mean to ask is, if I have a stream of bytes ofaudiodata, what
> kind of conversion or encoding is required before transmission.
> Aditya
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en