开发者

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

开发者 https://www.devze.com 2022-12-19 15:17 出处:网络
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 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 = 开发者_JAVA百科: " + 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. You'd still need a file or URI input source.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号