0

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 i m 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.

4

1 回答 1

0

您可以为索引添加一个类实例变量并根据需要递增它,并根据需要convert()递归调用该方法。它看起来像这样(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();
        }
   }
}
于 2016-11-09T18:55:06.123 回答