我正在尝试一个星期来录制视频,并在尝试了示例和示例后,这是不可能的,我总是遇到类似的问题。我的代码是以下代码:
public class VideoRecorder extends Activity{
private final static String TAG = "Playing";
private MediaRecorder recorder;
private Preview mPreview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
recorder = new MediaRecorder();
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
mPreview = new Preview(this,recorder);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(mPreview);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
menu.add(0, 0, 0, "StartRecording");
menu.add(0, 1, 0, "StopRecording");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId())
{
case 0:
try {
recorder.start();
} catch (Exception e) {
Log.w(TAG, e);
recorder.release();
}
break;
case 1: //GoToAllNotes
recorder.stop();
recorder.release();
recorder = null;
finish();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
class Preview extends SurfaceView implements SurfaceHolder.Callback{
private SurfaceHolder mHolder;
private MediaRecorder tempRecorder;
Preview(Context context, MediaRecorder recorder){
super(context);
tempRecorder = recorder;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public Surface getSurface(){
return mHolder.getSurface();
}
public void surfaceCreated(SurfaceHolder holder){
tempRecorder.setOutputFile("/sdcard/myVideo.3gp");
tempRecorder.setPreviewDisplay(mHolder.getSurface());
try{
tempRecorder.prepare();
} catch (Exception e) {
tempRecorder.release();
tempRecorder = null;
}
}
public void surfaceDestroyed(SurfaceHolder holder){
if(tempRecorder!=null){
tempRecorder.release();
tempRecorder = null;
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h){
}
}
这个是一个简单的应用程序,它正在录制视频,但经过尝试和尝试,它不工作,问题是:
媒体录制器:准备失败-17
如果有人知道如何让它正常工作......我想要一个帮助因为我对此非常绝望。
顺便说一句,对于那些会告诉我添加权限的人,是的,我添加了它们:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<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-feature android:name="android.hardware.camera" android:required="true" />
我试过其他的例子,总是发现一个问题,如果不是准备-17,那就是启动-0 ............
非常感谢!
答案 0 :(得分:0)
如果您的设备支持所请求的MPEG_4_SP
视频格式,则可以询问CamcorderProfile api。
根据Android Supported Media Formats,不支持MPEG_4_SP
编码直到android 4.
可能正在使用h.263
为您服务。
答案 1 :(得分:0)
您必须按照此处所述的相同顺序致电MediaRecorder
和Camera
方法:http://developer.android.com/guide/topics/media/camera.html#capture-video