Android program that Streams MIC directly to Speaker of Phone

0 votes

I'm developing an Android app that streams the microphone to the phone's speaker directly. The programme and UI both hang, despite the working code. But even if the app hangs, audio transfer continues to function. What is the mistake?

 RecordBufferSize=AudioRecord.getMinBufferSize(sampleRateInHz,AudioFormat.CHANNEL_IN_MONO,AudioFormat.ENCODING_PCM_16BIT);
    TrackBufferSize= AudioTrack.getMinBufferSize(sampleRateInHz,AudioFormat.CHANNEL_OUT_MONO,AudioFormat.ENCODING_PCM_16BIT);

    am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    Record record = new Record();
    record.run();

}
public class Record extends Thread
{

    final short[] buffer = new short[RecordBufferSize];
    short[] readBuffer = new short[TrackBufferSize];

    public void run() {
        isRecording = true;
        android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
        AudioRecord arec = new AudioRecord(MediaRecorder.AudioSource.MIC,sampleRateInHz,AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT,RecordBufferSize);
        AudioTrack atrack = new AudioTrack(AudioManager.STREAM_MUSIC,sampleRateInHz,AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, TrackBufferSize, AudioTrack.MODE_STREAM);
        //am.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL);
        atrack.setPlaybackRate(sampleRateInHz);
        byte[] buffer = new byte[RecordBufferSize];
        arec.startRecording();
        atrack.play();
        while(isRecording) {
            AudioLenght=arec.read(buffer, 0, RecordBufferSize);
            atrack.write(buffer, 0, AudioLenght);
        }
        arec.stop();
        atrack.stop();
        isRecording = false;
    }
}

above is my code.

Dec 10, 2022 in Android by sarit
• 1,830 points
606 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Android

0 votes
0 answers

How to Calculate Height of Android Phone from ground?

In order to complete a project, I ...READ MORE

Nov 24, 2022 in Android by Tejashwini
• 3,820 points
352 views
0 votes
1 answer

Open Contact information in Contact List using Phone Number of Contact Android Studio

You must first obtain the contact's CONTACT ...READ MORE

answered Nov 7, 2022 in Android by Edureka
• 12,690 points
654 views
0 votes
1 answer

How to open the Google Play Store directly from my Android application?

If you want to link to your ...READ MORE

answered Nov 7, 2022 in Android by Edureka
• 12,690 points
3,307 views
0 votes
0 answers

How to open the Google Play Store directly from my Android application?

I have open the Google Play store ...READ MORE

Nov 23, 2022 in Android by Ashwini
• 5,430 points
434 views
0 votes
0 answers
0 votes
0 answers

"unable to locate adb" using Android Studio

I've been attempting to test my software ...READ MORE

Sep 21, 2022 in Android by Edureka
• 13,620 points
370 views
0 votes
0 answers

What is audio focus in Android class AudioManager?

There are two methods in the AudioManager ...READ MORE

Nov 25, 2022 in Android by Edureka
• 13,620 points
352 views
0 votes
1 answer

Running docker on Android

According to the documentation, the Android kernel is ...READ MORE

answered Aug 1, 2018 in Docker by Kalgi
• 52,360 points
3,402 views
0 votes
1 answer

Task Canceled Exception while invoking AWS Lambda

I'm guessing either the TaskCanceledException instance is ...READ MORE

answered Sep 19, 2018 in AWS by Priyaj
• 58,090 points
2,195 views
0 votes
1 answer

Is there a way to run Python on Android?

YES! An example via Matt Cutts via SL4A -- "here’s ...READ MORE

answered Sep 19, 2018 in Python by Priyaj
• 58,090 points
832 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP