Why can't my GUI return back to recording mode despite having service?

  • Replies:0
  • OpenNot stickiedUnanswered

Apr 8, 2011 5:39:42 AM via Website

Hi i'm new to android so that's why i come to find help... Hi an introduction to the app i'm doing here, i'm actually doing an car blackbox app..

I had my service run the video recording processes in the background so i should be able to leave the app for a while and come back to the Video recording GUI with the surfaceview but my application was force to close when i return to the app. So is the problem related to onResume() method or something else? If its yes what i should do in order to get my application GUI to return to the video recording surfaceview and still running the background recording using the service rather than giving me an error? Can someone help please...

1public class CameraTest extends Activity implements SurfaceHolder.Callback {
2
3 private static final String TAG = "Exception";
4
5 public static SurfaceView surfaceView;
6 public static SurfaceHolder surfaceHolder;
7 public static Camera camera;
8 public static boolean previewRunning;
9
10 @Override
11 public void onCreate(Bundle savedInstanceState) {
12 super.onCreate(savedInstanceState);
13 setContentView(R.layout.main);
14
15 surfaceView = (SurfaceView)findViewById(R.id.surface_camera);
16 surfaceHolder = surfaceView.getHolder();
17 surfaceHolder.addCallback(this);
18
19 surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
20
21 Button btnStart = (Button) findViewById(R.id.button4);
22 btnStart.setOnClickListener(new View.OnClickListener()
23 {
24 public void onClick(View v)
25 {
26 startService(new Intent(getApplicationContext(), ServiceRecording.class));
27 }
28 });
29
30 Button btnStop = (Button) findViewById(R.id.button5);
31 btnStop.setOnClickListener(new View.OnClickListener()
32 {
33 public void onClick(View v)
34 {
35 stopService(new Intent(getApplicationContext(), ServiceRecording.class));
36 }
37 });
38 }
39
40 @Override
41 public void surfaceCreated(SurfaceHolder holder) {
42 camera = Camera.open();
43 if (camera != null) {
44 Camera.Parameters params = camera.getParameters();
45 camera.setParameters(params);
46 }
47 else {
48 Toast.makeText(getApplicationContext(), "Camera not available!", Toast.LENGTH_LONG).show();
49 finish();
50 }
51 }
52
53 @Override
54 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
55 if (previewRunning) {
56 camera.stopPreview();
57 }
58
59 Camera.Parameters p = camera.getParameters();
60 p.setPreviewSize(320, 240);
61 p.setPreviewFormat(PixelFormat.JPEG);
62 camera.setParameters(p);
63
64 try {
65 camera.setPreviewDisplay(holder);
66 camera.startPreview();
67 previewRunning = true;
68 }
69 catch (IOException e) {
70 Log.e(TAG,e.getMessage());
71 e.printStackTrace();
72 }
73 }
74
75 @Override
76 public void onResume(){
77 super.onResume();
78
79 }
80
81 @Override
82 public void surfaceDestroyed(SurfaceHolder holder){
83 camera.stopPreview();
84 previewRunning = false;
85 camera.release();
86 }
87}

My RecordingService.java Service file
1public class ServiceRecording extends Service {
2
3 @Override
4 public IBinder onBind(Intent intent) {
5 // TODO Auto-generated method stub
6 return null;
7 }
8
9 private SurfaceView surfaceView;
10 private SurfaceHolder surfaceHolder;
11 private Camera camera;
12 private boolean recordingStatus;
13 private MediaRecorder mediaRecorder;
14 private final int maxDurationInMs = 20000;
15
16 private static final String TAG = "Exception";
17
18 @Override
19 public void onCreate() {
20 super.onCreate();
21
22 recordingStatus = false;
23 camera = CameraTest.camera;
24 surfaceView = CameraTest.surfaceView;
25 surfaceHolder = CameraTest.surfaceHolder;
26 }
27
28 @Override
29 public int onStartCommand(Intent intent, int flags, int startId) {
30 super.onStartCommand(intent, flags, startId);
31 if (recordingStatus == false)
32 startRecording();
33
34 return START_STICKY;
35 }
36
37 @Override
38 public void onDestroy() {
39 super.onDestroy();
40
41 stopRecording();
42 //camera.stopPreview();
43 recordingStatus = false;
44 //camera.release();
45 }
46
47 public boolean startRecording(){
48 try {
49 Toast.makeText(getBaseContext(), "Recording Started", Toast.LENGTH_SHORT).show();
50 try{
51 camera.unlock();
52 }
53 catch(Exception e){
54
55 camera.reconnect();
56 }
57
58 mediaRecorder = new MediaRecorder();
59
60 mediaRecorder.setCamera(camera);
61
62 mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
63
64 mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
65
66 mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
67
68 mediaRecorder.setMaxDuration(maxDurationInMs);
69
70 mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
71
72 mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
73
74 //mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
75 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH_mm_ss");
76 Date date = new Date();
77 File directory = new File(Environment.getExternalStorageDirectory() + "/VideoList");
78
79 if(!(directory.exists()))
80 directory.mkdir();
81
82 File FileSaved = new File(Environment.getExternalStorageDirectory() + "/VideoList", dateFormat.format(date) + ".3gp");
83 mediaRecorder.setOutputFile(FileSaved.getPath());
84
85 mediaRecorder.setVideoSize(surfaceView.getWidth(),surfaceView.getHeight());
86
87 //mediaRecorder.setVideoFrameRate(videoFramesPerSecond);
88
89 mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
90
91 mediaRecorder.prepare();
92 mediaRecorder.start();
93
94 recordingStatus = true;
95
96 return true;
97 }
98
99 catch (IllegalStateException e) {
100 Log.d(TAG,e.getMessage());
101 e.printStackTrace();
102 return false;
103 }
104 catch (IOException e) {
105 Log.d(TAG,e.getMessage());
106 e.printStackTrace();
107 return false;
108 }
109 }
110
111 public void stopRecording() {
112 Toast.makeText(getBaseContext(), "Recording Stopped", Toast.LENGTH_SHORT).show();
113 mediaRecorder.stop();
114 try {
115 camera.reconnect();
116 } catch (IOException e) {
117 // TODO Auto-generated catch block
118 e.printStackTrace();
119 }
120 recordingStatus = false;
121 }
122}