我想在媒体工作的提取在Android上OGG文件格式的音频流。 我已经写了一些代码与谷歌文档的帮助。 但它并没有在所有的工作。 可能是因为我的学生我写了一个错误的代码或语法。 它告诉我没有实例化提取
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MediaExtractor extractor = new MediaExtractor();
extractor.setDataSource("http://examplelink.com/ogg");// I cant put real link so sorry for that
int numTracks = extractor.getTrackCount();
for (int i = 0; i < numTracks; ++i) {
MediaFormat format = extractor.getTrackFormat(i);
String mime = format.getString(MediaFormat.KEY_MIME);
extractor.selectTrack(i);
ByteBuffer inputBuffer = ByteBuffer.allocate(1024);
while (extractor.readSampleData(inputBuffer,0) >= 0) {
int trackIndex = (int) extractor.getSampleTime();
long presentationTimeUs = extractor.getSampleTime();
}
MediaCodec codec = MediaCodec.createDecoderByType(mime);
codec.configure(format, null /* surface */, null /* crypto */, 0 /* flags */);
codec.start();
ByteBuffer[] inputBuffers = codec.getInputBuffers();
ByteBuffer[] outputBuffers = codec.getOutputBuffers();
format = codec.getOutputFormat();
Long timeoutUs=(long) 1;
for (;;) {
int inputBufferIndex = codec.dequeueInputBuffer(timeoutUs);
if (inputBufferIndex >= 0) {
// fill inputBuffers[inputBufferIndex] with valid data
codec.queueInputBuffer(inputBufferIndex, 0, 128, 0,0);
}
MediaCodec.BufferInfo info = new BufferInfo();
int outputBufferIndex = codec.dequeueOutputBuffer(info, timeoutUs);
if (outputBufferIndex >= 0) {
// outputBuffer is ready to be processed or rendered.
codec.releaseOutputBuffer(outputBufferIndex, false);
} else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
outputBuffers = codec.getOutputBuffers();
} else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
// Subsequent data will conform to new format.
format = codec.getOutputFormat();
AudioTrack mAudioTrack = null;
mAudioTrack.setPlaybackRate(format.getInteger(MediaFormat.KEY_SAMPLE_RATE));
}
codec.stop();
codec.release();
codec = null;
}
}
}