简体   繁体   English

For循环等待调用方法的完成

[英]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. 如果您看到有反对转换的成功消息,并且我从未在for循环中遇到过,而如果我仅传递了一个文件,那么我会获得成功消息。 pls advice. 请咨询。

You could add a class instance variable for an index and increment it as necessary, calling the convert() method recursively as necessary. 您可以为索引添加类实例变量,并根据需要对其进行递增,并根据需要以递归方式调用convert()方法。 It'd look something like this (Java is a little rusty, you may have to clean up syntax): 看起来像这样(Java有点生锈,您可能需要清理语法):

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();
        }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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