Android can realize recording through the two tools MediaRecorder and AudioRecord. MediaRecorder directly saves the microphone data to a file and can be directly encoded (such as AMR, MP3, etc.), while AudioRecord reads the microphone audio stream. This article uses AudioRecord to read the audio stream, uses AudioTrack to play the audio stream, and implements a simple hearing aid program by "reading and playing" and increasing the volume.
PS: Since the current Android emulator does not support AudioRecord, this program needs to be compiled and put on the real machine to run.
Activity_layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android: android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Close" /> </LinearLayout>
MainActivity:
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class MainActivity extends AppCompatActivity implements { private static final String TAG = "ktv"; /** * Button */ private Button bt_exit; /** * AudioRecord write buffer size */ protected int m_in_buf_size; /** * Record audio objects */ private AudioRecord m_in_rec; /** * Entered byte array */ private byte[] m_in_bytes; /** * Storing the size of the entered byte array */ private LinkedList<byte[]> m_in_q; /** * AudioTrack playback buffer size */ private int m_out_buf_size; /** * Play audio objects */ private AudioTrack m_out_trk; /** * Played byte array */ private byte[] m_out_bytes; /** * Record audio thread */ private Thread record; /** * Play audio thread */ private Thread play; /** * Flag to stop thread */ private boolean flag = true; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); ("Audio Loop"); requestAllPower(); ("dfdfd", "333333333333"); init(); record = new Thread(new recordSound()); play = new Thread(new playRecord()); // Start the recording thread (); // Start the playback thread (); } private void init() { bt_exit = findViewById(.bt_yinpinhuilu_testing_exit); (TAG, "bt_exit====" + bt_exit); bt_exit.setOnClickListener(this); // AudioRecord gets the size of the minimum buffer to record m_in_buf_size = (8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT); // Instantiate the playback audio object m_in_rec = new AudioRecord(, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, m_in_buf_size); // Instantiate a byte array with the length of the minimum buffer m_in_bytes = new byte[m_in_buf_size]; // Instantiate a linked list to store the number of byte groups m_in_q = new LinkedList<byte[]>(); // AudioTrack gets the size of the minimum playback buffer m_out_buf_size = (8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT); // Instantiate the playback audio object m_out_trk = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, m_out_buf_size, AudioTrack.MODE_STREAM); // Instantiate a byte array with a length of playback minimum buffer size m_out_bytes = new byte[m_out_buf_size]; } public void onClick(View v) { // TODO Auto-generated method stub switch (()) { case .bt_yinpinhuilu_testing_exit: flag = false; m_in_rec.stop(); m_in_rec = null; m_out_trk.stop(); m_out_trk = null; (); } } /** * Recording thread */ class recordSound implements Runnable { @Override public void run() { (TAG, "........recordSound run()......"); byte[] bytes_pkg; // Start recording m_in_rec.startRecording(); while (flag) { m_in_rec.read(m_in_bytes, 0, m_in_buf_size); bytes_pkg = m_in_bytes.clone(); (TAG, "........recordSound bytes_pkg==" + bytes_pkg.length); if (m_in_q.size() >= 2) { m_in_q.removeFirst(); } m_in_q.add(bytes_pkg); } } } /** * Play thread */ class playRecord implements Runnable { @Override public void run() { // TODO Auto-generated method stub (TAG, "........playRecord run()......"); byte[] bytes_pkg = null; // Start playing m_out_trk.play(); while (flag) { try { m_out_bytes = m_in_q.getFirst(); bytes_pkg = m_out_bytes.clone(); m_out_trk.write(bytes_pkg, 0, bytes_pkg.length); } catch (Exception e) { (); } } } } /** * Recording dynamic permission */ public void requestAllPower() { if ((this, .RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) { if ((this, .RECORD_AUDIO)) { } else { (this, new String[]{.RECORD_AUDIO, .READ_EXTERNAL_STORAGE}, 1); } } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { (requestCode, permissions, grantResults); if (requestCode == 1) { for (int i = 0; i < ; i++) { (this, "" + "Permissions" + permissions[i] + "Application successful", Toast.LENGTH_SHORT).show(); } } } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:andro package=""> <uses-permission android:name=".WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name=".RECORD_AUDIO" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="" /> <category android:name="" /> </intent-filter> </activity> </application> </manifest>
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.