In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about how Android implements the function of recording and broadcasting at the same time. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Android can realize recording through MediaRecorder and AudioRecord. MediaRecorder directly saves the data of the microphone to a file and can directly encode it (such as AMR,MP3, etc.), while AudioRecord reads the audio stream of the microphone. This paper uses AudioRecord to read the audio stream, uses AudioTrack to play the audio stream, and implements a simple hearing aid program by "playing while reading" and increasing the volume.
PS: since the current Android simulator does not support AudioRecord, this program needs to be compiled and run on the real machine.
Activity_layout:
MainActivity:
Import androidx.appcompat.app.AppCompatActivity;import androidx.core.app.ActivityCompat;import androidx.core.content.ContextCompat; import android.Manifest;import android.content.pm.PackageManager;import android.media.AudioFormat;import android.media.AudioManager;import android.media.AudioRecord;import android.media.AudioTrack;import android.media.MediaRecorder;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast; import java.util.LinkedList Public class MainActivity extends AppCompatActivity implements View.OnClickListener {private static final String TAG = "ktv"; / * * Button * / private Button bt_exit; / * * AudioRecord write buffer size * / protected int audio object * / private AudioRecord m_in_rec / * entered byte array * / private byte [] m playback bytes; / * store the size of the entered byte array * / private LinkedList playback buffer size * / private int m_out_buf_size / * playback audio object * / private AudioTrack audio outbound trk; / * Byte array played * / private byte [] mbroadcast outbound bytes; / * * recording audio thread * / private Thread record; / * * playback audio thread * / private Thread play / * the flag to stop the thread * / private boolean flag = true; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); this.setTitle ("audio loop"); requestAllPower (); Log.d ("dfdfd", "333333333333"); init () Record = new Thread (new recordSound ()); play = new Thread (new playRecord ()); / / start recording thread record.start (); / / start playback thread play.start ();} private void init () {bt_exit = findViewById (R.id.bt_yinpinhuilu_testing_exit) Log.i (TAG, "bt_exit====" + bt_exit); bt_exit.setOnClickListener (this); / / AudioRecord gets the minimum recording buffer size m_in_buf_size = AudioRecord.getMinBufferSize (8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT) / / instantiate the playback audio object m_in_rec = new AudioRecord (MediaRecorder.AudioSource.MIC, 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 (); / / AudioTrack to get the minimum playback buffer size m_out_buf_size = AudioTrack.getMinBufferSize (8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT) / / instantiate 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 m_out_bytes = new byte [m _ out_buf_size] with the minimum playback buffer size;} public void onClick (View v) {/ / TODO Auto-generated method stub switch (v.getId ()) {case R.id.bt_yinpinhuilu_testing_exit: flag = false M_in_rec.stop (); m_in_rec = null; m_out_trk.stop (); m_out_trk = null; this.finish () }} / * recording thread * / class recordSound implements Runnable {@ Override public void run () {Log.i (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 (); Log.i (TAG, ".recordSound bytes_pkg==" + bytes_pkg.length) If (m_in_q.size () > = 2) {m_in_q.removeFirst ();} m_in_q.add (bytes_pkg) } / * playback thread * / class playRecord implements Runnable {@ Override public void run () {/ / TODO Auto-generated method stub Log.i (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) {e.printStackTrace () } / * recording oh dynamic permissions * / public void requestAllPower () {if (ContextCompat.checkSelfPermission (this, Manifest.permission.RECORD_AUDIO)! = PackageManager.PERMISSION_GRANTED) {if (ActivityCompat.shouldShowRequestPermissionRationale (this)) Manifest.permission.RECORD_AUDIO) {} else {ActivityCompat.requestPermissions (this, new String [] {Manifest.permission.RECORD_AUDIO, Manifest.permission.READ_EXTERNAL_STORAGE}, 1) } @ Override public void onRequestPermissionsResult (int requestCode, String [] permissions, int [] grantResults) {super.onRequestPermissionsResult (requestCode, permissions, grantResults); if (requestCode = = 1) {for (int I = 0; I < permissions.length) Toast.makeText +) {Toast.makeText (this, "" + "permission" + permissions [I] + "Application successful", Toast.LENGTH_SHORT) .show ();}
AndroidManifest.xml
Thank you for reading! This is the end of the article on "how to record and broadcast while Android". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.