Android Record Audio并获取其持续时间会以WAV格式引发异常

时间:2019-04-01 15:24:59

标签: android android-audiorecord

我的情况就像每当用户打开一个活动时,只要活动消失,我就需要录制音频。我将录制文件上传到服务器上。

下面是我在android端用来记录文件的代码

    private MediaRecorder myAudioRecorder;
    myAudioRecorder = new MediaRecorder();
    myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    myAudioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    myAudioRecorder.setAudioSamplingRate(16000);
    myAudioRecorder.setAudioChannels(1);
    myAudioRecorder.setAudioEncodingBitRate(16000);
    myAudioRecorder.setOutputFile("Path to the storage with filename and extension as wav");

下面是我用来从音频中提取持续时间的服务器端代码:

public class MyUtils {
 public int getAudioFileSize(String pathToFile) {

  try {
   File file = new File(pathToFile);


   AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);


   AudioFormat format = audioInputStream.getFormat();
   long frames = audioInputStream.getFrameLength();
   double durationInSeconds = (frames + 0.0) / format.getFrameRate();
   return (int) durationInSeconds;
  } catch (UnsupportedAudioFileException | IOException e) {
   e.printStackTrace();
  }
  return 0;
 }

 public static void main(String[] args) throws UnsupportedAudioFileException, IOException {
  System.out.println(new MyUtils().getAudioFileSize("/tmp/mysample.wav"));
 }



}

以下是我得到的例外情况:

javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java: 1189)
at ai.talentify.utils.ui.AudioUtils.getAudioFileSize(AudioUtils.java: 28)
at ai.talentify.utils.ui.AudioUtils.main(AudioUtils.java: 42)

但是如果i convert same file using this url to wav file it worked

所以我不知道如何以WAV格式正确录制音频。请建议我该怎么办

0 个答案:

没有答案