Here I am posting some sample which works fine with me..

package com.audiotrack;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.BufferUnderflowException;

import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AudioPlayBackActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button play = (Button) findViewById(R.id.button1);
play.setOnClickListener(new OnClickListener() {

// @Override
public void onClick(View v) {

String filePath = "/sdcard/voice8K16bitmono_sir.pcm";
if (filePath == null)
return;

// Reading the file..
byte[] byteData = null;
File file = null;
file = new File(filePath);
byteData = new byte[(int) file.length()];
System.out.println("bytedata--" + byteData);
FileInputStream in = null;

try {

in = new FileInputStream(file);
in.read(byteData);
System.out.println("READING CARD-- " + in + "--BYTE Data--"
+ byteData + byteData.length);
in.close();
} catch (IOException e) {
e.printStackTrace();
} catch (BufferUnderflowException e) {
e.printStackTrace();
}

int dataSize = byteData.length / 2;
short[] shorts = byte2short(byteData);
int BufferElements2Play = 1024; // want to play 2048 (2K) since
// 2 bytes we use only 1024
int BytesPerElement = 2; // 2 bytes in 16bit format

int intSize = android.media.AudioTrack.getMinBufferSize(8000,
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT);

System.out.println("buffersize -- " + intSize);
AudioTrack at = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
8000, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, BufferElements2Play
* BytesPerElement, AudioTrack.MODE_STREAM);

if (at != null) {

int shortLength = shorts.length;
int shortLengthOffset2Play = 0;

System.out.println("SHORTS Data" + shortLength);
// Write the byte array to the track
at.write(shorts, 0, BufferElements2Play);
at.play();

shortLengthOffset2Play += BufferElements2Play; // Wrote 512
// ushorts

while (shortLengthOffset2Play < shortLength) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {

e.printStackTrace();
}
// Waiting, Simulating your condition of receiving data
// from server...
at.write(shorts, shortLengthOffset2Play,
BufferElements2Play);
// in your case it will 0, numberofShorts(2048)
shortLengthOffset2Play += BufferElements2Play;
System.out.println("shortlengthoffset --- "
+ shortLengthOffset2Play);
// Wrote yet another 512 shorts
}
} else
Log.d("Audio", "audio track is not initialised ");
}

private short[] byte2short(byte[] byteD) {
int byteArrsize = byteD.length / 2;
short[] shorts = new short[byteArrsize];
for (int i = 0; i < byteArrsize; i++) {
shorts[i] = (short) (byteD[i * 2] + (byteD[(i * 2) + 1] << 8));
}
return shorts;
}
});
}
}

On Mon, Oct 15, 2012 at 12:36 PM, Asheesh Arya <[email protected]>wrote:

> please refer this link
> http://www.benmccann.com/dev-blog/android-audio-recording-tutorial/
>
>  --
> 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
>

-- 
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

Reply via email to