我正在嘗試在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;
}
}
}
崩潰了我上線「的setDataSource」相同的消息被宣佈。 嘗試與SD卡和內部存儲...試圖FilePath和FileDescriptor ..不工作。 你有沒有想過出了什麼問題? – badbadboy
@badbadboy還沒有我目前在我的應用程序中使用mp3流..但我正在等待新版本的android發佈,所以之後,我可以有更多的文件在媒體提取和代碼。 –