I have problem with end of stream signalled earlier than expected (way before file ends) by the advance() method of MediaExtractor class. According to Google reference the advance() method could work wrong when using a local file (and this is my case - filePath ponits to a local file):
When extracting a local file, the behaviors of advance() and readSampleData(ByteBuffer, int) are undefined in presence of concurrent writes to the same local file:
Unfortunately there is no single word about using MediaExtractor without advance() method. How to move to the next sample? If there is no way to do it then I'd like to know how to feed inputBuffer without using MediaExtractor.
A fragment of my code below:
if(Build.VERSION.SDK_INT >= 21 ) {
try {
codec = MediaCodec.createByCodecName("OMX.google.mp3.decoder");
} catch (IOException e) {
e.printStackTrace();
}
final MediaExtractor extractor = new MediaExtractor();
try {
extractor.setDataSource(filePath); //local file
Log.i("filePath", String.valueOf(filePath));
extractor.selectTrack(0);
} catch (IOException e) {
e.printStackTrace();
}
codec.setCallback(new MediaCodec.Callback() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onInputBufferAvailable(MediaCodec mc, int inputBufferId) {
ByteBuffer inputBuffer = codec.getInputBuffer(inputBufferId);
// fill inputBuffer with valid data
int sampleSize = extractor.readSampleData(inputBuffer,0);
if(extractor.advance) {
codec.queueInputBuffer(inputBufferId, 0, sampleSize, 0, 0);
} else {
// EOS
codec.queueInputBuffer(inputBufferId, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
codec.stop();
codec.release();
}
}
// more callbacks
};
}