android mediacode 获取解码后的输出数据到文件
时间: 2023-06-02 18:01:30 浏览: 158
MediaCoder.5755专业破解版
要获取MediaCodec解码后的输出数据并写入文件,可以使用以下步骤:
1. 配置MediaCodec并启动解码器:
```
// 配置MediaCodec
MediaCodec codec = MediaCodec.createDecoderByType(mime);
codec.configure(format, surface, null, 0);
codec.start();
```
2. 在循环中调用`codec.dequeueOutputBuffer()`获取解码后的数据:
```
boolean outputDone = false;
while (!outputDone) {
int outputBufferIndex = codec.dequeueOutputBuffer(info, TIMEOUT_US);
if (outputBufferIndex >= 0) {
ByteBuffer outputBuffer = codec.getOutputBuffer(outputBufferIndex);
// 处理输出数据
// ...
codec.releaseOutputBuffer(outputBufferIndex, false);
if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
outputDone = true;
}
} else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
// 处理输出格式改变
// ...
}
}
```
3. 处理输出数据,并将解码后的数据写入文件:
```
FileOutputStream fos = new FileOutputStream(file);
while (data.remaining() > 0) {
byte[] buffer = new byte[data.remaining()];
data.get(buffer);
fos.write(buffer);
}
fos.close();
```
完整示例代码:
```
MediaCodec codec = MediaCodec.createDecoderByType(mime);
codec.configure(format, surface, null, 0);
codec.start();
boolean outputDone = false;
while (!outputDone) {
int outputBufferIndex = codec.dequeueOutputBuffer(info, TIMEOUT_US);
if (outputBufferIndex >= 0) {
ByteBuffer outputBuffer = codec.getOutputBuffer(outputBufferIndex);
// 处理输出数据
// ...
// 写入文件
FileOutputStream fos = new FileOutputStream(file);
while (outputBuffer.remaining() > 0) {
byte[] buffer = new byte[outputBuffer.remaining()];
outputBuffer.get(buffer);
fos.write(buffer);
}
fos.close();
codec.releaseOutputBuffer(outputBufferIndex, false);
if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {
outputDone = true;
}
} else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
// 处理输出格式改变
// ...
}
}
```
阅读全文