2017-08-22 24 views
1
ArrayList<String> list1 = splitFileList; 

       for (int i = 0; i < list1.size(); i++) { 

        tempFileName = splitFileList.get(i); 
        String splitFileCheckinDirectory = splitVideofilepath + Constant.SPLIT_VIDEO + "/" + list1.get(i) + Constant.FILE_EXTENSION; 

        File myfile = new File(splitFileCheckinDirectory); 
        if (!myfile.exists()) { 
         new TrimmVideo(getExternalFilesDir(null) + "/" + getFileNameFromFilePath(mFilePath), mStartTImelist.get(i), mEndTimelist.get(i) - mStartTImelist.get(i)).execute(); 

        } 
        } 

にforloop内の文字列のリストについては、Asynktaskを呼び出す方法を私はforループの内部で実行しようとしている私のAsynktaskあるは以下のアンドロイド

private class TrimmVideo extends AsyncTask<Void, Void, Void> { 
     private final String mediaPath; 
     private final double endTime; 
     private final int length; 
     private double startTime; 
     private ProgressDialog progressDialog; 

     private TrimmVideo(String mediaPath, int startTime, int length) { 
      this.mediaPath = mediaPath; 
      this.startTime = startTime; 
      this.length = length; 
      this.endTime = this.startTime + this.length; 
     } 

     @Override 
     protected void onPreExecute() { 
      progressDialog = ProgressDialog.show(VideoPlayActvity.this, 
        "Trimming videos", "Please wait...", true); 
      super.onPreExecute(); 
     } 

     @Override 
     protected Void doInBackground(Void... params) { 
      trimVideo(); 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      progressDialog.dismiss(); 
      dbHandler.updateFlag(fileModel == null ? tempFileName : fileModel.getfilename()); 
      btn_save_video.setVisibility(View.INVISIBLE); 
      super.onPostExecute(result); 
     } 

     private void trimVideo() { 
      try { 
       File file = new File(mediaPath); 
       FileInputStream fis = new FileInputStream(file); 
       FileChannel in = fis.getChannel(); 
       Movie movie = MovieCreator.build(in); 

       List<Track> tracks = movie.getTracks(); 
       movie.setTracks(new LinkedList<Track>()); 

       boolean timeCorrected = false; 

       // Here we try to find a track that has sync samples. Since we can only start decoding 
       // at such a sample we SHOULD make sure that the start of the new fragment is exactly 
       // such a frame 
       for (Track track : tracks) { 
        if (track.getSyncSamples() != null && track.getSyncSamples().length > 0) { 
         if (timeCorrected) { 
          // This exception here could be a false positive in case we have multiple tracks 
          // with sync samples at exactly the same positions. E.g. a single movie containing 
          // multiple qualities of the same video (Microsoft Smooth Streaming file) 

          //throw new RuntimeException("The startTime has already been corrected by another track with SyncSample. Not Supported."); 
         } else { 
          startTime = correctTimeToNextSyncSample(track, startTime); 
          timeCorrected = true; 
         } 
        } 
       } 

       for (Track track : tracks) { 
        long currentSample = 0; 
        double currentTime = 0; 
        long startSample = -1; 
        long endSample = -1; 

        for (int i = 0; i < track.getDecodingTimeEntries().size(); i++) { 
         TimeToSampleBox.Entry entry = track.getDecodingTimeEntries().get(i); 
         for (int j = 0; j < entry.getCount(); j++) { 
          // entry.getDelta() is the amount of time the current sample covers. 

          if (currentTime <= startTime) { 
           // current sample is still before the new starttime 
           startSample = currentSample; 
          } else if (currentTime <= endTime) { 
           // current sample is after the new start time and still before the new endtime 
           endSample = currentSample; 
          } else { 
           // current sample is after the end of the cropped video 
           break; 
          } 
          currentTime += (double) entry.getDelta()/(double) track.getTrackMetaData().getTimescale(); 
          currentSample++; 
         } 
        } 
        movie.addTrack(new CroppedTrack(track, startSample, endSample)); 
       } 

       IsoFile out = new DefaultMp4Builder().build(movie); 
       File storagePath = new File(getExternalFilesDir(null) + "/" + Constant.SPLIT_VIDEO + "/"); 
       storagePath.mkdirs(); 
       File myMovie = new File(storagePath, fileModel == null ? "/" + tempFileName + Constant.FILE_EXTENSION : fileModel.getfilename() + Constant.FILE_EXTENSION); 
       FileOutputStream fos = new FileOutputStream(myMovie); 
       FileChannel fc = fos.getChannel(); 
       out.getBox(fc); 
       dbHandler.updateFlag(fileModel == null ? tempFileName : fileModel.getfilename()); 
       fc.close(); 
       fos.close(); 
       fis.close(); 
       in.close(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 

     private double correctTimeToNextSyncSample(Track track, double cutHere) { 
      double[] timeOfSyncSamples = new double[track.getSyncSamples().length]; 
      long currentSample = 0; 
      double currentTime = 0; 
      for (int i = 0; i < track.getDecodingTimeEntries().size(); i++) { 
       TimeToSampleBox.Entry entry = track.getDecodingTimeEntries().get(i); 
       for (int j = 0; j < entry.getCount(); j++) { 
        if (Arrays.binarySearch(track.getSyncSamples(), currentSample + 1) >= 0) { 
         // samples always start with 1 but we start with zero therefore +1 
         timeOfSyncSamples[Arrays.binarySearch(track.getSyncSamples(), currentSample + 1)] = currentTime; 
        } 
        currentTime += (double) entry.getDelta()/(double) track.getTrackMetaData().getTimescale(); 
        currentSample++; 
       } 
      } 
      for (double timeOfSyncSample : timeOfSyncSamples) { 
       if (timeOfSyncSample > cutHere) { 
        return timeOfSyncSample; 
       } 
      } 
      return timeOfSyncSamples[timeOfSyncSamples.length - 1]; 
     } 
    } 

splitFileListリスト2サイズデータaが含まれ、双方向希望は同期実行します1つずつ、つまり0からループを開始すると、0のasynkタスクが完了するはずです。ループが1つ進むと、それが完了するはずです。forループで1つずつasynkタスクを実行する方法を教えてください。

答えて

1

あなたはこのようにいくつかのことをスレッドを使用する必要がありAsyncTaskで同期的に実行することはできません:あなたは@Hazemあなたは別のアプローチで行くことができ答える実装できない場合

Thread t = new Thread(
     new Runnable() { 
      public void run() { 
       try { 
ArrayList<String> list1 = splitFileList; 

      for (int i = 0; i < list1.size(); i++) { 

       tempFileName = splitFileList.get(i); 
       String splitFileCheckinDirectory = splitVideofilepath + Constant.SPLIT_VIDEO + "/" + list1.get(i) + Constant.FILE_EXTENSION; 

       File myfile = new File(splitFileCheckinDirectory); 
       if (!myfile.exists()) { 
        trimVideo(getExternalFilesDir(null) + "/" + getFileNameFromFilePath(mFilePath), mStartTImelist.get(i), mEndTimelist.get(i) - mStartTImelist.get(i)); //here you can run synchronously work 
        } 
       } 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
t.start(); 
try { 
    t.join(); 
..... 
} catch (Exception e) { 
    e.printStackTrace(); 
} 




private void trimVideo(String mediaPath, int startTime, int length) { 
     try { 
      File file = new File(mediaPath); 
      FileInputStream fis = new FileInputStream(file); 
      FileChannel in = fis.getChannel(); 
      Movie movie = MovieCreator.build(in); 

      List<Track> tracks = movie.getTracks(); 
      movie.setTracks(new LinkedList<Track>()); 

      boolean timeCorrected = false; 

      // Here we try to find a track that has sync samples. Since we can only start decoding 
      // at such a sample we SHOULD make sure that the start of the new fragment is exactly 
      // such a frame 
      for (Track track : tracks) { 
       if (track.getSyncSamples() != null && track.getSyncSamples().length > 0) { 
        if (timeCorrected) { 
         // This exception here could be a false positive in case we have multiple tracks 
         // with sync samples at exactly the same positions. E.g. a single movie containing 
         // multiple qualities of the same video (Microsoft Smooth Streaming file) 

         //throw new RuntimeException("The startTime has already been corrected by another track with SyncSample. Not Supported."); 
        } else { 
         startTime = correctTimeToNextSyncSample(track, startTime); 
         timeCorrected = true; 
        } 
       } 
      } 

      for (Track track : tracks) { 
       long currentSample = 0; 
       double currentTime = 0; 
       long startSample = -1; 
       long endSample = -1; 

       for (int i = 0; i < track.getDecodingTimeEntries().size(); i++) { 
        TimeToSampleBox.Entry entry = track.getDecodingTimeEntries().get(i); 
        for (int j = 0; j < entry.getCount(); j++) { 
         // entry.getDelta() is the amount of time the current sample covers. 

         if (currentTime <= startTime) { 
          // current sample is still before the new starttime 
          startSample = currentSample; 
         } else if (currentTime <= endTime) { 
          // current sample is after the new start time and still before the new endtime 
          endSample = currentSample; 
         } else { 
          // current sample is after the end of the cropped video 
          break; 
         } 
         currentTime += (double) entry.getDelta()/(double) track.getTrackMetaData().getTimescale(); 
         currentSample++; 
        } 
       } 
       movie.addTrack(new CroppedTrack(track, startSample, endSample)); 
      } 

      IsoFile out = new DefaultMp4Builder().build(movie); 
      File storagePath = new File(getExternalFilesDir(null) + "/" + Constant.SPLIT_VIDEO + "/"); 
      storagePath.mkdirs(); 
      File myMovie = new File(storagePath, fileModel == null ? "/" + tempFileName + Constant.FILE_EXTENSION : fileModel.getfilename() + Constant.FILE_EXTENSION); 
      FileOutputStream fos = new FileOutputStream(myMovie); 
      FileChannel fc = fos.getChannel(); 
      out.getBox(fc); 
      dbHandler.updateFlag(fileModel == null ? tempFileName : fileModel.getfilename()); 
      fc.close(); 
      fos.close(); 
      fis.close(); 
      in.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    private double correctTimeToNextSyncSample(Track track, double cutHere) { 
     double[] timeOfSyncSamples = new double[track.getSyncSamples().length]; 
     long currentSample = 0; 
     double currentTime = 0; 
     for (int i = 0; i < track.getDecodingTimeEntries().size(); i++) { 
      TimeToSampleBox.Entry entry = track.getDecodingTimeEntries().get(i); 
      for (int j = 0; j < entry.getCount(); j++) { 
       if (Arrays.binarySearch(track.getSyncSamples(), currentSample + 1) >= 0) { 
        // samples always start with 1 but we start with zero therefore +1 
        timeOfSyncSamples[Arrays.binarySearch(track.getSyncSamples(), currentSample + 1)] = currentTime; 
       } 
       currentTime += (double) entry.getDelta()/(double) track.getTrackMetaData().getTimescale(); 
       currentSample++; 
      } 
     } 
     for (double timeOfSyncSample : timeOfSyncSamples) { 
      if (timeOfSyncSample > cutHere) { 
       return timeOfSyncSample; 
      } 
     } 
     return timeOfSyncSamples[timeOfSyncSamples.length - 1]; 
    } 
+0

私たちはAsynk Taskを使用することはできません。これにはThreadを使用する必要があります。そこにもう1つのメソッドを作成する必要があります。Thread – Adevelopment

+0

はい、それを試すことができますか? – Hazem

+0

いいえ、私はこの中で私を助けてください。私はスレッド内で実行することができません@Hazem – Adevelopment

0

このためには、各データのカウンタを維持してforループを忘れてください。

まずあなたは、このようなあなたのlist.Somethingの最初の位置のためのasynctaskを呼び出す必要があります:

if(list1.size() > 0) { 
     fileCounter=0; 
     tempFileName = splitFileList.get(fileCounter); 
     String splitFileCheckinDirectory = splitVideofilepath + Constant.SPLIT_VIDEO + "/" + tempFileName + Constant.FILE_EXTENSION; 

     File myfile = new File(splitFileCheckinDirectory); 
     if (!myfile.exists()) { 
      new TrimmVideo(getExternalFilesDir(null) + "/" + getFileNameFromFilePath(mFilePath), mStartTImelist.get(i), mEndTimelist.get(i) - mStartTImelist.get(i)).execute(); 
     } 
} 

その後、あなたasyncTaskのonPostExecuteに、これはあなたを助ける

 @Override 
     protected void onPostExecute(Void result) { 
      progressDialog.dismiss(); 
      dbHandler.updateFlag(fileModel == null ? tempFileName : fileModel.getfilename()); 
      btn_save_video.setVisibility(View.INVISIBLE); 
      super.onPostExecute(result); 
      // Update your counter here 
      fileCounter++; 
      // Check if your incremented counter doesn't exceed your list size 
      if(fileCounter < list1.size()) { 
       // Then call your asynctask again with updated counter data 
       empFileName = splitFileList.get(fileCounter); 
       String splitFileCheckinDirectory = splitVideofilepath + Constant.SPLIT_VIDEO + "/" + tempFileName + Constant.FILE_EXTENSION; 

       File myfile = new File(splitFileCheckinDirectory); 
       if (!myfile.exists()) { 
         new TrimmVideo(getExternalFilesDir(null) + "/" + getFileNameFromFilePath(mFilePath), mStartTImelist.get(i), mEndTimelist.get(i) - mStartTImelist.get(i)).execute(); 
       } 
      } 
     } 

希望を。