0
我在運行mediarecorder的start()時出現此錯誤。 以下是我的代碼。我的cose有什麼問題? 代碼:MediaRecorder啓動失敗
public class RecordVideoActivity extends Activity implements OnClickListener {
private Camera mCamera;
private CameraPreview mPreview;
private MediaRecorder mMediaRecorder;
private boolean isRecording = false;
private Button captureButton;
private Button goodButton;
private Button badButton;
Thread thread;
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
ArrayList<TrimPoint> mTrimPoints;
Timer mTimer;
double currentTime=0;
static String filepath;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recordvideo);
loadGUI();
mTimer = new Timer();
mTrimPoints = new ArrayList<TrimPoint>();
// Create an instance of Camera
mCamera = getCameraInstance();
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
}
private void loadGUI() {
captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setOnClickListener(this);
goodButton = (Button) findViewById(R.id.button_good);
goodButton.setOnClickListener(this);
badButton = (Button) findViewById(R.id.button_bad);
badButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_capture:
captureButtonHandler();
break;
case R.id.button_bad:
badButtonHandler();
break;
case R.id.button_good:
goodButtonHandler();
break;
default:
break;
}
}
private void goodButtonHandler() {
TrimPoint mTrimPoint = new TrimPoint(true,currentTime);
mTrimPoints.add(mTrimPoint);
}
private void badButtonHandler() {
TrimPoint mTrimPoint = new TrimPoint(false,currentTime);
mTrimPoints.add(mTrimPoint);
Camera.Parameters params = mCamera.getParameters();
params.setColorEffect(Camera.Parameters.EFFECT_MONO);
mCamera.setParameters(params);
if(thread != null){
thread = null;
}
thread = new Thread()
{
@Override
public void run() {
try {
//while(true) {
int time = 5000;
Log.d("HFI","Time: "+time);
sleep(time);
Camera.Parameters params = mCamera.getParameters();
params.setColorEffect(Camera.Parameters.EFFECT_NONE);
mCamera.setParameters(params);
//}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
}
private void captureButtonHandler() {
if (isRecording) {
// stop recording and release camera
mMediaRecorder.stop(); // stop the recording
mTimer.cancel();
releaseMediaRecorder(); // release the MediaRecorder object
mCamera.lock(); // take camera access back from MediaRecorder
// inform the user that recording has stopped
captureButton.setText("Capture");
mCamera.startPreview();
isRecording = false;
Intent intent = new Intent(Intent.ACTION_VIEW); //I encourage using this instead of specifying the string "android.intent.action.VIEW"
intent.setDataAndType(Uri.parse(filepath), "video/mp4");
startActivity(intent);
} else {
// initialize video camera
if (prepareVideoRecorder()) {
// Camera is available and unlocked, MediaRecorder is
// prepared,
// now you can start recording
mMediaRecorder.start();
// inform the user that recording has started
captureButton.setText("Stop");
isRecording = true;
mTimer.schedule(new TimerTask() {
@Override
public void run() {
currentTime = currentTime+1;
}
}, 1000,1000);
} else {
// prepare didn't work, release the camera
releaseMediaRecorder();
// inform user
}
}
}
private final Handler handler = new Handler() {
@Override
public void handleMessage(final Message msg) {
super.handleMessage(msg);
//start Asyntask here. progress show/hide should be done in asynctaswk itself.
}
};
/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
if (c != null) {
Camera.Parameters params = c.getParameters();
c.setParameters(params);
}
} catch (Exception e) {
Log.d("DEBUG", "Camera did not open: " + e.toString());
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
private boolean prepareVideoRecorder() {
if(mMediaRecorder == null){
mMediaRecorder = new MediaRecorder();
}else{
Log.d(Constants.TAG,"MediaRecoder is Not Null");
}
// Step 1: Unlock and set camera to MediaRecorder
mCamera.stopPreview();
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
// Step 2: Set sources
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
// Step 4: Set output file
mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
// Step 5: Set the preview output
mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
// Step 6: Prepare configured MediaRecorder
try {
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
Log.d("DEBUG", "IllegalStateException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
} catch (IOException e) {
Log.d("DEBUG", "IOException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
}
return true;
}
/** Create a file Uri for saving an image or video */
@SuppressWarnings("unused")
private static Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
@SuppressLint("SimpleDateFormat")
private static File getOutputMediaFile(int type) {
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),"HFVideos");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
} else if (type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
} else {
return null;
}
filepath = mediaFile.toString();
Log.d(Constants.TAG,"Path: "+filepath);
return mediaFile;
}
@Override
protected void onPause() {
super.onPause();
releaseMediaRecorder(); // if you are using MediaRecorder, release it
// first
releaseCamera(); // release the camera immediately on pause event
}
private void releaseMediaRecorder() {
if (mMediaRecorder != null) {
mMediaRecorder.reset(); // clear recorder configuration
mMediaRecorder.release(); // release the recorder object
mMediaRecorder = null;
mCamera.setPreviewCallback(null);
mCamera.lock(); // lock camera for later use
}
}
private void releaseCamera() {
if (mCamera != null) {
isRecording = false;
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
}
的logcat:
03-27 10:00:17.849: I/MediaRecorderJNI(2736): prepare: surface=0x26c898 (identity=147)
03-27 10:00:17.869: E/MediaRecorder(2736): start failed: -19
03-27 10:00:17.869: D/AndroidRuntime(2736): Shutting down VM
03-27 10:00:17.869: W/dalvikvm(2736): threadid=1: thread exiting with uncaught exception (group=0x40a92930)
03-27 10:00:17.869: E/AndroidRuntime(2736): FATAL EXCEPTION: main
03-27 10:00:17.869: E/AndroidRuntime(2736): java.lang.RuntimeException: start failed.
03-27 10:00:17.869: E/AndroidRuntime(2736): at android.media.MediaRecorder.start(Native Method)
03-27 10:00:17.869: E/AndroidRuntime(2736): at uk.org.humanfocus.hfi.RecordVideoActivity.captureButtonHandler(RecordVideoActivity.java:151)
03-27 10:00:17.869: E/AndroidRuntime(2736): at uk.org.humanfocus.hfi.RecordVideoActivity.onClick(RecordVideoActivity.java:77)
03-27 10:00:17.869: E/AndroidRuntime(2736): at android.view.View.performClick(View.java:4202)
03-27 10:00:17.869: E/AndroidRuntime(2736): at android.view.View$PerformClick.run(View.java:17344)
03-27 10:00:17.869: E/AndroidRuntime(2736): at android.os.Handler.handleCallback(Handler.java:725)
03-27 10:00:17.869: E/AndroidRuntime(2736): at android.os.Handler.dispatchMessage(Handler.java:92)
03-27 10:00:17.869: E/AndroidRuntime(2736): at android.os.Looper.loop(Looper.java:137)
03-27 10:00:17.869: E/AndroidRuntime(2736): at android.app.ActivityThread.main(ActivityThread.java:5191)
03-27 10:00:17.869: E/AndroidRuntime(2736): at java.lang.reflect.Method.invokeNative(Native Method)
03-27 10:00:17.869: E/AndroidRuntime(2736): at java.lang.reflect.Method.invoke(Method.java:511)
03-27 10:00:17.869: E/AndroidRuntime(2736): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
03-27 10:00:17.869: E/AndroidRuntime(2736): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
03-27 10:00:17.869: E/AndroidRuntime(2736): at dalvik.system.NativeStart.main(Native Method)
添加了一些文本,因爲刪除下面的錯誤,我張貼在我得到。
它看起來像你的文章主要是代碼;請添加更多的細節。
刪除了我的答案 - 我犯了一個錯誤,這是對一個球員沒有一個記錄。 – 2013-03-27 05:48:08