android:在模拟器上使用 MediaPlayer 录制音频

发布于 2024-12-24 02:38:22 字数 835 浏览 4 评论 0原文

我正在尝试使用以下代码从 Android 模拟器上的麦克风录制音频:

recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(Environment.getExternalStorageDirectory() + "/test/test.3gp");
try {
    recorder.prepare();
}
catch (IOException io) {
    Log.v(LOG_TAG, "Could not prepare the audio " + io.getMessage());
}
recorder.start();

为了停止音频,这是代码:

recorder.stop();
recorder.reset();
recorder.release();

录制过程工作正常,但生成的音频失真。当我录制 60 秒的音频并播放它时,它的持续时间显示为 120 秒。测量并不准确,但这只是为了给您一个想法。

只有 AMR_NB 编码器在我的模拟器上运行。我尝试过不同的输出格式,但结果总是相同的。

这是模拟器的限制还是我在这里做错了什么?

编辑1: 我也尝试过 AudioRecord 类,结果是相同的拖动音频。

谢谢。

I am trying to record audio from the microphone on the Android emulator with this code:

recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(Environment.getExternalStorageDirectory() + "/test/test.3gp");
try {
    recorder.prepare();
}
catch (IOException io) {
    Log.v(LOG_TAG, "Could not prepare the audio " + io.getMessage());
}
recorder.start();

For stopping the audio, this is the code:

recorder.stop();
recorder.reset();
recorder.release();

The recording process works fine but the resulting audio that is distorted. When I record an audio for 60 seconds duration and play it, it's duration is being shown as 120 seconds. The measurement is not exact but the this is just to give you an idea.

Only the AMR_NB encoder is working on my emulator. I have tried different output formats but the result is always the same.

Is it a limitation of the emulator or am I doing something wrong here?

Edit 1:
I have tried the AudioRecord class too and the result is the same dragging audio.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

用心笑 2024-12-31 02:38:22

我一直在为同样的事情工作并找到了解决方案,尝试使用以下代码:

    private void startRecording()
    {

        this.recorder = new MediaRecorder();
        this.recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        this.recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        this.recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        MediaRecorder.getAudioSourceMax();
        this.recorder.setOutputFile(this.getFilename());
        this.recorder.setOnErrorListener(this.errorListener);
        this.recorder.setOnInfoListener(this.infoListener);

        try
        {
            this.recorder.prepare();
            this.recorder.start();
        } catch (final IllegalStateException e)
        {
            e.printStackTrace();
        } catch (final IOException e)
        {
            e.printStackTrace();
        }
    }

这工作正常。希望对你有帮助:)

I have been working for the same and found the solution, Try using the following code:

    private void startRecording()
    {

        this.recorder = new MediaRecorder();
        this.recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        this.recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        this.recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        MediaRecorder.getAudioSourceMax();
        this.recorder.setOutputFile(this.getFilename());
        this.recorder.setOnErrorListener(this.errorListener);
        this.recorder.setOnInfoListener(this.infoListener);

        try
        {
            this.recorder.prepare();
            this.recorder.start();
        } catch (final IllegalStateException e)
        {
            e.printStackTrace();
        } catch (final IOException e)
        {
            e.printStackTrace();
        }
    }

This is working perfactly. Hope it helps you :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文