Reputation: 711
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int minBufferSize = AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, minBufferSize, AudioTrack.MODE_STREAM);
playfilesound();
}
private void playfilesound() throws IOException
{
int count = 512 * 1024; // 512 kb
//Reading the file..
byte[] byteData = null;
File file = null;
file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+"recordsound"); //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();
audioTrack.play();
while (bytesread < size) { // Write the byte array to the track
ret = in.read( byteData,0, count); //ret =size in bytes
if (ret != -1) {
audioTrack.write(byteData,0, ret);
bytesread += ret; } //ret
else break;
} //while
in.close();
audioTrack.stop(); audioTrack.release();
}
I used the debugger to step through the code and hover abover audioTrack, it's allocated and inited. The file also exists.
But when it hits audioTrack.play() it throws an error of saying its illegal state exception, unintilized AudioTrack.
I enclosed the project which includes the recording file part. http://www.mediafire.com/?6i2r3whg7e7rs79
Upvotes: 0
Views: 2246
Reputation: 1266
You've got more than one problems here:
Please also format your code.
EDIT: I also added
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_AUDIO);
to make the playback more soft. Then you can set it to default again:
android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
Upvotes: 0
Reputation: 21
The configuration of the channel you use is discontinued, in the place of AudioFormat.CHANNEL_CONFIGURATION_MONO
Uses AudioFormat.CHANNEL_IN_MONO
To record and AudioFormat.CHANNEL_OUT_MONO
to play...
Upvotes: 2
Reputation: 9
Looks like you called play before you write! Try this ...
int bytesread = 0, ret = 0;
int size = (int) file.length();
//audioTrack.play(); <---- play called prematurely
while (bytesread < size) { // Write the byte array to the track
ret = in.read( byteData,0, count); //ret =size in bytes
if (ret != -1) {
audioTrack.write(byteData,0, ret);
bytesread += ret;
audioTrack.play(); //<--- try calling it here!
} //ret
else break;
} //while
Upvotes: 0