createInputSurface本地录像demo
时间: 2023-08-09 22:05:21 浏览: 40
以下是一个使用`createInputSurface`方法进行本地录像的示例代码:
```java
public class MainActivity extends AppCompatActivity {
private MediaCodec mMediaCodec;
private Surface mSurface;
private MediaMuxer mMediaMuxer;
private int mTrackIndex;
private boolean mMuxerStarted;
private long mLastPresentationTimeUs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 设置录制视频的宽和高
int width = 720;
int height = 1280;
// 创建H.264编码器
try {
mMediaCodec = MediaCodec.createEncoderByType("video/avc");
} catch (IOException e) {
e.printStackTrace();
}
// 设置编码器的参数
MediaFormat format = MediaFormat.createVideoFormat("video/avc", width, height);
format.setInteger(MediaFormat.KEY_BIT_RATE, 125000);
format.setInteger(MediaFormat.KEY_FRAME_RATE, 30);
format.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 5);
mMediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
// 创建Surface对象,作为编码器的输入源
mSurface = mMediaCodec.createInputSurface();
// 创建MediaMuxer对象,用于将编码后的数据写入文件
try {
mMediaMuxer = new MediaMuxer(getExternalFilesDir(null) + "/output.mp4", MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4);
} catch (IOException e) {
e.printStackTrace();
}
// 开始编码器
mMediaCodec.start();
}
private void encodeFrame() {
// 获取编码器的输出缓冲区
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
int outputBufferIndex = mMediaCodec.dequeueOutputBuffer(bufferInfo, 0);
while (outputBufferIndex >= 0) {
// 将编码后的数据写入文件
if (!mMuxerStarted) {
mTrackIndex = mMediaMuxer.addTrack(mMediaCodec.getOutputFormat());
mMediaMuxer.start();
mMuxerStarted = true;
}
ByteBuffer outputBuffer = mMediaCodec.getOutputBuffer(outputBufferIndex);
mMediaMuxer.writeSampleData(mTrackIndex, outputBuffer, bufferInfo);
mMediaCodec.releaseOutputBuffer(outputBufferIndex, false);
outputBufferIndex = mMediaCodec.dequeueOutputBuffer(bufferInfo, 0);
}
}
private void record() {
// 获取SurfaceView
SurfaceView surfaceView = findViewById(R.id.surface_view);
// 设置SurfaceView的宽和高
int width = 720;
int height = 1280;
surfaceView.getHolder().setFixedSize(width, height);
// 创建一个Surface对象
Surface surface = surfaceView.getHolder().getSurface();
// 创建一个画布
Canvas canvas = surface.lockCanvas(null);
// 绘制一些内容
Paint paint = new Paint();
paint.setColor(Color.RED);
canvas.drawRect(0, 0, width, height, paint);
// 解锁画布
surface.unlockCanvasAndPost(canvas);
// 将Surface对象作为输入源连接到编码器上
mMediaCodec.setInputSurface(mSurface);
// 开始录制
long startTime = SystemClock.elapsedRealtime();
while (SystemClock.elapsedRealtime() - startTime < 5000) {
// 绘制一些内容
canvas = surface.lockCanvas(null);
paint.setColor(Color.BLUE);
canvas.drawRect(0, 0, width, height, paint);
surface.unlockCanvasAndPost(canvas);
// 编码一帧数据
encodeFrame();
}
// 停止录制
mMediaCodec.stop();
mMediaCodec.release();
mMediaMuxer.stop();
mMediaMuxer.release();
}
}
```
这个示例代码创建了一个720x1280的SurfaceView,使用Canvas在SurfaceView上绘制了一些内容。然后创建了一个H.264编码器,并将其输入源设置为通过`createInputSurface`方法创建的Surface对象。接着开始录制,每隔一段时间绘制一帧内容,并将其编码后写入文件中。最后停止录制并释放资源。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)