Android - getLength()在FileDescriptor上?

时间:2015-07-09 10:19:32

标签: android mp3 sd-card file-descriptor

我已经搜索了很多,但是我无法弄清楚是否有办法获得从sdcard加载的mp3文件的长度偏移

public void onClick(View v) {
String audioPath = "/sdcard/MP3/" + textView.getText();
File baseDir = Environment.getExternalStorageDirectory();
FileInputStream fis = null;
     try {
         fis = new FileInputStream(audioPath);
         fd = fis.getFD();
         String samplerateString = null, buffersizeString = null;
         if (Build.VERSION.SDK_INT >= 17) {
                AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                samplerateString = audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE);
                buffersizeString = audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER);
          }
          if (samplerateString == null) samplerateString = "44100";
          if (buffersizeString == null) buffersizeString = "512";
          long[] params = {
                fd.getStartOffset(),                                
                fd.getLength(),
                Integer.parseInt(samplerateString),
                Integer.parseInt(buffersizeString)
          };
       } catch (FileNotFoundException e) {
          e.printStackTrace();
       } catch (IOException e) {
          e.printStackTrace();
       }
}

fd.getStartOffset()和fd.getLength()仅在assetFIleDescriptor上可用..但是据我所知,assetFiledescriptor无法从SDCard加载mp3文件,对吧? / p>

1 个答案:

答案 0 :(得分:0)

File audioFile = new File(audioPath);
int length = (int) audioFile.length();