It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Hello Am Currently Doing An App Related To Sound Modulation I need to know How to Record A sound And Modulate The Recorded Sound in Another Voice . Like Talking Tom Cat Application. i had Tried A solution From Stack over flow From HERE
And From Another Blog Blog Address
But i did not get Any idea.Can Any one please Give me Some Ideas Or Solution?
at last i have find the solution this may help some one
http://android-er.blogspot.in/2012/06/implement-voice-changer-by-changing.html
code
public class Sound_modActivity extends Activity {
Integer[] freqset = {11025, 16000, 22050, 44100};
private ArrayAdapter<Integer> adapter;
Spinner spFrequency;
Button startRec, stopRec, playBack;
Boolean recording;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startRec = (Button)findViewById(R.id.startrec);
stopRec = (Button)findViewById(R.id.stoprec);
playBack = (Button)findViewById(R.id.playback);
startRec.setOnClickListener(startRecOnClickListener);
stopRec.setOnClickListener(stopRecOnClickListener);
playBack.setOnClickListener(playBackOnClickListener);
spFrequency = (Spinner)findViewById(R.id.frequency);
adapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, freqset);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spFrequency.setAdapter(adapter);
}
OnClickListener startRecOnClickListener
= new OnClickListener(){
public void onClick(View arg0) {
Thread recordThread = new Thread(new Runnable(){
public void run() {
recording = true;
startRecord();
}
});
recordThread.start();
}};
OnClickListener stopRecOnClickListener
= new OnClickListener(){
public void onClick(View arg0) {
recording = false;
}};
OnClickListener playBackOnClickListener
= new OnClickListener(){
public void onClick(View v) {
playRecord();
}
};
private void startRecord(){
File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");
int sampleFreq = (Integer)spFrequency.getSelectedItem();
try {
file.createNewFile();
OutputStream outputStream = new FileOutputStream(file);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
DataOutputStream dataOutputStream = new DataOutputStream(bufferedOutputStream);
int minBufferSize = AudioRecord.getMinBufferSize(sampleFreq,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
short[] audioData = new short[minBufferSize];
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
sampleFreq,
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();
dataOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
void playRecord(){
File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");
int shortSizeInBytes = Short.SIZE/Byte.SIZE;
int bufferSizeInBytes = (int)(file.length()/shortSizeInBytes);
short[] audioData = new short[bufferSizeInBytes];
try {
InputStream inputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
DataInputStream dataInputStream = new DataInputStream(bufferedInputStream);
int i = 0;
while(dataInputStream.available() > 0){
audioData[i] = dataInputStream.readShort();
i++;
}
dataInputStream.close();
int sampleFreq = (Integer)spFrequency.getSelectedItem();
AudioTrack audioTrack = new AudioTrack(
AudioManager.STREAM_MUSIC,
sampleFreq,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
bufferSizeInBytes,
AudioTrack.MODE_STREAM);
audioTrack.play();
audioTrack.write(audioData, 0, bufferSizeInBytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Related
I am developing an Android app. In my app I need to record audio using AudionRecord and need to play it. But throwing error when play the recorded pcm file. The main problem is I cannot log know the error as well because I have to test on real device and cannot run on emulator. Because when I run on emulator and record, my application crash.
This is my activity
public class MainActivity extends AppCompatActivity {
Boolean recording;
private Thread recordingThread = null;
DataOutputStream dataOutputStream;
short[] audioData;
private Button btnPlay,btnStop,btnRecord;
private String outputFile = null;
//outputFile = Environment.getExternalStorageDirectory().getAbsolutePath()+"/recording.3gp";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
initViews();
setUpViews();
}
private void initialize()
{
}
private void initViews()
{
btnPlay = (Button)findViewById(R.id.btn_play);
btnRecord = (Button)findViewById(R.id.btn_record);
btnStop = (Button)findViewById(R.id.btn_stop);
}
private void setUpViews()
{
btnRecord.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startRecord();
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
recording = false;
Toast.makeText(getBaseContext(),"Stopped",Toast.LENGTH_SHORT).show();
}
});
btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playRecord();
}
});
}
private void startRecord(){
File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");
try {
file.createNewFile();
OutputStream outputStream = new FileOutputStream(file);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
dataOutputStream = new DataOutputStream(bufferedOutputStream);
final int minBufferSize = AudioRecord.getMinBufferSize(11025,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
audioData = new short[minBufferSize];
final AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
11025,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
minBufferSize);
audioRecord.startRecording();
Toast.makeText(getBaseContext(),"Recording",Toast.LENGTH_SHORT).show();
recordingThread = new Thread(new Runnable() {
public void run() {
while(recording){
int numberOfShort = audioRecord.read(audioData, 0, minBufferSize);
for(int i = 0; i < numberOfShort; i++){
try{
dataOutputStream.writeShort(audioData[i]);
}
catch (IOException e)
{
Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
}
}
}, "AudioRecorder Thread");
audioRecord.stop();
dataOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void playRecord(){
File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");
int shortSizeInBytes = Short.SIZE/Byte.SIZE;
int bufferSizeInBytes = (int)(file.length()/shortSizeInBytes);
short[] audioData = new short[bufferSizeInBytes];
try {
InputStream inputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
DataInputStream dataInputStream = new DataInputStream(bufferedInputStream);
int i = 0;
while(dataInputStream.available() > 0){
audioData[i] = dataInputStream.readShort();
i++;
}
dataInputStream.close();
AudioTrack audioTrack = new AudioTrack(
AudioManager.STREAM_MUSIC,
11025,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
bufferSizeInBytes,
AudioTrack.MODE_STREAM);
audioTrack.play();
audioTrack.write(audioData, 0, bufferSizeInBytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The problem is with playing the recorded pcm. When I click play button, application throws fatal error and stopped. I cannot log the error as well. The pcm file really exists at the defined path as well. What is wrong with my code? Why can it not play?
As you can see below, pcm file created successfully after recording
Without seeing the Logcat, it is hard to figure the issue in your code. Take a look at this working code on 'How to play pcm files using AudioTrack':
private void PlayAudioFileViaAudioTrack(String filePath) throws IOException
{
// We keep temporarily filePath globally as we have only two sample sounds now..
if (filePath==null)
return;
int intSize = android.media.AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT);
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT, intSize, AudioTrack.MODE_STREAM);
if (at==null){
Log.d("TCAudio", "audio track is not initialised ");
return;
}
int count = 512 * 1024; // 512 kb
//Reading the file..
byte[] byteData = null;
File file = null;
file = new File(filePath);
byteData = new byte[(int)count];
FileInputStream in = null;
try {
in = new FileInputStream( file );
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int bytesread = 0, ret = 0;
int size = (int) file.length();
at.play();
while (bytesread < size) {
ret = in.read( byteData,0, count);
if (ret != -1) { // Write the byte array to the track
at.write(byteData,0, ret);
bytesread += ret;
}
else break;
}
in.close();
at.stop();
at.release();
}
code from http://jongladwin.blogspot.co.uk/2010/03/android-play-pcmwav-audio-buffer-using.html
I have a short code that streams the recorded audio in realtime to the speakers if the user push the start button. After he pushes the stop button, the buffered audio should be saved in a mp3 file. The file was created but it is empty. If I try to play the file I cant hear anything.
public class MainActivity extends Activity {
boolean m_stop = true;
AudioTrack m_audioTrack;
Thread m_noiseThread;
static final int bufferSize = 200000;
AudioRecord arec;
FileOutputStream os = null;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onStartStopClicked(View v)
{
Button StartButton = (Button)findViewById(R.id.StartStop);
if(m_stop) {
start();
} else {
stop();
}
}
Runnable m_noiseGenerator = new Runnable()
{
public void run()
{
String filepath = Environment.getExternalStorageDirectory().toString();
os = new FileOutputStream(filepath + "/test.mp3");
int buffersize1 = AudioRecord.getMinBufferSize(11025, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
arec = new AudioRecord(MediaRecorder.AudioSource.MIC, 11025, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, buffersize1);
m_audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 11025, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, 8000, AudioTrack.MODE_STREAM);
byte[] buffer = new byte[buffersize1];
arec.startRecording();
m_audioTrack.play();
while(!m_stop)
{
arec.read(buffer, 0, buffersize1);
m_audioTrack.write(buffer, 0, buffer.length);
os.write(buffer);
}
}
};
public void start()
{
m_stop = false;
m_noiseThread = new Thread(m_noiseGenerator);
m_noiseThread.start();
}
public void stop()
{
m_stop = true;
arec.stop();
m_audioTrack.stop();
os.flush();
os.close();
}
}
Can someone help me to fix that? That would be great thanks :)
In one of my app i need to change sound frequency like it should be in thick voice for that i have use
THIS LINK
The above link code works fine for me...
While playing audio it change voice
My problem is HOW TO STORE THIS CHANGED VOICE?
this is method which play voice with changing in voice
void playRecord(){
File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");
int shortSizeInBytes = Short.SIZE/Byte.SIZE;
int bufferSizeInBytes = (int)(file.length()/shortSizeInBytes);
short[] audioData = new short[bufferSizeInBytes];
try {
InputStream inputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
DataInputStream dataInputStream = new DataInputStream(bufferedInputStream);
int i = 0;
while(dataInputStream.available() > 0){
audioData[i] = dataInputStream.readShort();
i++;
}
dataInputStream.close();
int sampleFreq = (Integer)spFrequency.getSelectedItem();
AudioTrack audioTrack = new AudioTrack(
AudioManager.STREAM_MUSIC,
sampleFreq,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
bufferSizeInBytes,
AudioTrack.MODE_STREAM);
audioTrack.play();
audioTrack.write(audioData, 0, bufferSizeInBytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
How can i store this clip in SDCARD? this is what i need is
In short i need all this thing to share that changed voice clip.. how i can i do this?
Can anybody help me to solve this problem?
I had the same problem and searched a lot and could not find a solution, But I came to a solution is to record the file with natural sound and save it in the phone and then used and called with the change Hz again ..
This Is Code StartRecord And Save it in Storage Device..
private void startRecord() {
AudioSavePathInDevice = Environment.getExternalStorageDirectory().getAbsolutePath() + "/SarahaVoice";
NameAudio = CreateRandomAudioFileName(5) + "Recording.mp3";
Toast.makeText(getActivity(), NameAudio, Toast.LENGTH_SHORT).show();
File file = new File(AudioSavePathInDevice, NameAudio);
try {
//file.createNewFile();
//Upcasting
OutputStream outputStream = new FileOutputStream(file);
//Buffering (Get Temporary Space To Write File.. When Finished Writing File Removed Temporary Space)
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
//Uses To Write Data Types Of Primitive Data In File
DataOutputStream dataOutputStream = new DataOutputStream(bufferedOutputStream);
int minBufferSize = AudioRecord.getMinBufferSize(11025, 2, 2);
short[] audioData = new short[minBufferSize];
AudioRecord audioRecord = new AudioRecord(1, 11025, 2, 2,minBufferSize);
audioRecord.startRecording();
while(recording){
int numberOfShort = audioRecord.read(audioData, 0, minBufferSize);
for(int i = 0; i < numberOfShort; i++){
dataOutputStream.writeShort(audioData[i]);
}
}
if (!recording.booleanValue())
{
audioRecord.stop();
audioRecord.release();
dataOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
And Also Code To PlayRecord audio From Storage Device With Change Hz..
void playRecord() {
audioTrack = null;
String str = (String) spFrequency.getSelectedItem();
File file = new File(AudioSavePathInDevice, NameAudio);
int shortSizeInBytes = Short.SIZE/Byte.SIZE;
final int bufferSizeInBytes = (int)(file.length()/shortSizeInBytes);
final short[] audioData = new short[bufferSizeInBytes];
try {
InputStream inputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
DataInputStream dataInputStream = new DataInputStream(bufferedInputStream);
int j = 0;
while(dataInputStream.available() > 0){
audioData[j] = dataInputStream.readShort();
j++;
}
dataInputStream.close();
if (str.equals(getString(R.string.normal)))
{
i = 11025;
}
if (str.equals(getString(R.string.monster)))
{
i = 8000;
}
if (str.equals(getString(R.string.chipmunk)))
{
i = 16000;
}
if (str.equals(getString(R.string.funny)))
{
i = 9000;
}
if (str.equals(getString(R.string.bee)))
{
i = 13000;
}
audioTrack = new AudioTrack(3,i,2,2,bufferSizeInBytes,1);
audioTrack.play();
audioTrack.write(audioData, 0, bufferSizeInBytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Ask yourself what they are Function CreateRandomAudioFileName() ^_^ This Function Create Random text Its code is down..
public String CreateRandomAudioFileName(int string){
StringBuilder stringBuilder = new StringBuilder( string );
int i = 0 ;
while(i < string ) {
stringBuilder.append(RandomAudioFileName.charAt(random.nextInt(RandomAudioFileName.length())));
i++ ;
}
return stringBuilder.toString();
}
God be with you :)
I am developing an android application that records through MIC and changes its frequency. Similar to Helium booth app for iphone. For the time being i have written code to record a file and save it in sdcard. I am getting an error which says:
Could not get audio input for record source 1
Error creating AudioRecord instance: initialization check failed.
[ android.media.AudioRecord ] Error code -20 when initializing native
FATAL EXCEPTION: Thread-10
java.lang.IllegalStateException: startRecording() called on an uninitialized
Following is my code:
public class MainActivity extends Activity {
Integer[] freqset = {8000};
private ArrayAdapter<Integer> adapter;
Spinner spFrequency;
Button startRec, stopRec, playBack;
Boolean recording;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startRec = (Button)findViewById(R.id.startrec);
stopRec = (Button)findViewById(R.id.stoprec);
playBack = (Button)findViewById(R.id.playback);
startRec.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Thread recordThread = new Thread(new Runnable(){
#Override
public void run() {
recording = true;
startRecord();
}
});
recordThread.start();
}
});
stopRec.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
recording = false;
}
});
playBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playRecord();
}
});
spFrequency = (Spinner)findViewById(R.id.frequency);
adapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, freqset);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spFrequency.setAdapter(adapter);
}
private void startRecord(){
File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");
int BytesPerElement = 2;
int sampleFreq = (Integer)spFrequency.getSelectedItem();
Log.e("Test","Start Recording"+sampleFreq);
try {
file.createNewFile();
OutputStream outputStream = new FileOutputStream(file);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
DataOutputStream dataOutputStream = new DataOutputStream(bufferedOutputStream);
int minBufferSize = AudioRecord.getMinBufferSize(sampleFreq,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
Log.i("Test","Buffer Size is"+minBufferSize);
short[] audioData = new short[minBufferSize];
Log.i("Test","audioData initialized"+audioData);
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
sampleFreq,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
minBufferSize*BytesPerElement);
Log.i("Test","audioRecord initialized"+audioRecord);
audioRecord.startRecording();
while(recording){
int numberOfShort = audioRecord.read(audioData, 0, minBufferSize);
for(int i = 0; i < numberOfShort; i++){
dataOutputStream.writeShort(audioData[i]);
Log.i("Test","Recording buzzz");
}
}
audioRecord.stop();
dataOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
Log.e("Testing", "Esception Found");
}
}
void playRecord(){
File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");
int shortSizeInBytes = Short.SIZE/Byte.SIZE;
int bufferSizeInBytes = (int)(file.length()/shortSizeInBytes);
short[] audioData = new short[bufferSizeInBytes];
try {
InputStream inputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
DataInputStream dataInputStream = new DataInputStream(bufferedInputStream);
int i = 0;
while(dataInputStream.available() > 0){
audioData[i] = dataInputStream.readShort();
i++;
}
dataInputStream.close();
int sampleFreq = (Integer)spFrequency.getSelectedItem();
AudioTrack audioTrack = new AudioTrack(
AudioManager.STREAM_MUSIC,
sampleFreq,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
bufferSizeInBytes,
AudioTrack.MODE_STREAM);
audioTrack.play();
audioTrack.write(audioData, 0, bufferSizeInBytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}}
P.S I asked similar question earlier but i used MediaRecorder. Here i am using AudioRecord class now.
I am developing a sound recording app for android. The problem I am facing is that I want to know when the sound file that is being played finishes and I do not know how to check. There is setOnCompleteListener for MediaRecorder class but I am using AudioRecorder class because I need to do some editing while recording too, like change in frequency and pitch of the speaker.
Following is my code:
public class MainActivity extends Activity {
Integer[] freqset = {8000};
private ArrayAdapter<Integer> adapter;
Spinner spFrequency;
Button startRec, stopRec, playBack;
Boolean recording;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startRec = (Button)findViewById(R.id.startrec);
stopRec = (Button)findViewById(R.id.stoprec);
playBack = (Button)findViewById(R.id.playback);
startRec.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Thread recordThread = new Thread(new Runnable(){
#Override
public void run() {
recording = true;
startRecord();
}
});
recordThread.start();
}
});
stopRec.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
recording = false;
}
});
playBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("Test","Play Record called");playRecord();
}
});
spFrequency = (Spinner)findViewById(R.id.frequency);
adapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, freqset);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spFrequency.setAdapter(adapter);
}
private void startRecord(){
File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");
int BytesPerElement = 2;
int sampleFreq = (Integer)spFrequency.getSelectedItem();
Log.e("Test","Start Recording"+sampleFreq);
try {
file.createNewFile();
OutputStream outputStream = new FileOutputStream(file);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
DataOutputStream dataOutputStream = new DataOutputStream(bufferedOutputStream);
int minBufferSize = AudioRecord.getMinBufferSize(sampleFreq,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT);
Log.i("Test","Buffer Size is"+minBufferSize);
short[] audioData = new short[minBufferSize];
Log.i("Test","audioData initialized"+audioData);
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
sampleFreq,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
minBufferSize*BytesPerElement);
Log.i("Test","audioRecord initialized"+audioRecord);
audioRecord.startRecording();
while(recording){
int numberOfShort = audioRecord.read(audioData, 0, minBufferSize);
for(int i = 0; i < numberOfShort; i++){
dataOutputStream.writeShort(audioData[i]);
Log.i("Test","Recording buzzz"+recording);
}
}
Log.i("Test","Recording Value after stop"+recording);
audioRecord.stop();
dataOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
Log.e("Testing", "Esception Found");
}
}
void playRecord(){
File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");
int shortSizeInBytes = Short.SIZE/Byte.SIZE;
int bufferSizeInBytes = (int)(file.length()/shortSizeInBytes);
short[] audioData = new short[bufferSizeInBytes];
try {
InputStream inputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
DataInputStream dataInputStream = new DataInputStream(bufferedInputStream);
int i = 0;
while(dataInputStream.available() > 0){
audioData[i] = dataInputStream.readShort();
i++;
}
dataInputStream.close();
int sampleFreq = (Integer)spFrequency.getSelectedItem();
Log.i("Test","Sab set hai");
AudioTrack audioTrack = new AudioTrack(
AudioManager.STREAM_MUSIC,
sampleFreq,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
bufferSizeInBytes,
AudioTrack.MODE_STREAM);
Log.i("Test","will start play");
audioTrack.play();
audioTrack.write(audioData, 0, bufferSizeInBytes);
Log.i("Test","does this print");
} catch (FileNotFoundException e) {
Log.i("Test","Ye wali exception");
e.printStackTrace();
} catch (IOException e) {
Log.i("Test"," Ni Ye wali exception");
e.printStackTrace();
}
}}
you can use AudioTrack.OnPlaybackPositionUpdateListener and AudioTrack.setNotificationMarkerPosition to known Audio playing is finished or not as :
AudioTrack audioTrack = new AudioTrack(
AudioManager.STREAM_MUSIC,
sampleFreq,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
bufferSizeInBytes,
AudioTrack.MODE_STREAM);
// set setNotificationMarkerPosition accouding audio length
audioTrack.setNotificationMarkerPosition(bufferSizeInBytes);
// now add OnPlaybackPositionUpdateListener to audioTrack
audioTrack.setPlaybackPositionUpdateListener(
new AudioTrack.OnPlaybackPositionUpdateListener() {
#Override
public void onMarkerReached(AudioTrack track) {
// do your work here....
}
});