2013-08-24 63 views
0

这里我有代码,它将记录音频流到文件。问题是我想用正确的文件扩展名保存这个文件(主要是.mp3和.aac)。我怎样才能做到这一点?如何识别音频流格式?

URLConnection conn = new URL(StringUrls[0]).openConnection(); 
      InputStream in = conn.getInputStream(); 

      BufferedOutputStream bufOutstream = new BufferedOutputStream(new FileOutputStream(new File(env.getExternalStorageDirectory()+"/temp.file"))); 

      byte[] buffer = new byte[4096]; 
      int len = in.read(buffer); 
      while (len != -1) { 
       bufOutstream.write(buffer, 0, len); 
       len = in.read(buffer); 

       if (Recorder.this.isCancelled) break; 

      } 
      bufOutstream.close(); 
+0

你需要分析流内容 - 看看http://stackoverflow.com/questions/11360286/detect-if-file-is-mp3 – trebron

回答

2

其中一种方法是查看二进制数据,而您已经掌握了这些数据。根据这一File signatures table MP3和AAC具有独特的魔法标题:

  • 49 44 33MPEG-1音频层3(MP3)音频文件
  • FF F1MPEG-4高级音频编码(AAC)低复杂性(LC)音频
  • FF F9MPEG-2高级音频编码(AAC),低复杂度(LC)音频