Note that there are some explanatory texts on larger screens.

plurals
  1. POApp slows down with calculation of length in seconds of WAV file
    primarykey
    data
    text
    <p>In a ListView of sound files in a folder I want to show the length in seconds of the files. The steps that I take:</p> <ol> <li>First I create an ArrayList for the instances of the soundFiles. </li> <li>Then in a for loop I add the data to the instance by soundFile.setLength(calculateLength(file[i])).</li> <li>After this I initiate my CustomArrayAdapter and I apply it to my listView.</li> <li>In my CustomArrayAdapter I apply it: tvFileLength.setText(soundFile.getLength()); (whith a holder though..)</li> </ol> <p>But since I am doing this, my app is slower than a turtle! (having 400 files) Is there any way I can fix this speed?</p> <pre><code>private int calculateLength(File yourFile) throws IllegalArgumentException, IllegalStateException, IOException { MediaPlayer mp = new MediaPlayer(); FileInputStream fs; FileDescriptor fd; fs = new FileInputStream(yourFile); fd = fs.getFD(); mp.setDataSource(fd); mp.prepare(); int length = mp.getDuration(); length = length / 1000; mp.release(); return length; } </code></pre> <hr> <pre><code> **EDIT** </code></pre> <p>New code I am having:</p> <p>Activity</p> <pre><code>myList = new ArrayList&lt;RecordedFile&gt;(); File directory = Environment.getExternalStorageDirectory(); file = new File(directory + "/test/"); File list[] = file.listFiles(); for (int i = 0; i &lt; list.length; i++) { if (checkExtension(list[i].getName()) == true) { RecordedFile q = new RecordedFile(); q.setTitle(list[i].getName()); q.setFileSize(readableFileSize(list[i].length())); //above is the size in kB, is something else but I //also might move this to the AsyncTask! myList.add(q); } } new GetAudioFilesLength(myList).execute(); </code></pre> <p>AsyncTask</p> <pre><code>List&lt;RecordedFile&gt; mFiles = new ArrayList&lt;RecordedFile&gt;(); public GetAudioFilesLength(List&lt;RecordedFile&gt; theFiles) { mFiles = theFiles; } @Override protected String doInBackground(Void... params) { File directory = Environment.getExternalStorageDirectory(); // File file = new File(directory + "/test/"); String mid = "/test/"; for (RecordedFile fileIn : mFiles) { File file = new File(directory + mid + fileIn.getTitle()); try { int length = readableFileLengthSeconds(file); fileIn.setFileLengthSeconds(length); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 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. } return mid; } @Override protected void onProgressUpdate(Void... values) { } @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() { } */ public int readableFileLengthSeconds(File yourFile) throws IllegalArgumentException, IllegalStateException, IOException { MediaPlayer mp = new MediaPlayer(); FileInputStream fs; FileDescriptor fd; fs = new FileInputStream(yourFile); fd = fs.getFD(); mp.setDataSource(fd); mp.prepare(); // might be optional int length = mp.getDuration(); length = length / 1000; mp.release(); return length; } </code></pre> <p>Awesome, it works partly, but! I got 2 remaining questions:</p> <ol> <li>Does this looks ok and efficient? </li> <li>It works for lets say the first 100 elements in my listview, after that it displays 0 s, it has something to do with onProgressUpdate I assume, but I am not sure how I can make this work.</li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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