Android development and learning-synchronous playback of recordings

Source: Internet
Author: User

I recently saw an article about audio. I suddenly remembered a friend from China Media University who asked me to help her design a program that can play the input audio in real time, I thought of using DirectSound at the time, but I felt a little scared about such things that I never touched, in addition, we use a language such as C # To write this relatively low-level thing, so this thing is gone. Fortunately, later this friend successfully completed the completion of the project. At this moment, I have encountered this problem again on Android. I will try to learn it with my determination. The main code is as follows:

Package com. android. record2play; import java. util. permission list; 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. app. activity; import android. view. menu; import android. widget. toast; public class MainActivity extends Activity {// write buffer size private int m_Record_Size; // audio recording object private AudioRecord mAudioRecord; // audio write storage byte array private byte [] m_Input_Bytes; // playback buffer size private int m_Play_Size; // audio playback object private AudioTrack mAudioTrack; // The main Thread private Thread Record2Play_Thread; // The flag variable private boolean IsRecording = true; @ SuppressWarnings ("deprecation") @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // obtain the minimum write cache size for recording m_Record_Size = AudioRecord. getMinBufferSize (44100, AudioFormat. CHANNEL_CONFIGURATION_MONO, AudioFormat. ENCODING_PCM_16BIT); // obtain the audio recording object mAudioRecord = new AudioRecord (MediaRecorder. audioSource. MIC, 44100, AudioFormat. CHANNEL_CONFIGURATION_MONO, AudioFormat. ENCODING_PCM_16BIT, m_Record_Size); // obtain the minimum playback buffer size m_Play_Size = AudioTrack. getMinBufferSize (8000, AudioFormat. CHANNEL_CONFIGURATION_MONO, AudioFormat. ENCODING_PCM_16BIT); // instantiate the playing audio object mAudioTrack = new AudioTrack (AudioManager. STREAM_MUSIC, 8000, AudioFormat. CHANNEL_CONFIGURATION_MONO, AudioFormat. ENCODING_PCM_16BIT, m_Play_Size, AudioTrack. MODE_STREAM); // initialization array m_Input_Bytes = new byte [m_Record_Size]; Record2Play_Thread = new Thread (new Record2Play (); Record2Play_Thread.start ();} class Record2Play implements Runnable {@ Overridepublic void run () {try {byte [] mBytes; // start recording mAudioRecord. startRecording (); mAudioTrack. play (); while (IsRecording) {int BytesSize = mAudioRecord. read (m_Input_Bytes, 0, m_Record_Size); mBytes = new byte [BytesSize]; mBytes = m_Input_Bytes.clone (); mAudioTrack. write (mBytes, 0, mBytes. length);} mAudioRecord. stop (); mAudioTrack. stop ();} catch (Exception e) {Toast. makeText (MainActivity. this, e. getMessage (), Toast. LENGTH_SHORT ). show ();}}}}

This is the same as the previous basic idea. First, we obtain an audio stream from the recording and write it into the buffer zone. Then, we extract it from the buffer zone and hand it over to the playing device for playback, but how do I feel that Android is so simple? To put this program into use, you need to solve the following problems:

1. Audio Recording Noise Reduction

2. Echo Elimination

3. system execution problems

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.