本帖最后由 HawkOfWinter 于 2014-07-29 18:34:47 编辑

解决方案 »

  1.   

    前三个我不太清楚, 多媒体我做过一点。
    setDataSource 这个函数只是告诉mediaplayer要播放的文件的url,你的mediaplayer这时候还没加载这个文件
    获取duration:
    try {
        mp.setDataSource(file.getAbsolutePath());
        mp.prepare();
    } catch (Exception e)  {}
    player.setOnPreparedListener(new OnPreparedListener() {
                    public void onPrepared(MediaPlayer mp) {
                        int size = mp.getDuration();
                    }
    });
      

  2.   

    已经基本ok了大家建议要弄个OnPreparedListener监听一下,我就没这么做,原因目前已经能读出大小,且只是显示给用户看下大约多长。另外,问个小问题:java支持数组类型String[]的返回,这在c++中是不支持的吧?
    /*
         * 函数介绍:获取文件细节
         * 输入参数:file,文件
         * 输出参数:无
         * 返回值  :字符串数组
         */
        private String[] getPVRfiledetails(File file) {
         Log.i(TAG, "getPVRfiledetails is called");
         String[] strArray = new String[4]; 
        
         String strName = file.getName();
        
         strArray[0] = "DATE : ";
         strArray[1] = "TIME : ";
         strArray[2] = "SIZE : ";
            strArray[3] = "DURATION : ";
        
         //P2007-01-20-23-23-32-1537.pvr
         if (strName.length() >= 26) {
         strArray[0] += strName.substring(1, 11);
         strArray[1] += strName.substring(12, 20);
         }
         else {
         strArray[0] += "....-..-..";
             strArray[1] += "..-..-..";
         }
        
         long lSize = file.length();
         if (lSize < 1024) {
         strArray[2] += lSize + "bytes";
         }
         else if (lSize < 1024*1024) {
         strArray[2] += lSize/1024 + "Kbytes";
         }
         else if (lSize < 1024*1024*1024) {
         strArray[2] += lSize/(1024*1024) + "Mbytes";
         }
         else {
         strArray[2] += lSize/(1024*1024*1024) + "Gbytes";
         }
            
         String strPath = file.getAbsolutePath();
         MediaPlayer mp = new MediaPlayer();
         try {
         if (mp != null) {
    mp.setDataSource(strPath);
    mp.prepare();
    int second = mp.getDuration() / 1000;
    if (second < 60) {
    strArray[3] += second + "sec(s)";
    }
    else if (second < (60*60)) {
    strArray[3] += second/60 + "min(s)";
    }
    else {
    strArray[3] += second/(60*60) + "hr(s)";
    }
    mp.release();
    mp = null;
         }
    } catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SecurityException 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();
    }
        
         return strArray;
        }