Live Audio Recording and Playing in Android and Thread & callback handling

后端 未结 2 1213
借酒劲吻你
借酒劲吻你 2020-11-29 00:58

I want to record the live audio and play it.As far as UI is concerned the app just has three buttons:one for start recording and streaming it, one for playing a pre recorded

相关标签:
2条回答
  • 2020-11-29 01:16

    My suggestion is to use Async task other wise known as painless threading. That's the best way to leverage from the threading in Android. You can make use of background processing, pre-execute and post execute methods by dividing your existing program.

    http://android-developers.blogspot.ca/2009/05/painless-threading.html

    0 讨论(0)
  • 2020-11-29 01:23

    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.

    0 讨论(0)
提交回复
热议问题