所以现在我尝试了一个例子并下载了源代码。我用你的代码修改了它,它在 API 8 上没有保存声音。这是主类:
public class AndroidVideoCapture extends Activity implements SurfaceHolder.Callback{
Button myButton;
MediaRecorder mediaRecorder;
SurfaceHolder surfaceHolder;
boolean recording;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recording = false;
mediaRecorder = new MediaRecorder();
initMediaRecorder();
setContentView(R.layout.main);
SurfaceView myVideoView = (SurfaceView)findViewById(R.id.videoview);
surfaceHolder = myVideoView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
myButton = (Button)findViewById(R.id.mybutton);
myButton.setOnClickListener(myButtonOnClickListener);
}
private Button.OnClickListener myButtonOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(recording){
mediaRecorder.stop();
mediaRecorder.release();
finish();
}else{
mediaRecorder.start();
recording = true;
myButton.setText("STOP");
}
}};
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
prepareMediaRecorder();
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
private void initMediaRecorder(){
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mediaRecorder.setOutputFile("/sdcard/myvideo.mp4");
}
private void prepareMediaRecorder(){
mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
与您的代码不同的是,在本教程中没有实例化相机。它仅适用于 MediaRecorder。另一个区别是 prepare() 和 start() 方法是分开的。MediaRecorder 在启动应用程序时初始化,在创建 SurfaceView 时准备好,并在单击按钮时启动。也许您也应该尝试将它们分开并且不要使用相机。请以这种方式尝试,如果可行,请给我反馈。