title: Play video on surfaceview while another Thread do some calculations on different Surface - Android
body: <p>In my app i want to do some calculations on video file in separate thread(not on main)  and in the middle of the test i want to preview another video.</p>
  
  <p>This is a part of my code : </p>
  
  <pre><code>public class SampleTestingAlgorithmAct extends Activity {
  
  Intent intentTestResults;
  
  private static final String TAG = "Decoder";
  private DecoderThread mThread = null;
  Date startTime, endTime;
  GlobalVarClass GVC;
  
  int Frame_Size = 100, Frame_Size_ROI = 5;//600,30
  String SCR = "", Velocity = "", VSCR = "", ROI = "", MSC = "", ROI_X = "", ROI_Y = ""; 
  
  boolean Finished = false, StopCamera = false;
  String VIDEO_PATH = "", VIDEO_PATH_TMP = "";
  
  VideoView videoview;
  MediaController mediaController;
  
  ProgressBar PB;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
      WindowManager.LayoutParams.FLAG_FULLSCREEN);
      getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  
      setContentView(R.layout.activity_sample_testing_algorithm);
  
      overridePendingTransition(0, 0);
  
      intentTestResults = new Intent(this, TestResultsAct.class);
  
       GVC = ((GlobalVarClass) getApplicationContext());
  
       VIDEO_PATH = Environment.getExternalStorageDirectory() + "/DCIM/Frames/" + GVC.getFileForPreview();
       VIDEO_PATH_TMP = Environment.getExternalStorageDirectory() + "/DCIM/Frames/" + GVC.getTempFileForPreview();
  
      videoview = (VideoView)findViewById(R.id.Videoview);
      mediaController = new MediaController(this);
  
      videoview.setMediaController(mediaController);
  
      PB = (ProgressBar)findViewById(R.id.ProgressBarTest);
      PB.bringToFront();
      PB.setFocusable(true);
  
      StartTest();
  }
  
  private void StartTest() {
  
      startTime = new Date();
      mThread = null;
      mThread = new DecoderThread( null, VIDEO_PATH, Frame_Size, Frame_Size_ROI);
      mThread.dumpStack();
      mThread.start();
  
      final Timer myTimer1 = new Timer();
  
      myTimer1.schedule(new TimerTask() {
          @Override
          public void run() {
  
              runOnUiThread(new Runnable() {
  
                      @Override
                      public void run() {
                          if(!Finished)
                          {
                              if(mThread.outputSurface != null)
                              {
                                  ROI = "( " + String.format("%.0f",mThread.outputSurface.OUTcropCenter_C) + "," + String.format("%.0f",mThread.outputSurface.OUTcropCenter_R) + " )";
                                  ROI_X = String.format("%.0f",mThread.outputSurface.OUTcropCenter_C);
                                  ROI_Y = String.format("%.0f",mThread.outputSurface.OUTcropCenter_R);
                                  SCR = String.format("%.3f",mThread.outputSurface.SCR);
                                  MSC = String.format("%.3f",mThread.outputSurface.MSC);
                                  Velocity = String.format("%.3f",mThread.outputSurface.Velocity);
                                  VSCR = String.format("%.3f",mThread.outputSurface.VSCR);
                                  if( !VSCR.equals("0.000")  &amp; !SCR.equals("0.000") &amp; !MSC.equals("0.000") &amp; !Velocity.equals("0.000") &amp;! VSCR.equals("") &amp; !SCR.equals("") &amp; !MSC.equals("") &amp; !Velocity.equals(""))
                                  {
                                      WriteToXMLFile();
                                      SaveKeyParameters();
                                      endTime = new Date();
                                      Finished = true;
                                  }
                                  if( !ROI.equals("( 0,0 )")  &amp; !ROI.equals("") &amp; !StopCamera)
                                  {
                                      StopCamera = true;
  
                                      Uri videoURI = Uri.parse(VIDEO_PATH_TMP);
                                      videoview.setVideoURI(videoURI);
                                      videoview.requestFocus();
                                      videoview.start();
                                  }
                                      PB.setMax(Frame_Size + Frame_Size_ROI + 2);
  
                                  if(!Finished)
                                      PB.setProgress(mThread.counter);
                              }
                          }
                      }
              });
          if(Finished)
              {
                  PB.setProgress(0);
                  System.gc();
  
                  myTimer1.cancel();
                  myTimer1.purge();
  
  
  
              }
          }
      }, 1000,10); 
  
  }
  </code></pre>
  
  <p>mThread  is  a class the extended Thread. and after first part of calculation I want to play the Video.</p>
  
  <p>(<strong>private class DecoderThread extends Thread {</strong>)
  But I've got this message on the device : </p>
  
  <p><strong>"Can't play this video"</strong></p>
  
  <p>If I don't start the Thread (mThread.Start()) the video is playing normally.</p>
  
  <p>How can I fix it ?</p>
  
  <p>Thanks!</p>
  
  <p><strong>Edit 1:</strong>
  This is the logcat:</p>
  
  <pre><code>03-12 10:08:23.800: E/OMX(1938):  setParam  sum(4147200) &gt; max resolution(3010560)
  03-12 10:08:23.800: E/ACodec(24626):  configureCodec multi window instance fail  appPid : 24626
  03-12 10:08:23.800: E/Bundle(2346): readBundle: bad magic number
  03-12 10:08:23.800: E/Bundle(2346): readBundle: trace = java.lang.RuntimeException
  03-12 10:08:23.800: E/Bundle(2346):     at android.os.Bundle.readFromParcelInner(Bundle.java:1677)
  03-12 10:08:23.800: E/Bundle(2346):     at android.os.Bundle.&lt;init&gt;(Bundle.java:83)
  03-12 10:08:23.800: E/Bundle(2346):     at android.os.Parcel.readBundle(Parcel.java:1579)
  03-12 10:08:23.800: E/Bundle(2346):     at android.os.Parcel.readBundle(Parcel.java:1564)
  03-12 10:08:23.800: E/Bundle(2346):     at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:371)
  03-12 10:08:23.800: E/Bundle(2346):     at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2125)
  03-12 10:08:23.800: E/Bundle(2346):     at android.os.Binder.execTransact(Binder.java:388)
  03-12 10:08:23.800: E/Bundle(2346):     at dalvik.system.NativeStart.run(Native Method)
  03-12 10:08:23.800: E/ACodec(24626): [OMX.SEC.avc.dec] configureCodec returning error -38
  03-12 10:08:23.800: E/MediaCodec(24626): Codec reported an error. (omx error 0x80001001, internalError -38)
  03-12 10:08:29.700: E/Watchdog(2346): !@Sync 221
  03-12 10:08:32.225: E/EnterpriseContainerManager(2555): ContainerPolicy Service is not yet ready!!!
  03-12 10:08:32.235: E/EnterpriseContainerManager(2555): ContainerPolicy Service is not yet ready!!!
  03-12 10:08:32.245: E/EnterpriseContainerManager(2555): ContainerPolicy Service is not yet ready!!!
  03-12 10:08:32.710: E/EnterpriseContainerManager(2555): ContainerPolicy Service is not yet ready!!!
  03-12 10:08:33.155: E/EnterpriseContainerManager(2346): ContainerPolicy Service is not yet ready!!!
  03-12 10:08:33.750: E/OMXNodeInstance(1938): !!! Observer died. Quickly, do something, ... anything...
  03-12 10:08:33.750: E/InputDispatcher(2346): channel ~ Channel is unrecoverably broken and will be disposed!
  03-12 10:08:33.750: E/InputDispatcher(2346): channel ~ Channel is unrecoverably broken and will be disposed!
  03-12 10:08:33.750: E/InputDispatcher(2346): channel ~ Channel is unrecoverably broken and will be disposed!
  03-12 10:08:33.775: E/WFDService(2346): FHD : android.intent.action.VIDEO_PLAYBACK_STOP
  </code></pre>