简体   繁体   English

Android - 我可以使用MediaRecord / MediaPlayer而不是AudioRecord / Track吗?

[英]Android - Can I do this with MediaRecord/MediaPlayer instead of AudioRecord/Track?

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. 我目前使用AudioRecord从Android设备的麦克风录制音频,然后将其直接传送到播放Android设备耳机的AudioTrack。

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. 我想知道我可以做同样的事情,但使用MediaPlayer和MediaRecorder类,因为我需要使用Media类上可用的AMR-NB编码,而不是Audio类。

My problem with MediaRecorder and MediaPlayer is that it seems like I have to record to a file and then play for that file? 我的MediaRecorder和MediaPlayer的问题是,我似乎必须录制到一个文件,然后播放该文件?

This is not what I want to do, is there anyway to stream the audio sound from Mic to Earpiece with the Media classes? 这不是我想要做的,无论如何将音频声音从Mic转换为带有Media类的Earpiece?

My code using the Audio classes is below, is it possible to do something the same with the MediaPlayer and MediaRecorder? 我使用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);
                     }  
    }
}

No, not at this time; 不,不是在这个时候; it's a well-known limitation of MediaPlayer that byte stream inputs are not supported. MediaPlayer的一个众所周知的限制是不支持字节流输入。 You'd still need a file or URI input source. 您仍然需要文件或URI输入源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM