【问题标题】:Android MediaRecorder getMaxAmplitude always returns 0 on LG OptimusAndroid MediaRecorder getMaxAmplitude 在 LG Optimus 上总是返回 0
【发布时间】:2011-09-29 19:57:44
【问题描述】:

我正在编写一个应用程序,它基本上只是测试我们是否可以从麦克风中得到任何东西。

它可以在多个安卓设备上完美运行,但在 LG Optimus 上却不行。每次我在 LG 上调用 MediaRecorder.getMaxAmplitude 时,它都会返回 0。

设备正在成功录音,因为我可以收听录音。

【问题讨论】:

    标签: android


    【解决方案1】:

    getMaxAmplitude 返回自上次调用以来的最大幅度。

    所以,第一次调用它时,它会初始化自己(因此返回 0),第二次它应该返回另一个值。根据文档:

    getMaxAmplitude() 返回自上次调用此方法以来采样的最大绝对幅度。仅在 setAudioSource() 之后调用。

    返回自上次调用以来测量的最大绝对幅度,或第一次调用时为 0

    或者,如果你使用得当,你会遇到和我一样的问题。我的代码适用于galaxyTab 7(Froyo),但不适用于10.1(Honeycomb)。

    编辑:我修复了我的问题(我希望它也能帮助你)。确保第一次调用 getMaxAmplitude 时,为了初始化它,首先调用了 start()。 我用过:

    recorder.prepare();
    recorder.getMaxAmplitude();
    recorder.start();
    //listening to the user
    int amplitude = recorder.getMaxAmplitude();
    

    应该是什么时候:

    recorder.prepare();
    recorder.start();
    recorder.getMaxAmplitude();
    //listening to the user
    int amplitude = recorder.getMaxAmplitude();
    

    EDIT :这段代码似乎仍有缺陷。例如,此代码似乎不适用于 S2。它会返回 0。但我只调用了两次 getMaxAmplitude(),所以如果你需要每秒更新一次幅度,它可能没问题。

    【讨论】:

      【解决方案2】:

      一种解决方案是在线程内部读取振幅。你会不时看到你会有一个不同于 0.0f 的值。

      private Runnable mPollTask = new Runnable() {
          public void run() {
                  while(true){
                 double amp = mSensor.getAmplitude();
                 System.out.print("Amplitude: ");
                 System.out.println(amp);
                  }
          }
      };
      

      【讨论】:

        【解决方案3】:

        以下代码对我有用,需要设置 setAudioSamplingRatesetAudioEncodingBitRate

        MediaRecorder recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        if (Build.VERSION.SDK_INT >= 10) {
        recorder.setAudioSamplingRate(44100);
        recorder.setAudioEncodingBitRate(96000);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        } else {
        // older version of Android, use crappy sounding voice codec
        recorder.setAudioSamplingRate(8000);
        recorder.setAudioEncodingBitRate(12200);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        }
        recorder.setOutputFile(file.getAbsolutePath());
        try {
        recorder.prepare();
        } catch (IOException e) {
        throw new RuntimeException(e);
        }
        

        【讨论】:

          猜你喜欢
          • 2015-09-27
          • 1970-01-01
          • 2023-02-16
          • 1970-01-01
          • 2011-06-16
          • 2015-10-01
          • 1970-01-01
          • 2014-05-30
          • 1970-01-01
          相关资源
          最近更新 更多