Android - 我可以使用 MediaRecord/MediaPlayer 而不是 AudioRecord/Track 来执行此操作吗?

发布于 2024-08-20 22:53:08 字数 2036 浏览 3 评论 0原文

我目前使用 AudioRecord 从 Android 设备的麦克风录制音频,然后将其直接传输到 AudioTrack,然后在 Android 设备的听筒上播放。

我想知道我可以做同样的事情,但使用 MediaPlayer 和 MediaRecorder 类,因为我需要使用媒体类而不是音频类上可用的 AMR-NB 编码。

我的 MediaRecorder 和 MediaPlayer 问题是,似乎我必须录制到文件,然后播放该文件?

这不是我想要做的,有没有办法通过媒体类将音频声音从麦克风传输到耳机?

我使用 Audio 类的代码如下,是否可以对 MediaPlayer 和 MediaRecorder 执行相同的操作?

public class Record extends Thread
 {

    static final int bufferSize = 200000;
    final short[] buffer = new short[bufferSize];
    short[] readBuffer = new short[bufferSize];

    public void run() {  
      isRecording = true;
      android.os.Process.setThreadPriority
      (android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);

      int buffersize = AudioRecord.getMinBufferSize(11025,
      AudioFormat.CHANNEL_CONFIGURATION_MONO,
      AudioFormat.ENCODING_PCM_16BIT);


                     arec = new AudioRecord(MediaRecorder.AudioSource.MIC,
                                     11025,
                                     AudioFormat.CHANNEL_CONFIGURATION_MONO,
                                     MediaRecorder.AudioEncoder.AMR_NB,
                                     buffersize);
                     atrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
                                     11025,
                                     AudioFormat.CHANNEL_CONFIGURATION_MONO,
                                     MediaRecorder.AudioEncoder.AMR_NB,
                                     buffersize,
                                     AudioTrack.MODE_STREAM);

                     Log.d("AUDIO", "sample rate = : " + arec.getSampleRate());

                     atrack.setPlaybackRate(11025);

                     byte[] buffer = new byte[buffersize];
                     arec.startRecording();
                     atrack.play();

                     while(isRecording) {
                             arec.read(buffer, 0, buffersize);
                             atrack.write(buffer, 0, buffer.length);
                     }  
    }
}

I currently use AudioRecord to record audio in from the mic of an Android device and then pipe that straight to an AudioTrack that plays it out the Earpiece of an Android device.

I'm wondering can I do the same but with the MediaPlayer and MediaRecorder classes instead as I need to use the AMR-NB encoding that is available on the Media classes and not the Audio classes.

My problem with MediaRecorder and MediaPlayer is that it seems like I have to record to a file and then play for that file?

This is not what I want to do, is there anyway to stream the audio sound from Mic to Earpiece with the Media classes?

My code using the Audio classes is below, is it possible to do something the same with the MediaPlayer and MediaRecorder?

public class Record extends Thread
 {

    static final int bufferSize = 200000;
    final short[] buffer = new short[bufferSize];
    short[] readBuffer = new short[bufferSize];

    public void run() {  
      isRecording = true;
      android.os.Process.setThreadPriority
      (android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);

      int buffersize = AudioRecord.getMinBufferSize(11025,
      AudioFormat.CHANNEL_CONFIGURATION_MONO,
      AudioFormat.ENCODING_PCM_16BIT);


                     arec = new AudioRecord(MediaRecorder.AudioSource.MIC,
                                     11025,
                                     AudioFormat.CHANNEL_CONFIGURATION_MONO,
                                     MediaRecorder.AudioEncoder.AMR_NB,
                                     buffersize);
                     atrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
                                     11025,
                                     AudioFormat.CHANNEL_CONFIGURATION_MONO,
                                     MediaRecorder.AudioEncoder.AMR_NB,
                                     buffersize,
                                     AudioTrack.MODE_STREAM);

                     Log.d("AUDIO", "sample rate = : " + arec.getSampleRate());

                     atrack.setPlaybackRate(11025);

                     byte[] buffer = new byte[buffersize];
                     arec.startRecording();
                     atrack.play();

                     while(isRecording) {
                             arec.read(buffer, 0, buffersize);
                             atrack.write(buffer, 0, buffer.length);
                     }  
    }
}

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

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

发布评论

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

评论(1

§对你不离不弃 2024-08-27 22:53:08

不,现在不行;不支持字节流输入是 MediaPlayer 的一个众所周知的限制。您仍然需要文件或 URI 输入源。

No, not at this time; it's a well-known limitation of MediaPlayer that byte stream inputs are not supported. You'd still need a file or URI input source.

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