在过去的几天里,我一直在尝试解决这个错误,但没有运气。我正在尝试构建一个应用程序来记录我的 RTP 传输的视频文件以发送到服务器。LOGCAT 不提供任何其他消息
Camera Error 1001
并且消息不断重复,直到我停止应用程序。相机初始化并且准备运行正常。调用 MediaRecorder.start() 时会引发错误。
这是录像机类的代码。
MediaRecorder mMediaRec;
Camera mCamera;
Button startRecording = null;
SurfaceView mSurfaceView;
SurfaceHolder mSurfaceHolder;
//CameraPreview mPreview;
boolean isRecording = false;
public static Socket sock;
public Uri filePath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media_streamer);
// Show the Up button in the action bar.
setupActionBar();
//addListenerOnButton();
Log.d(" Streamer On create", "Initializing camera");
initialize();
//startRecording();
}
/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.media_streamer, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public void initialize()
{
startRecording = (Button)findViewById(R.id.button_start);
mCamera = Camera.open();
Log.d("Initialization", "Camera opened");
mSurfaceView = (SurfaceView)findViewById(R.id.survace_camera);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//mSurfaceHolder.setType(SurfaceHholder.SURFACE_TYPE_PUSH_BUFFERS);
Log.d("Streamer init", "Camera Initialized");
}
protected void startRecording()
{
//Socket sock = new Socket();
Log.d("Start recording method","Attempting to start recording");
try
{
mMediaRec = new MediaRecorder();
mMediaRec.setPreviewDisplay(mSurfaceHolder.getSurface());
mCamera.unlock();
mMediaRec.setCamera(mCamera);
mMediaRec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRec.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRec.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mMediaRec.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
filePath = getOutputMediaFileUri();
mMediaRec.setOutputFile(filePath.getPath());
mMediaRec.setMaxFileSize(50000);
Thread.sleep(2000);
//mMediaRec.set
//mCamera.unlock();
mMediaRec.prepare();
Log.d("Start recording", "Recorder params set");
Thread.sleep(2000);
mMediaRec.start();
isRecording = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//StreamerBGManager bg = new StreamerBGManager();
//bg.doInBackground(new String[]{});
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch(IllegalStateException e)
{
e.printStackTrace();
releaseMediaRecorder();
releaseCamera();
}
}
protected void stopRecording()
{
mMediaRec.stop();
mMediaRec.release();
mCamera.release();
releaseMediaRecorder();
releaseCamera();
isRecording = false;
}
private void releaseMediaRecorder()
{
if(mMediaRec != null)
{
mMediaRec.reset();
mMediaRec.release();
mMediaRec = null;
mCamera.lock();
isRecording = false;
}
}
private void releaseCamera()
{
if (mCamera != null){
mCamera.release(); // release the camera for other applications
mCamera = null;
isRecording = false;
}
}
public void toggleRecord(View view)
{
if(isRecording)
stopRecording();
else
startRecording();
}
/*public void addListenerOnButton()
{
startRecording.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(isRecording)
stopRecording();
else
startRecording();
}
});
}*/
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (mCamera != null){
Parameters params = mCamera.getParameters();
mCamera.setParameters(params);
try {
mCamera.setPreviewDisplay(mSurfaceHolder);
mCamera.startPreview();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
Toast.makeText(getApplicationContext(), "Camera not available!", Toast.LENGTH_LONG).show();
finish();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
//mMediaRec.release();
}