Reputation: 113
I am currently using AudioTrack in stream mode to play some prerecorded tracks. Play and stop mechanics work properly.However, when i hit pause and try to resume afterwards the buffer only reads -1, indicating that the EOF was reached.
I tried to mark and reset the Inputstream, like it's suggested in some posts but it didn't help.
public void pauseTrack() {
currentAudioTrack.pause();
isPaused = true;
}
public void resumeTrack() {
isPaused = false;
}
@Override public void run() {
try {
while (offset < audioFile.length()) {
if (isPaused)
continue;
currentAudioTrack.play();
int numberOfBytesRead = fileInputStream.read(audioData);
if (numberOfBytesRead != -1) {
currentAudioTrack.write(audioData, 0, numberOfBytesRead);
offset+=numberOfBytesRead;
}
else {
return;
}
}
Log.v("status", "finished reading");
} catch (IOException io) {
Log.v("Exception", "IOException found: " + io.getLocalizedMessage());
} catch (IllegalStateException ie) {
Log.v("Exception","IllegalStateException:" + ie.getLocalizedMessage());
}
}
What am i doing wrong?
Upvotes: 2
Views: 916
Reputation: 9
I used the skip() method, as below, to skip the offset which is recorded when pause is triggered. Works fine sofar.
public void play() {
try {
dis.skip(offset); //skip "offset * bufferSize" in the **dis** data input stream
while (((i = dis.read(music_chunk, 0 , 0)) > -1) && !isPaused) {
i = dis.read(music_chunk, 0, bufferSize);
track.write(music_chunk, 0, i); // track is the AudioTrack
position += bufferSize;
}
track.stop();
}
public void pause() {
track.pause();
offset = position ;
isPaused = true;
}
Please adjust the above code to your needs before running.
Thanks
Upvotes: 0
Reputation: 113
Just to give some closure to the post, i ended up changing to the AudioTrack static mode. The change wasn't because of the pause situation, however it end up solving it. Since the buffer writing is done all in one take, it doesn't present the challenges of the stream mode.
Upvotes: 1
Reputation: 965
Try this
public void pauseTrack()
{
currentAudioTrack.pause();
isPaused = true;
}
public void resumeTrack()
{
isPaused = false;
}
@Override public void run()
{
try {
while (offset < audioFile.length())
{
if(isPaused != true)
{
currentAudioTrack.play();
int numberOfBytesRead = fileInputStream.read(audioData);
if (numberOfBytesRead != -1) {
currentAudioTrack.write(audioData, 0, numberOfBytesRead);
offset+=numberOfBytesRead;
}
else {
return;
}
}
}
Log.v("status", "finished reading");
} catch (IOException io) {
Log.v("Exception", "IOException found: " + io.getLocalizedMessage());
} catch (IllegalStateException ie) {
Log.v("Exception","IllegalStateException:" + ie.getLocalizedMessage());
}
}
Upvotes: 0