title: Download array of audios from Firebase Storage
body: <p>My app must download every sound that would be uploaded on Firebase Storage. To do so, I followed the next way:</p>
  
  <p>1) all sounds in Firebase Storage should be named from 1 to n (1.mp3, 2.mp3, 3.mp3...n.mp3).
  <a href="https://i.stack.imgur.com/zQU4N.png" rel="nofollow noreferrer">enter image description here</a></p>
  
  <p>2) After that, it creates ArrayList of tempfiles. The sounds from Firebase Storage will be uploaded right into them. </p>
  
  <pre><code>private int howmuch = 4;
  
  ArrayList&lt;File&gt; music = new ArrayList&lt;File&gt;();
  ArrayList&lt;StorageReference&gt; url = new ArrayList&lt;StorageReference&gt;();
      try {
          for(int i=1; i &lt;= howmuch; i++){
          music.add(File.createTempFile("qqqq"+i, "mp3"));}
      } catch (IOException e) {
          e.printStackTrace();
      }
      for(int j=1; j &lt;= howmuch; j++){
          url.add(FirebaseStorage.getInstance().getReferenceFromUrl("gs://engapp-c0f2e.appspot.com/beginner/"+j+".mp3"));
      }
      for (int i = 0; i &lt; howmuch; i++){
      url.get(i).getFile(music.get(i)).addOnSuccessListener(new OnSuccessListener&lt;FileDownloadTask.TaskSnapshot&gt;() {
          @Override
          public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
              // Successfully downloaded data to local file
              // ...
              Log.d(TAG, "Successfully");
          }
      }).addOnFailureListener(new OnFailureListener() {
          @Override
          public void onFailure(@NonNull Exception exception) {
              // Handle failed download
              // ...
              Log.w(TAG, "Failure");
          }
      });
  </code></pre>
  
  <p>Variable <strong>howmuch</strong> returns a positive integer, this is a number of sounds that need to be exactly downloaded and a number of temp files need to be created. Clearly, their numbers have to be equaled. It could be as 4, as 10, as 100.</p>
  
  <p>ArrayList <strong>music</strong> contains temp files, where it puts our sounds. IDE made me wrap it to try/catch.</p>
  
  <p>ArrayList <strong>url</strong> contains LINK to every file it needs to download. As you can see, I use <em>for</em> loop for that (it can seem weird, but I don't really know better solution).</p>
  
  <p>So it works well despite the algorithm seems weak. It downloads all sounds and I can furter play it with MediaPlayer. That's how I do it:</p>
  
  <pre><code>        try {
              mp.setDataSource(File.separator + DBcontent.getMusic(counter));
              mp.prepare();
              mp.start();
              counter ++;
          } catch (IOException e) {
              e.printStackTrace();
          }
  </code></pre>
  
  <p><strong>DBcontent</strong> is an object of class where we've put our sounds to temp files from Firebase Storage. Method <strong>getMusic()</strong> is a typical getter</p>
  
  <p><strong>counter</strong> is a typical counter. From the begining <strong>counter</strong>=0. So we will be retrieving EVERY sound from our ArrayList and make it play.</p>
  
  <pre><code>public File getMusic(int a){
      File c = music.get(a);
      return c;
  }
  </code></pre>
  
  <p>Now the main problem. If Firebase Storage contains less sounds than we need to download (for example <strong>howmuch</strong> = 10, but we have only 5 sounds in our storage) it's going to work anyway, but temp files would be empty so it can not to be played. it means, if <strong>howmuch</strong> was 10, and <strong>counter</strong> is 6, so the app would work not properly. </p>
  
  <p>What exactly I need. Either the algorithm to check ArrayList of temp files for EMPTY/null and remove them afterwards or any other solution to download sounds and put them into ArrayList (or even sipmlest Array[]).</p>
  
  <p>Thank you.</p>