gpt4 book ai didi

android - 使用 MediaRecorder 录制 MPEG TS

转载 作者:太空宇宙 更新时间:2023-11-03 10:49:50 28 4
gpt4 key购买 nike

我正在尝试录制 mpeg2-ts 视频,以便使用 Android MediaRecorder 类将其流式传输到 server/socket,如所述here ...

代码如下:

public class MediaRecorderDemo extends Activity
{
private final static String TAG = "MediaRecorderDemo";
Camera camera;
CameraPreview cameraPreview;
MediaRecorder mediaRecorder;
File outputFile = new File(Environment.getExternalStorageDirectory().getPath() + "/out1.ts");

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.media_recorder_demo_layout);

camera = getCameraInstance();
cameraPreview = new CameraPreview(this);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(cameraPreview);
}

private Camera getCameraInstance()
{
final String FUNCTION = "getCameraInstance";

Camera c = null;
try
{
c = Camera.open();
}
catch(Exception e)
{
Log.e(TAG, FUNCTION + " : " + e.getMessage());
}

return c;
}

void initMediaRecorder()
{
final String FUNCTION = "initMediaRecorder";
FileDescriptor outputFileFD = null;
try
{
outputFile.createNewFile();
outputFileFD = new FileOutputStream(outputFile).getFD();
}
catch(Exception e)
{
Log.e(TAG, FUNCTION + " : " + e.getMessage());
}

mediaRecorder = new MediaRecorder();
mediaRecorder.setOnErrorListener(new MediaRecorder.OnErrorListener() {
final static String TAG = "MediaRecorder.onErrorListener";

@Override
public void onError(MediaRecorder mr, int what, int extra) {
Log.e(TAG, "Error : " + what + " " + extra);
}
});

camera.unlock();
mediaRecorder.setPreviewDisplay(cameraPreview.getHolder().getSurface());
mediaRecorder.setCamera(camera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
//mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
mediaRecorder.setOutputFormat(8);
Log.d(TAG, "File Exists : " + outputFile.exists());
mediaRecorder.setOutputFile(outputFileFD);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
//mediaRecorder.setVideoSize(640, 480);
mediaRecorder.setMaxDuration(-1);
//mediaRecorder.setVideoFrameRate(16);
mediaRecorder.setVideoEncodingBitRate(1024 * 1024);

try
{
mediaRecorder.prepare();
Log.d(TAG, "MediaRecorder Prepared.");
mediaRecorder.start();

}
catch(Exception e)
{
Log.e(TAG, FUNCTION + " : " + e.getMessage());
//releaseMediaRecorder();
}
}

void releaseMediaRecorder()
{
final String FUNCTION = "releaseMediaRecorder";

try
{
if(mediaRecorder != null)
{
mediaRecorder.stop();
mediaRecorder.reset();
mediaRecorder.release();
mediaRecorder = null;
camera.lock();
}
}

catch(Exception e)
{
Log.e(TAG, FUNCTION + " : " + e.getMessage());
}
}

void releaseCamera()
{
final String FUNCTION = "releaseCamera";
try
{
if(camera != null)
{
camera.stopPreview();
camera.release();
}
camera = null;
}
catch(Exception e)
{
Log.e(TAG, FUNCTION + " : " + e.getMessage());
}
}

@Override
public void onStart()
{
super.onStart();
}

@Override
public void onPause()
{
super.onPause();
}

@Override
public void onResume()
{
super.onResume();
}

@Override
public void onStop()
{
super.onStop();
}

@Override
public void onDestroy()
{
super.onDestroy();
}

public class CameraPreview extends SurfaceView
{
private final static String TAG = "CameraPreview";
SurfaceHolder holder;
boolean isPreviewDisplaySet;

public CameraPreview(Context context)
{
this(context, (AttributeSet)null);

this.holder = getHolder();
this.holder.addCallback(new SurfaceHolderCallback());
}

public CameraPreview(Context context, AttributeSet attrSet)
{
this(context, attrSet, 0);
}

public CameraPreview(Context context, AttributeSet attrSet, int defStyle)
{
super(context, attrSet, defStyle);
}

private void releaseCamera()
{
if(camera != null)
{
camera.release();
camera = null;
}
}

private class SurfaceHolderCallback implements SurfaceHolder.Callback
{
@Override
public void surfaceCreated(SurfaceHolder holder)
{
final String FUNCTION = "surfaceCreated";
Log.d(TAG, "Surface Created.");

try
{
camera.setPreviewDisplay(holder);
camera.startPreview();

initMediaRecorder();
//mediaRecorder.start();
}
catch(Exception e)
{
Log.e(TAG, FUNCTION + " : " + e.getMessage());
}
}

@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
final String FUNCTION = "surfaceDestroyed";
Log.d(TAG, "Surface Destroyed.");

try
{
releaseMediaRecorder();
releaseCamera();
}
catch(Exception e)
{
Log.e(TAG, FUNCTION + " : " + e.getMessage());
}
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
{
final String FUNCTION = "surfaceChanged";
Log.d(TAG, "Surface Changed.");

if(holder.getSurface() == null)
return;

try
{
camera.stopPreview();
}
catch(Exception e)
{
Log.e(TAG, FUNCTION + " : " + e.getMessage());
}

try
{
camera.setPreviewDisplay(holder);
camera.startPreview();
}
catch(Exception e)
{
Log.e(TAG, FUNCTION + " : " + e.getMessage());
}
}
}
}
}

调用 mediaRecorder.prepare()mediaRecorder.start() 没有任何错误,并显示相机预览...但是过了一会儿预览被切断然后屏幕卡住并在输出路径创建一个空文件......Android Issue List 上也报告了此问题但还没有被整改...

我曾尝试在带有 Stock Android ICS ROM 的 Galaxy Note N7000 以及带有自定义 Android 4.2 ROM 的 Samsung Galaxy Tab 2 P3100 上运行相同的应用程序...所以它似乎与以下内容无关ROM 或特定的硬件配置...

如果有什么遗漏或做错了,知道后会很高兴并且肯定会松一口气......?

谢谢...

最佳答案

解决方案是在 M2ts Writer 中应用补丁。构建 libstagefright.so 并推送到设备。在application下面也设置

recorder.setAudioSamplingRate(48000); 
recorder.setAudioEncodingBitRate(128000);

否则将无法完整录制剪辑。不过,我没有深究设置上述参数的原因。

libstagefright 中的 M2tsWriter 补丁:

diff --git a/media/libstagefright/MPEG2TSWriter.cpp b/media/libstagefright/MPEG2TSWriter.cpp

index c9ed5bb..a42371f 100644

--- a/media/libstagefright/MPEG2TSWriter.cpp

+++ b/media/libstagefright/MPEG2TSWriter.cpp

@@ -411,6 +411,7 @@ void MPEG2TSWriter::SourceInfo::onMessageReceived(const sp<AMessage> &msg) {
(const uint8_t *)buffer->data()
+ buffer->range_offset(),
buffer->range_length());
+ readMore();
} else if (buffer->range_length() > 0) {
if (mStreamType == 0x0f) {
if (!appendAACFrames(buffer)) {

关于android - 使用 MediaRecorder 录制 MPEG TS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15535335/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com