0

我正在尝试计算媒体文件的持续时间并在检索时更新列表。我asyncTask用于计算持续时间。但是应用程序在计算大小之前一直挂起。为什么会这样?有更好的方法吗?

我的异步任务是:- ewLongOperation().execute(mFileMang.getCurrentDir()+"/" + mDataSource.get(position));

 private class LongOperation extends AsyncTask<String, Void, String> {

     @Override
     protected String doInBackground(String... params) {

            MediaPlayer mp = MediaPlayer.create(mContext,  Uri.parse(params[0]));
            long duration = mp.getDuration();
            mp.reset();
            mp.release();

               String finalTimerString = "";
               String secondsString = "";
               String minutesString = "";
               // Convert total duration into time
               int hours = (int)( duration / (1000*60*60));
               int minutes = (int)(duration % (1000*60*60)) / (1000*60);
               int seconds = (int) ((duration % (1000*60*60)) % (1000*60) / 1000);
               // Add hours if there
               if(hours > 0){
               finalTimerString = hours + ":";
               }

               // Prepending 0 to seconds if it is one digit
               if(seconds < 10){
               secondsString = "0" + seconds;
               }else{
               secondsString = "" + seconds;}

               if(minutes < 10){
                   minutesString = "0" + minutes;
               }else{
                   minutesString = "" + minutes;}


               duration_video = finalTimerString = finalTimerString + minutesString + ":" + secondsString;

               return null;
     }      

     @Override
     protected void onPostExecute(String result) {

           //might want to change "executed" for the returned string passed into onPostExecute() but that is upto you
     }

     @Override
     protected void onPreExecute() {
     }

     @Override
     protected void onProgressUpdate(Void... values) {
     }

}

4

1 回答 1

-2

AsyncTask 不应该用于长时间的操作(不超过 2 到 3 秒)。我不知道是否是这种情况,但是由于您将任务称为 LongOperation,因此我认为它运行的时间更长。

您可以在此处找到有关它的更多信息

于 2013-10-10T13:24:38.913 回答