Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Reading the files in so that MediaPlayer can find the duration is clearly taking some time. Since you are running this on the UI thread, that's going to slow down the entire application. </p> <p>I don't have any suggestions for how to speed up the process, but you can make your application behave much more smoothly if you do this work in a background thread with AsyncTask. That might look something like this:</p> <pre><code>private class GetAudioFilesLength extends AsyncTask&lt;Void, Void, Void&gt; { List&lt;File&gt; mFiles = new ArrayList&lt;File&gt;(); public GetAudioFilesLength(List&lt;File&gt; theFiles){ mFiles = theFiles; } @Override protected String doInBackground(String... params) { for(File file : mFiles){ int length = calculateLength(file); // Do something with the length // You might want to update the UI with the length of this file // with onProgressUpdate so that you display the length of the files // in real time as you process them. } } @Override protected void onPostExecute(String result) { // Update the UI in any way you want. You might want // to store the file lengths somewhere and then update the UI // with them here } @Override protected void onPreExecute() { } @Override protected void onProgressUpdate(Void... values) { } } </code></pre> <p>When you want to start the processing, just call <code>new GetAudioFilesLength(files).execute()</code></p> <p>Edit to answer additional questions:</p> <ol> <li><p>It looks as efficient as your original code. The difference now is that the user will still be able to interact with your application because the work will be done in the background thread. It is possible that there is a more efficient way to read in the length of an audio file, but I don't know what that is. If you knew the sample rate and encoding, I can imagine you could write code that would calculate the length of the audio without loading it into MediaPlayer, which takes longer. Again, though, someone else would have to help with that.</p></li> <li><p>I'm not sure I understand what the problem is, but I think you are asking how to use onProgressUpdate to update the UI and add the lengths to a ListView?</p></li> </ol> <p>You could change the middle argument to the AsyncTask generation to be a String (or something else) <code>AsyncTask&lt;Void, String, Void&gt;, that tells onProgressUpdate what you will be passing to it. You can then call</code>publishProgress` from doInBackground to update the UI accordingly.</p> <pre><code>@Override protected String doInBackground(Void... params) { for(File file : mFiles){ int length = calculateLength(file); // Do something with the length // You might want to update the UI with the length of this file // with onProgressUpdate so that you display the length of the files // in real time as you process them. publishProgress("The length of " + file.getName() + " is: " + length); } } @Override protected void onPostExecute(Void result) { // Update the UI in any way you want. You might want // to store the file lengths somewhere and then update the UI // with them here } @Override protected void onProgressUpdate(String... values) { // You'll have to implement whatever you'd like to do with this string on the UI doSomethingWithListView(values[0]); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload