Live Audio Recording and Playing in Android and Thread & callback handling
If your requirement is while it is recording it should play(means looping back audio), In your while loop thread, you are storing the recorded data (audioData bufer), there itself you can copy it to player object with in the while loop (player.write(audioData, 0, numShortsRead);). You said like your UI thread is stuck, it might be because of you are giving more priority to Audio record thread.
Check the below the code which I used for above loop back requirement
boolean m_isRun=true;
public void loopback() {
// Prepare the AudioRecord & AudioTrack
try {
buffersize = AudioRecord.getMinBufferSize(SAMPLE_RATE,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
if (buffersize <= BUF_SIZE) {
buffersize = BUF_SIZE;
}
Log.i(LOG_TAG,"Initializing Audio Record and Audio Playing objects");
m_record = new AudioRecord(MediaRecorder.AudioSource.MIC,
SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, buffersize * 1);
m_track = new AudioTrack(AudioManager.STREAM_ALARM,
SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, buffersize * 1,
AudioTrack.MODE_STREAM);
m_track.setPlaybackRate(SAMPLE_RATE);
} catch (Throwable t) {
Log.e("Error", "Initializing Audio Record and Play objects Failed "+t.getLocalizedMessage());
}
m_record.startRecording();
Log.i(LOG_TAG,"Audio Recording started");
m_track.play();
Log.i(LOG_TAG,"Audio Playing started");
while (m_isRun) {
m_record.read(buffer, 0, BUF_SIZE);
m_track.write(buffer, 0, buffer.length);
}
Log.i(LOG_TAG, "loopback exit");
}
private void do_loopback() {
m_thread = new Thread(new Runnable() {
public void run() {
loopback();
}
});
One more thing, If your requirement is record for few seconds and then play, while it is playing your record should start again, you can do that with a delay handler thread with a time out, In that thread you can stop recording copy the buffer, then start recording.
thread problem when recording audio
You are calling findViewById()
within record()
, but record()
is not running in the UI thread as you call it within the run()
method of your new thread. As the error says:
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views
Remove the call to findViewById()
and text.setText()
from your record()
method. Instead, try setting your UI elements in onClickRecord()
before you create the new thread.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView)findViewById(R.id.TextView01);
}
...
public void onClickRecord(View v){
text.setText("recording");
thread = new Thread(new Runnable() {
public void run() {
isRecording = true;
record();
}
});
thread.start();
isRecording = false;
}
Recording and saving audio based on click event android
Please check MediaRecorder api for android. You can record, save and use the file.
Audio Record and play loopback Issue
This issue is resolved, The problem is in device source, I tested it in many other devices. it is working fine.
Related Topics
Android Studio 3.1 Cannot Resolve Symbol (Themes, Widget, Attr, etc.)
How to Click or Tap on a Textview Text on Different Words
Getting Started with Open Nfc Emulator
Eclipse Android Project, How to Reference Library Within Workspace
How to Block Outgoing Calls and Text Sms
How to Compile C into an Executable Binary File and Run It in Android from Android Shell
Load the Image Saved in Sdcard in Webview
How to Schedule Notifications Using Workmanager
How to Use Android Canvas to Draw a Rectangle with Only Topleft and Topright Corners Round
Android: Making a Fullscreen Application
Android: Appwidget with Custom View Not Working
How to Play Local Swf Files in a Webview
Run-As Package 'A.B.C' Is Unknown - Galaxy S4 Jellybean or Android 4.3
How to Upload Multipart Form Data and Image to Server in Android
Firebase Push Notifications Update Db