SoFunction
Updated on 2025-03-02

Android implements the application while recording and broadcasting

This article shares the specific code for Android recording and broadcasting for your reference. The specific content is as follows

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := testRecord
include $(BUILD_PACKAGE)

<?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:andro
    package=""
    android:versionCode="1"
    android:versionName="1.0">
  <uses-permission android:name=".RECORD_AUDIO"></uses-permission>  
  <application
    android:icon="@drawable/icon"
    android:label="Bug Report Sender">
      <activity android:name=".testRecord"
                android:label="@string/app_name">
        <intent-filter>
          <action android:name=""/>
          <category android:name=""/>
        </intent-filter>
      </activity>
    </application>
</manifest>

/layout/

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:andro  
    android:orientation="vertical" android:layout_width="fill_parent"  
    android:layout_height="fill_parent">  
  
    <Button android:layout_height="wrap_content" android:  
        android:layout_width="fill_parent" android:text="@string/btnR"></Button>  
    <Button android:layout_height="wrap_content"  
        android:layout_width="fill_parent" android:text="@string/btnS" android:></Button>  
    <Button android:layout_height="wrap_content" android:  
        android:layout_width="fill_parent" android:text="@string/btnE"></Button>  
    <TextView android: android:layout_height="wrap_content"  
        android:text="@string/textV" android:layout_width="fill_parent"></TextView>  
    <SeekBar android:layout_height="wrap_content" android:  
        android:layout_width="fill_parent"></SeekBar>  
  
</LinearLayout>

/values/

<?xml version="1.0" encoding="utf-8"?>
    <resources>
       <string name="app_name">bianlubianbo</string>
       <string name="btnR">start</string>
       <string name="btnS">stop</string>
       <string name="btnE">exit</string>
       <string name="textV">vlounm</string>
</resources>

/drawable/
/com/testRecord/

package ;  
  
import ;  
import ;  
import ;  
import ;  
import ;  
import ;  
import ;  
import ;  
import ;  
import ;  
import ;  
  
public class testRecord extends Activity {  
    /** Called when the activity is first created. */  
    Button btnRecord, btnStop, btnExit;  
    SeekBar skbVolume;//Adjust the volume    boolean isRecording = false;//Whether to record the mark    static final int frequency = 8000;//44100;  
    static final int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;  
    static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;  
    int recBufSize,playBufSize;  
    AudioRecord audioRecord;  
    AudioTrack audioTrack;  
  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        (savedInstanceState);  
        setContentView();  
        setTitle("Hearing aid");  
        recBufSize = (frequency,  
                channelConfiguration, audioEncoding);  
  
        playBufSize=(frequency,  
                channelConfiguration, audioEncoding);  
        // -----------------------------------------  
        audioRecord = new AudioRecord(, frequency,  
                channelConfiguration, audioEncoding, recBufSize*10);  
  
        audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, frequency,  
                channelConfiguration, audioEncoding,  
                playBufSize, AudioTrack.MODE_STREAM);  
        //------------------------------------------  
        btnRecord = (Button) ();  
        (new ClickEvent());  
        btnStop = (Button) ();  
        (new ClickEvent());  
        btnExit = (Button) ();  
        (new ClickEvent());  
        skbVolume=(SeekBar)();  
        (100);//The limit of volume adjustment        (70);//Set the position value of seekbar        (0.7f, 0.7f);//Set the current volume        (new () {  
              
            @Override  
            public void onStopTrackingTouch(SeekBar seekBar) {  
                float vol=(float)(())/(float)(());  
                (vol, vol);//Set the volume            }  
              
            @Override  
            public void onStartTrackingTouch(SeekBar seekBar) {  
                // TODO Auto-generated method stub  
            }  
              
            @Override  
            public void onProgressChanged(SeekBar seekBar, int progress,  
                    boolean fromUser) {  
                // TODO Auto-generated method stub  
            }  
        });  
    }  
  
    @Override  
    protected void onDestroy() {  
        ();  
        (());  
    }  
  
    class ClickEvent implements  {  
  
        @Override  
        public void onClick(View v) {  
            if (v == btnRecord) {  
                isRecording = true;  
                new RecordPlayThread().start();// Open a thread while recording and playing            } else if (v == btnStop) {  
                isRecording = false;  
            } else if (v == btnExit) {  
                isRecording = false;  
                ();  
            }  
        }  
    }  
  
    class RecordPlayThread extends Thread {  
        public void run() {  
            try {  
                byte[] buffer = new byte[recBufSize];  
                ();//Start recording                ();//Start playback                  
                while (isRecording) {  
                    //Save data from MIC to buffer                    int bufferReadResult = (buffer, 0,  
                            recBufSize);  
  
                    byte[] tmpBuf = new byte[bufferReadResult];  
                    (buffer, 0, tmpBuf, 0, bufferReadResult);  
                    //Write data and play                    (tmpBuf, 0, );  
                }  
                ();  
                ();  
            } catch (Throwable t) {  
                (, (), 1000);  
            }  
        }  
    };  
} 

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.