问题
I'm working on the Android application which is using JLayer library to decode MP3 file (I need access to audio samples) and AudioTrack object to play it. I have something like this:
int timeMarker = 0;
int timeStep = 5000;
while (!isCancelled()) {
outSignalBuffer = decode(fileToPlay.getPath(), timeMarker, timeStep, bufferSize);
audioTrack.write(outSignalBuffer, 0, outSignalBuffer.length);
//publishProgress(outSignalBuffer);
timeMarker += timeStep;
}
This code is placed in my custom player created as AsyncTask. In general my decode and write method works OK because I hear sound file. The problem is, that my sound regularly jerk in every 5 seconds (timeStep = 5000 ms). As I see audioTrack.write(...) blocks my async task, so the short pause after every time Steps is caused by waiting to result from decode method.
Do you have any advices what is the best approach to correctly deliver outSignalBuffer to write method from decoder to get smooth audioTrack player?
回答1:
Have you tried timing the decode and audioTrack.write functions to see what is holding up your Async task? I wouldn't think audioTrack.write would be the problem.
My experience with JLayer is that it can be too slow for real-time decoding, especially if you're working with CD-quality audio (stereo, 44.1kHz sampling rate).
If you can raise your base SDK, Media Codec might be a good way to go: http://developer.android.com/reference/android/media/MediaCodec.html
You can also look at implementing your audio using openSL es. This post deals with some of the challenges and solutions for Android audio programming: Android audio programming nightmare - soundpool, audiotrack arrghh?
来源:https://stackoverflow.com/questions/21801644/use-audiotrack-with-jlayer-decoder