title: Why does a newly created Android Camera2 MP4 File fail to play on some players, but not others
body: <p>I am running into an issue where while using an external library (<a href="https://github.com/natario1/CameraView" rel="nofollow noreferrer">link</a>) and writing a new MP4 file is causing it to be unplayable on multiple players but entirely playable on others. </p>
  
  <p>In theory, it should be immediately playable on all of them, but that is proving not the case. I am creating the video file like this:</p>
  
  <pre><code>        String timeStamp = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss",
                      Locale.getDefault()).format(new Date());
          String imageFileName = "MyApp_" + timeStamp + "";            
          File video;
          File storageDir = getExternalFilesDir(Environment.DIRECTORY_MOVIES);
          try {
              if(this.saveVideoToGallery){
                  storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
                  if(!storageDir.exists()){
                      boolean createdFolder = false;
                      try {
                          createdFolder = storageDir.mkdir();
                      } catch (Exception e){
                          e.printStackTrace();
                      }
                      if(!createdFolder){
                          storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
                      }
                  }
                  video = new File(storageDir, imageFileName + ".mp4");
              } else {
                  if(!storageDir.exists()) {
                      try {
                          storageDir.mkdir();
                      } catch (Exception e) {}
                  }
                  video = File.createTempFile(
                          imageFileName,  /* prefix */
                          ".mp4",         /* suffix */
                          storageDir      /* directory */
                  );
              }
          } catch (IOException ioe){
              ioe.printStackTrace();
              video = new File(storageDir + imageFileName + ".mp4");
          }
  </code></pre>
  
  <p>Note that the boolean</p>
  
  <pre><code>saveVideoToGallery
  </code></pre>
  
  <p>Is just a configurable global var that allows writing to an external directory. </p>