简体   繁体   中英

For loop wait for completion of called method

I have a code which is checking a defined type of audio file in folder and calling converter to change its format. Now when first file is passed, converter is called and as file is in process of being conversion, for loop called converter again for second file. In this i felt earlier/later process is terminated and hence im getting only file converted as output. Code is here. How can i manage to get all files convereted.

public void convertAudio(View v) {


        final File pathanme = new File(Environment.getExternalStorageDirectory() + "/sdcard/test");
        File files[] = pathanme.listFiles();
        for (File f : files) {
            if (f.getName().endsWith(".mp4")) {
                String filename = f.getName().toLowerCase().toString();
                System.out.println(filename);

                File wavFile = new File(pathanme, filename);
                IConvertCallback callback = new IConvertCallback() {
                    @Override
                    public void onSuccess(File convertedFile) {
                        Toast.makeText(NewMainActivity.this, "SUCCESS: " + convertedFile.getPath(), Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onFailure(Exception error) {
                        Toast.makeText(NewMainActivity.this, "ERROR: " + error.getMessage(), Toast.LENGTH_LONG).show();
                    }
                };
                Toast.makeText(this, "Converting audio file..." + filename, Toast.LENGTH_SHORT).show();

                AndroidAudioConverter.with(this)
                        .setFile(wavFile)
                        .setFormat(AudioFormat.MP3)
                        .setCallback(callback)
                        .convert();
            }
        }

If u see there is success message against conversion and i never got this under for loop whereas if i pass only one file, i got success message. pls advice.

You could add a class instance variable for an index and increment it as necessary, calling the convert() method recursively as necessary. It'd look something like this (Java is a little rusty, you may have to clean up syntax):

public class MyClass {

   private int fileIndex = 0;
   private File[] files;

   public void convertAudio(View v) {
     final File pathanme = new File(Environment.getExternalStorageDirectory() + "/sdcard/test");
     this.files = pathanme.listFiles();
     fileIndex = 0;
     convertFile(files[fileIndex]);

  }

   private void convertFile(File f) {
      if (f.getName().endsWith(".mp4")) {
         String filename = f.getName().toLowerCase().toString();
         System.out.println(filename);

         File wavFile = new File(pathanme, filename);
         IConvertCallback callback = new IConvertCallback() {
            @Override
            public void onSuccess(File convertedFile) {
               Toast.makeText(NewMainActivity.this, "SUCCESS: " + convertedFile.getPath(), Toast.LENGTH_LONG).show();
               fileIndex++;
               if (this.files.size > fileIndex) {
                  convertFile(this.files[fileIndex];
               } else {
                  // we're done converting
               }
            }

            @Override
            public void onFailure(Exception error) {
               Toast.makeText(NewMainActivity.this, "ERROR: " + error.getMessage(), Toast.LENGTH_LONG).show();
               // cancel out or keep going, whatever
            }
         };

            Toast.makeText(this, "Converting audio file..." + filename, Toast.LENGTH_SHORT).show();

            AndroidAudioConverter.with(this)
                    .setFile(wavFile)
                    .setFormat(AudioFormat.MP3)
                    .setCallback(callback)
                    .convert();
        }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM