Also irgendwie komm ich grad nicht weiter ...
Mein bisheriger Code schaut wie Folgt aus:
Java
public class MainActivity extends Activity implements SurfaceHolder.Callback {
private Camera camera = null;
private SurfaceView surface = null;
private SurfaceHolder holder = null;
private MediaRecorder recorder = new MediaRecorder();
private boolean isRecording = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Camera.CameraInfo info = new CameraInfo();
LinearLayout ll = (LinearLayout) findViewById(R.id.llButtons);
for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
Camera.getCameraInfo(i, info);
Button b = new Button(this);
if (info.facing == CameraInfo.CAMERA_FACING_BACK) {
b.setText("Rückseite");
} else {
b.setText("Vorderseite");
}
b.setOnClickListener(new MyClickListener(i));
ll.addView(b);
}
}
private class MyClickListener implements View.OnClickListener {
private int cam = -1;
public MyClickListener(int i) {
this.cam = i;
}
@Override
public void onClick(View v) {
if (camera != null) {
camera.stopPreview();
camera.release();
}
camera = Camera.open(cam);
camera.setDisplayOrientation(90);
if (surface != null) {
holder.removeCallback(MainActivity.this);
holder = null;
surface.destroyDrawingCache();
surface = null;
}
surface = new SurfaceView(MainActivity.this);
LinearLayout llSurface = (LinearLayout) findViewById(R.id.llSurface);
llSurface.removeAllViews();
llSurface.addView(surface);
holder = surface.getHolder();
holder.addCallback(MainActivity.this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if (holder.getSurface() == null || surface == null) {
return;
}
camera.stopPreview();
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
} catch (IOException e) {
Log.e(MainActivity.class.getSimpleName(), e.getMessage());
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (camera != null) {
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
} catch (IOException e) {
Log.e(MainActivity.class.getSimpleName(), e.getMessage());
}
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (isRecording) {
recorder.stop();
isRecording = false;
}
recorder.release();
LinearLayout llSurface = (LinearLayout) findViewById(R.id.llSurface);
llSurface.removeAllViews();
surface.destroyDrawingCache();
this.holder = null;
surface = null;
camera.release();
camera = null;
}
public void clickHandler(View v) {
switch (v.getId()) {
case R.id.btnStart:
if (!isRecording) {
recorder.release();
recorder = new MediaRecorder();
initRecorder();
prepareRecorder();
recorder.start();
isRecording = true;
}
break;
case R.id.btnStop:
if (isRecording) {
recorder.stop();
isRecording = false;
}
recorder.release();
recorder = null;
break;
default:
break;
}
}
private void initRecorder() {
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(profile);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
File video = new File(Environment.getExternalStorageDirectory() + "/DCIM/");
String videoname = "test_" + System.currentTimeMillis() + ".3gp";
recorder.setOutputFile(video.getAbsolutePath() + "/" + videoname);
}
private void prepareRecorder() {
recorder.setPreviewDisplay(holder.getSurface());
try {
recorder.prepare();
} catch (IllegalStateException e) {
Log.e(MainActivity.class.getSimpleName(), e.getMessage());
} catch (IOException e) {
Log.e(MainActivity.class.getSimpleName(), e.getMessage());
}
}
}
Alles anzeigen
meine Permissions
HTML
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
Alles anzeigen
und das Ganze führt zu folgendem LogCat output:
HTML
05-06 11:27:07.425: D/libEGL(13422): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
05-06 11:27:07.464: D/libEGL(13422): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
05-06 11:27:07.472: D/libEGL(13422): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
05-06 11:27:07.566: D/OpenGLRenderer(13422): Enabling debug mode 0
05-06 11:27:11.261: E/MediaRecorder(13422): setOutputFormat called in an invalid state: 4
05-06 11:27:11.261: D/AndroidRuntime(13422): Shutting down VM
05-06 11:27:11.261: W/dalvikvm(13422): threadid=1: thread exiting with uncaught exception (group=0x40f7b930)
05-06 11:27:11.277: E/AndroidRuntime(13422): FATAL EXCEPTION: main
05-06 11:27:11.277: E/AndroidRuntime(13422): java.lang.IllegalStateException: Could not execute method of the activity
05-06 11:27:11.277: E/AndroidRuntime(13422): at android.view.View$1.onClick(View.java:3599)
05-06 11:27:11.277: E/AndroidRuntime(13422): at android.view.View.performClick(View.java:4204)
05-06 11:27:11.277: E/AndroidRuntime(13422): at android.view.View$PerformClick.run(View.java:17355)
05-06 11:27:11.277: E/AndroidRuntime(13422): at android.os.Handler.handleCallback(Handler.java:725)
05-06 11:27:11.277: E/AndroidRuntime(13422): at android.os.Handler.dispatchMessage(Handler.java:92)
05-06 11:27:11.277: E/AndroidRuntime(13422): at android.os.Looper.loop(Looper.java:137)
05-06 11:27:11.277: E/AndroidRuntime(13422): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-06 11:27:11.277: E/AndroidRuntime(13422): at java.lang.reflect.Method.invokeNative(Native Method)
05-06 11:27:11.277: E/AndroidRuntime(13422): at java.lang.reflect.Method.invoke(Method.java:511)
05-06 11:27:11.277: E/AndroidRuntime(13422): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-06 11:27:11.277: E/AndroidRuntime(13422): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-06 11:27:11.277: E/AndroidRuntime(13422): at dalvik.system.NativeStart.main(Native Method)
05-06 11:27:11.277: E/AndroidRuntime(13422): Caused by: java.lang.reflect.InvocationTargetException
05-06 11:27:11.277: E/AndroidRuntime(13422): at java.lang.reflect.Method.invokeNative(Native Method)
05-06 11:27:11.277: E/AndroidRuntime(13422): at java.lang.reflect.Method.invoke(Method.java:511)
05-06 11:27:11.277: E/AndroidRuntime(13422): at android.view.View$1.onClick(View.java:3594)
05-06 11:27:11.277: E/AndroidRuntime(13422): ... 11 more
05-06 11:27:11.277: E/AndroidRuntime(13422): Caused by: java.lang.IllegalStateException
05-06 11:27:11.277: E/AndroidRuntime(13422): at android.media.MediaRecorder.setOutputFormat(Native Method)
05-06 11:27:11.277: E/AndroidRuntime(13422): at de.matze.cameratest.MainActivity.initRecorder(MainActivity.java:168)
05-06 11:27:11.277: E/AndroidRuntime(13422): at de.matze.cameratest.MainActivity.clickHandler(MainActivity.java:142)
05-06 11:27:11.277: E/AndroidRuntime(13422): ... 14 more
05-06 11:27:12.722: I/Process(13422): Sending signal. PID: 13422 SIG: 9
Alles anzeigen
Ich würd mich aber eigentlich an die API halten und der Code entspricht auch den Beispielen, die ich so gefunden habe ...
ZitatSets the format of the output file produced during recording. Call this after setAudioSource()/setVideoSource() but before prepare().
Mach ich ja eigentlich ...
Hat jemand eine Idee?
Gruß,
matze
EDIT:
Ich steh echt auf dem Schlauch -.- hab sogar ne Schritt für Schritt Anleitung gefunden, die eigentlich alles klärt, aber ich seh meinen Fehler nicht ... hier der Link: Camera - Capturing videos