I am trying to play audio buffered sound (.wav) using AudioTrack. Please see the code below. I need to call this function under a Thread to support simultaneous play. It is fine being under a Thread. It is working fine playing the sound normally. But if i execute playing the sound using AudioTrack one after another continuously(i.e. executing second play before completing the first play sound), produces device crash (force close unexpectedly error).
Does anyone come across such problems and resolve it in a way?
private void PlayAudioTrack(String filePath) throws IOException
{
if (filePath==null)
return;
byte[] byteData = null;
File file = null;
file = new File(filePath); // sdcard path
byteData = new byte[(int) file.length()];
FileInputStream in = null;
try {
in = new FileInputStream( file );
in.read( byteData );
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int intSize = android.media.AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_8BIT);
at = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_8BIT, intSize, AudioTrack.MODE_STREAM);
at.play();
at.write(byteData, 0, byteData.length);
at.stop();
}
Appreciate your response.
Thanks.
You have to release the AudioTracks resources as well as stopping it
at.release();
Related
Iam trying to develop an application where i can set the speed of music file(mp3) to be set like 1x,1.5x,2x,2.5x like this.but MediaPlayer does not support this feauture unless it is 23 api.How can i use AudioTrack to play this mp3 file and also seek to position.the below code gives me "zzzzzzz" sound.
public void playAudio(){
int minBufferSize = AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
int bufferSize = 512;
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, minBufferSize, AudioTrack.MODE_STREAM);
String filepath = Environment.getExternalStorageDirectory().getAbsolutePath();
int i = 0;
byte[] s = new byte[bufferSize];
try {
final String path= Environment.getExternalStorageDirectory().getAbsolutePath() + "/folioreader/audio"+".mp3";
FileInputStream fin = new FileInputStream(path);
DataInputStream dis = new DataInputStream(fin);
audioTrack.play();
while((i = dis.read(s, 0, bufferSize)) > -1){
audioTrack.write(s, 0, i);
}
audioTrack.stop();
audioTrack.release();
dis.close();
fin.close();
} catch (FileNotFoundException e) {
// TODO
e.printStackTrace();
} catch (IOException e) {
// TODO
e.printStackTrace();
}
}
AudioTrack can play uncompressed audio (WAV) not any compressed ones (mp3 or AAC etc.,) You need to call MediaCodec to decode and then use Audio Track to play audio. Refer these links, https://developer.android.com/reference/android/media/AudioTrack.html and https://developer.android.com/reference/android/media/MediaCodec.html.
For faster playback, give sample rate proportional to the speed that you require. For ex. to play 8kHz audio in 2X rate, give 16kHz in AudioTrack and so on. This is crude way.
I'm a beginner in Android programming!
My hardware is a Samsung Galaxy Young GT-S5360!
With my app I want to record something using the mic source.
If I record a sine with 1000Hz or other sound samples I always get two transients or clicking sounds at the beginning of the sample. After 0,200s the sample looks ok for me.
How can I elimenate these transients?!
Here's my code which I'm using found on the web:
private void startrec(){
File file = new File(Environment.getExternalStorageDirectory(),"test.pcm" );
int minBufferSize = audioRecord.getMinBufferSize(44100,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
short[] audioData = new short[minBufferSize];
try {
file.createNewFile();
OutputStream outputStream = new FileOutputStream(file);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
DataOutputStream dataOutputStream = new DataOutputStream(bufferedOutputStream);
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
44100,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
minBufferSize);
audioRecord.startRecording();
while(isRecording){
int numberOfShort = audioRecord.read(audioData, 0, minBufferSize);
for(int i = 0; i < numberOfShort; i++){
dataOutputStream.writeShort(audioData[i]);
}
}
audioRecord.stop();
audioRecord.release();
dataOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
The startrec() function is called with a button click and is stopped with another button.
Maybe the Button-Click-Sound causes the transients at the beginning, because it also uses the same SOURCE(MIC).
It could also be a setup problem, but I'dont know yet.
I also want to record with 44100 samples p/s .
During the time I have tried different sample rates, but there's still the same problem.
I hope someone can help me and can give me some advice!
Have a nice day!
I want to play audio file during a call and i'm able to play it but when i make a call it play file only on that device on which i have installed the application.
here is my sample code by which i have played the audio file.
public void play() {
bufferSize = AudioTrack.getMinBufferSize(16000,
AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
AudioTrack at = null;
at = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
16000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM);
String filePath = Environment.getExternalStorageDirectory()
.getAbsolutePath();
String file = filePath + "/" + "myfile.wav";
int i = 0;
byte[] s = new byte[bufferSize];
try {
FileInputStream fin = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fin, 44000);
DataInputStream dis = new DataInputStream(bis);
at.play();
while ((i = dis.read(s, 0, bufferSize)) > -1) {
at.write(s, 0, i);
}
at.stop();
at.release();
dis.close();
fin.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
This code works fine with me.
But i want that this file would be able to hear by the other end person who make call. how could i do it???
Any idea?? i know android API doesn't allow to do it..but anyone who did it??
please give only genuine and accurate answer not assumptions.
Is there a way to launch an audio file when answering a call to be played NOT into the call (so the other side could hear), but only in the call speaker (so only our side could hear).
Sounds strange, I know but it is part of a much larger app.
First of all, you'll need to set up a BroadcastReceiver (let's call it "CallReceiver"), and permission to know about the phone state (intuitively, the permission to add is android.permission.READ_PHONE_STATE).
Register your CallReceiver action like this.
<receiver android:name=".CallReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
</intent-filter>
</receiver>
At your CallReceiver, you may decide upon which actions should your audio play back (incoming/outcoming/phone ringing...), so just read the EXTRA_STATE, and getCallState() (check out the TelephonyManager docs).
About the audio, you will need to use the AudioManager, and set the "in call" mode of playback before playing the sound.
private AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_IN_CALL);
am.setSpeakerphoneOn(false);
I hope this helps!
private void PlayShortAudioFileViaAudioTrack(String filePath) throws IOException {
// We keep temporarily filePath globally as we have only two sample sounds now..
if (filePath == null)
return;
//Reading the file..
byte[] byteData = null;
File file = null;
file = new File(filePath); // for ex. path= "/sdcard/samplesound.pcm" or "/sdcard/samplesound.wav"
byteData = new byte[(int) file.length()];
FileInputStream in = null;
try {
in = new FileInputStream(file);
in.read(byteData);
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Set and push to audio track..
int intSize = android.media.AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_8BIT);
AudioTrack at = new AudioTrack(AudioManager.STREAM_VOICE_CALL, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_8BIT, intSize, AudioTrack.MODE_STREAM);
if (at != null) {
at.play();
// Write the byte array to the track
at.write(byteData, 0, byteData.length);
at.stop();
at.release();
} else
Log.d("TCAudio", "audio track is not initialised ");
I am trying to play the bigger size audio wav file(which is >20 mb) using the following code(AudioTrack) on my Android 1.6 HTC device which basically has less memory. But i found device crash as soon as it executes reading, writing and play. But the same code works fine and plays the lesser size audio wav files(10kb, 20 kb files etc) very well.
P.S: I should play PCM(.wav) buffer sound, the reason behind why i use AudioTrack here.
Though my device has lesser memory, how would i read bigger audio files bytes by bytes and play the sound to avoid crashing due to memory constraints.
private void AudioTrackPlayPCM() throws IOException
{
String filePath = "/sdcard/myWav.wav"; // 8 kb file
byte[] byteData = null;
File file = null;
file = new File(filePath);
byteData = new byte[(int) file.length()];
FileInputStream in = null;
try {
in = new FileInputStream( file );
in.read( byteData );
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int intSize = android.media.AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_8BIT);
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_8BIT, intSize, AudioTrack.MODE_STREAM);
at.play();
at.write(byteData, 0, byteData.length);
at.stop();
at.release();
}
Could someone guide me please to play the AudioTrack code for bigger size wav files?
Your intSize will be the minimum buffer size needed to init the AudioTrack, try using a multiple of this to create a bigger buffer
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 8000,
AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_8BIT, intSize*10,
AudioTrack.MODE_STREAM);