Problème pour enregistrer une vidéo avec vue en temps réel
Bonjour à tous,
j'ai suivi le tutoriel pour l'enregistrement vidéo + preview du livre sur le développement Android 2 de Reto Meier, la preview fonctionne mais l'enregistrement arrête mon application. J'ai suivi ensuite d'autre tutoriel mais je n'avance pas. La méthode start() de la classe MediaRecorder semble bloquer mon programme.
Voici ma classe principale :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| package com.example.test2;
import java.io.IOException;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements SurfaceHolder.Callback, OnClickListener{
private Camera mCamera = null;
private MediaRecorder recorder = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SurfaceView surface = (SurfaceView)findViewById(R.id.surfaceView);
SurfaceHolder holder = surface.getHolder();
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.addCallback(this);
}
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
protected void onResume() {
super.onResume();
mCamera = Camera.open(CameraInfo.CAMERA_FACING_FRONT);
}
@Override
protected void onPause() {
super.onPause();
mCamera.release();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.RecordingButton:
recorder = new MediaRecorder();
mCamera.unlock();
recorder.setCamera(mCamera);
recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFile("/sdcard/video.mp4");
try {
recorder.prepare();
} catch (IllegalStateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
recorder.start();
break;
case R.id.StopRecordingButton:
recorder.stop();
recorder.release();
recorder = null;
break;
default:
break;
}
}
} |
Le manifest :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.test2.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest> |
Ma vue en XML :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/RecordingButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Start Recording" />
<Button
android:id="@+id/StopRecordingButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Stop Recording" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</SurfaceView>
</FrameLayout>
</LinearLayout> |
et mon erreur :
Citation:
05-03 11:31:59.770: D/HAL(1752): [HAL] hw_get_module:share library path:/system/lib/hw/gralloc.tegra.so
05-03 11:32:03.490: E/MediaRecorder(1752): setOutputFile called in an invalid state(2)
05-03 11:32:03.490: W/System.err(1752): java.lang.IllegalStateException
05-03 11:32:03.490: W/System.err(1752): at android.media.MediaRecorder._setOutputFile(Native Method)
05-03 11:32:03.490: W/System.err(1752): at android.media.MediaRecorder.prepare(MediaRecorder.java:646)
05-03 11:32:03.490: W/System.err(1752): at com.example.test2.MainActivity.onClick(MainActivity.java:74)
05-03 11:32:03.490: W/System.err(1752): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 11:32:03.490: W/System.err(1752): at java.lang.reflect.Method.invoke(Method.java:491)
05-03 11:32:03.490: W/System.err(1752): at android.view.View$1.onClick(View.java:2678)
05-03 11:32:03.490: W/System.err(1752): at android.view.View.performClick(View.java:3110)
05-03 11:32:03.490: W/System.err(1752): at android.view.View$PerformClick.run(View.java:11934)
05-03 11:32:03.490: W/System.err(1752): at android.os.Handler.handleCallback(Handler.java:587)
05-03 11:32:03.490: W/System.err(1752): at android.os.Handler.dispatchMessage(Handler.java:92)
05-03 11:32:03.490: W/System.err(1752): at android.os.Looper.loop(Looper.java:132)
05-03 11:32:03.490: W/System.err(1752): at android.app.ActivityThread.main(ActivityThread.java:4123)
05-03 11:32:03.490: W/System.err(1752): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 11:32:03.490: W/System.err(1752): at java.lang.reflect.Method.invoke(Method.java:491)
05-03 11:32:03.490: W/System.err(1752): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-03 11:32:03.490: W/System.err(1752): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-03 11:32:03.490: W/System.err(1752): at dalvik.system.NativeStart.main(Native Method)
05-03 11:32:03.490: E/MediaRecorder(1752): start called in an invalid state: 2
05-03 11:32:03.490: D/AndroidRuntime(1752): Shutting down VM
05-03 11:32:03.490: W/dalvikvm(1752): threadid=1: thread exiting with uncaught exception (group=0x401d5760)
05-03 11:32:03.500: E/AndroidRuntime(1752): FATAL EXCEPTION: main
05-03 11:32:03.500: E/AndroidRuntime(1752): java.lang.IllegalStateException: Could not execute method of the activity
05-03 11:32:03.500: E/AndroidRuntime(1752): at android.view.View$1.onClick(View.java:2683)
05-03 11:32:03.500: E/AndroidRuntime(1752): at android.view.View.performClick(View.java:3110)
05-03 11:32:03.500: E/AndroidRuntime(1752): at android.view.View$PerformClick.run(View.java:11934)
05-03 11:32:03.500: E/AndroidRuntime(1752): at android.os.Handler.handleCallback(Handler.java:587)
05-03 11:32:03.500: E/AndroidRuntime(1752): at android.os.Handler.dispatchMessage(Handler.java:92)
05-03 11:32:03.500: E/AndroidRuntime(1752): at android.os.Looper.loop(Looper.java:132)
05-03 11:32:03.500: E/AndroidRuntime(1752): at android.app.ActivityThread.main(ActivityThread.java:4123)
05-03 11:32:03.500: E/AndroidRuntime(1752): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 11:32:03.500: E/AndroidRuntime(1752): at java.lang.reflect.Method.invoke(Method.java:491)
05-03 11:32:03.500: E/AndroidRuntime(1752): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-03 11:32:03.500: E/AndroidRuntime(1752): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-03 11:32:03.500: E/AndroidRuntime(1752): at dalvik.system.NativeStart.main(Native Method)
05-03 11:32:03.500: E/AndroidRuntime(1752): Caused by: java.lang.reflect.InvocationTargetException
05-03 11:32:03.500: E/AndroidRuntime(1752): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 11:32:03.500: E/AndroidRuntime(1752): at java.lang.reflect.Method.invoke(Method.java:491)
05-03 11:32:03.500: E/AndroidRuntime(1752): at android.view.View$1.onClick(View.java:2678)
05-03 11:32:03.500: E/AndroidRuntime(1752): ... 11 more
05-03 11:32:03.500: E/AndroidRuntime(1752): Caused by: java.lang.IllegalStateException
05-03 11:32:03.500: E/AndroidRuntime(1752): at android.media.MediaRecorder.start(Native Method)
05-03 11:32:03.500: E/AndroidRuntime(1752): at com.example.test2.MainActivity.onClick(MainActivity.java:83)
05-03 11:32:03.500: E/AndroidRuntime(1752): ... 14 more
Merci d'avance pour les solutions.
À bientôt