我是Android新视频录制等先进技术的新手。我正在尝试创建一个简单的应用程序来录制视频。
以下是我用来录制视频的代码。
private PrintWriter out = null;
private final MediaRecorder mediaRecorder = new MediaRecorder();
private final Camera camera = getCameraInstance();
private CameraPreview surfaceView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = new CameraPreview(this, camera);
FrameLayout frameLayout = (FrameLayout)findViewById(R.id.frameLayout);
frameLayout.addView(surfaceView);
Parameters params = camera.getParameters();
List<Size> sizes = params.getSupportedPreviewSizes();
Size optimalSize = getOptimalPreviewSize(sizes, 200, 200);
params.setPreviewSize(optimalSize.width, optimalSize.height);
mediaRecorder.setCamera(camera);
mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
mediaRecorder.setVideoSize(optimalSize.width, optimalSize.height);
mediaRecorder.setOutputFile("/sdcard/test.mpg");
}
我在按钮的onClick()方法中调用start()方法
public void startRecording(View view){
try {
mediaRecorder.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
releaseMediaRecorder();
} catch(Exception e){
e.printStackTrace();
releaseMediaRecorder();
}
}
这是我的SurfaceView代码
/** A basic Camera preview class */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private final Camera mCamera;
private final Context context;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
this.context = context;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(this.getClass().getName(), "Error setting camera preview: " + e.getMessage());
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
if(context instanceof MainActivity){
((MainActivity)context).stopMediaPlayer();
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
if (mHolder.getSurface() == null){
return;
}
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
if(context instanceof MainActivity){
((MainActivity)context).prepareMediaPlayer();
}
} catch (Exception e){
Log.d(this.getClass().getName(), "Error starting camera preview: " + e.getMessage());
e.printStackTrace();
}
}
}
我收到以下错误
08-01 16:17:39.121: I/MediaRecorderJNI(29423): prepare: surface=0x202790 (identity=1681)
08-01 16:17:44.911: E/MediaRecorder(29423): start failed: -16
08-01 16:17:44.911: W/System.err(29423): java.lang.RuntimeException: start failed.
08-01 16:17:44.911: W/System.err(29423): at android.media.MediaRecorder.start(Native Method)
08-01 16:17:44.911: W/System.err(29423): at com.sample.recorder.MainActivity.startRecording(MainActivity.java:149)
08-01 16:17:44.911: W/System.err(29423): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 16:17:44.911: W/System.err(29423): at java.lang.reflect.Method.invoke(Method.java:507)
08-01 16:17:44.911: W/System.err(29423): at android.view.View$1.onClick(View.java:2163)
08-01 16:17:44.911: W/System.err(29423): at android.view.View.performClick(View.java:2552)
08-01 16:17:44.911: W/System.err(29423): at android.view.View$PerformClick.run(View.java:9229)
08-01 16:17:44.911: W/System.err(29423): at android.os.Handler.handleCallback(Handler.java:587)
08-01 16:17:44.911: W/System.err(29423): at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 16:17:44.911: W/System.err(29423): at android.os.Looper.loop(Looper.java:130)
08-01 16:17:44.911: W/System.err(29423): at android.app.ActivityThread.main(ActivityThread.java:3701)
08-01 16:17:44.911: W/System.err(29423): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 16:17:44.911: W/System.err(29423): at java.lang.reflect.Method.invoke(Method.java:507)
08-01 16:17:44.911: W/System.err(29423): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
08-01 16:17:44.911: W/System.err(29423): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
08-01 16:17:44.911: W/System.err(29423): at dalvik.system.NativeStart.main(Native Method)
08-01 16:17:44.921: I/com.sample.recorder.MainActivity(29423): Media Recorder released successfully.
在该错误代码中,当活动加载时打印第1行,点击开始按钮时产生的其余行在内部调用startRecording()函数
AndroidManifestFile中使用的权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.STORAGE" />
答案 0 :(得分:3)
好的,所以我设法通过删除
解决了这个错误mediaRecorder.setVideoSize(width, height)
在3.0之前的Android版本的设备上(HoneyComb)
这是我的代码:
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
} else {
if (! isFrontCamera) {
CamcorderProfile camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
mMediaRecorder.setProfile(camcorderProfile);
} else {
CamcorderProfile camcorderProfile = CamcorderProfile.get(mFrontCamId, CamcorderProfile.QUALITY_LOW);
mMediaRecorder.setProfile(camcorderProfile);
}
mMediaRecorder.setVideoSize(720, 480);
}
mMediaRecorder.setOutputFile(filePath);
mMediaRecorder.setMaxDuration(60000);
mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
try {
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
} catch (IOException e) {
releaseMediaRecorder();
}
在此更改后,它适用于我拥有的所有测试设备。
希望这对你也有帮助。
答案 1 :(得分:0)
尝试使用以下方法进行更改:
private PrintWriter out = null;
private final MediaRecorder mediaRecorder;
private final Camera camera;
private CameraPreview surfaceView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaRecorder = new MediaRecorder();
camera = getCameraInstance();}