title: Using a service for video recording results in IOException "Invalid Preview Surface"
body: <p>I've seen similar problems but my code is structured quite different compared to those threads I've read about this issue.</p>
  
  <p>My problem is that when I try to set the previewDisplay of the MediaRecorder</p>
  
  <pre><code>mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
  </code></pre>
  
  <p>I get the IOException "invalid preview surface".<br>
  A <code>Log.v(TAG, mSurfaceHolder.getSurface().toString()</code> gives "Surface(name=null)/@0x42333f90" so I think the surface is null somehow? </p>
  
  <p>I'm trying to make this camera recording as a service which makes me uncertain about what my surface and surfaceholder should be.</p>
  
  <p>Currently, in onCreate() of RecorderService.class is:</p>
  
  <pre><code>SurfaceView view = new SurfaceView(getActivity());
  mSurfaceHolder = view.getHolder();
  </code></pre>
  
  <p>where <code>getActivity()</code> returns the activity where the service is started from. </p>
  
  <p>The camera is started from:</p>
  
  <pre><code>Log.v(TAG, "Starting camera");
  Intent intent = new Intent(getActivity(), CameraRecorder.class);
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  getActivity().startActivity(intent);
  </code></pre>
  
  <p>So when I try to stop the recording, the app crashes on <code>mMediaRecorder.stop()</code> due to a IllegalStateException.</p>
  
  <p>To sum it up:</p>
  
  <ol>
  <li>I want a RecorderService.class which can be started from any activity and it will record the camera until stopService()</li>
  <li>I'm not sure what to use for SurfaceView. I'm currently using the activity from where the recording is started but this results in a null SurfaceView (I think).</li>
  </ol>
  
  <p>I can provide more code if wanted.</p>
  
  <p>Thanks in advance!</p>