The address of this project, the name is recording and playback of PCM, seeking star
Https://github.com/979451341/Audio-and-video-learning-materials
1.AudioTrack Official description
The Audiotrack allows the PCM audio buffer to stream to the audio receiver for playback. This is the signal by "pushing" the data object using the Write (byte[], int, int) and write (short[], int, int) methods.
A signal can be run in two modes: static or flow.
In streaming mode, the application writes a continuous stream of signals, using the Write () method. These are closed and returned when the data has been moved from the Java layer to the local tier to be queued for playback. Streaming mode is most useful when playing audio data blocks:
Because the duration of the playback sound is too large for memory, because of the characteristics of the audio data (high sampling rate)
When dealing with short sounds that are appropriate for memory, you should select static mode and need to play with minimal delay. As a result, static mode is more suitable for frequently played UI and game sounds, and may be minimal overhead.
In authoring, a channel object initializes its associated audio buffers. The size of this buffer, stipulated during the construction process, determines how long the signal can run out of the data before playing.
Using a static mode signal, which is the size of the sound, it can play its maximum size.
For streaming mode, the data is written to the audio receiver, whose size is less than or equal to the total buffer size. The signal is not the end point, which allows subclasses, but is not recommended for use.
2.AudioTrack How to create and configure
static public int getMinBufferSize(int sampleRateInHz, int channelConfig, int audioFormat)public AudioTrack(int streamType, int sampleRateInHz, int channelConfig, int audioFormat, int bufferSizeInBytes, int mode)
SAMPLERATEINHZ: The acquisition rate, there are 8000, 20100, etc., generally the higher the sound quality, but the larger the size of the file.
Streamtype: type of audio stream, Stream_voice_call, Stream_system, stream_ring, Stream_music, Stream_alarm, and STREAM_NOTIFICATION , this parameter is related to the Audiomanager in Android, which involves the audio management strategy on the phone.
Channelconfig: Channel, mono Channel_out_mono and dual channel Channel_out_stereo
Audioformat: Sample point size, only Encoding_pcm_16bit and encoding_pcm_8bit two options, meaning a collection point 16bit or 8bit
Buffersizeinbytes:audiotrack can receive the smallest sound resource size at a time, obtained through the getminbuffersize function,
Mode: There are two categories of mode_static and Mode_stream.
Stream means that the user writes the data once in the application by writing it to Audiotrack, which is inefficient.
Static meaning is the beginning of the creation of the time, the audio data into a fixed buffer, and then directly to the Audiotrack, only read once, this method for the ringtone and other memory consumption is small, high latency requirements of the sound is very suitable.
3.AudioTrack use
In fact, this and audiorecord the same reason, because is played, so the play file exists, directly read the file, through the stream form once read the data, while playing
First create the Audiotrack
buffersize = audiotrack.getminbuffersize (8000,
Audioformat.channel_in_stereo, Audioformat.encoding_pcm_16bit);
// 实例AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM);
Then
Track.play ()
And then loop through the data, stop () when you're done reading it, halt the loop if you interrupt.
track.play(); //writeToFileHead(); while (isStart) { if (null != track&&dis.available() > 0) { int i = 0; while (dis.available() > 0 && i < buffer.length){ buffer[i] = dis.readShort(); i++; } track.write(buffer,0,buffer.length); } } track.stop();
Attention is paid to the timely release of resources
if (track != null) { if (track.getState() == AudioRecord.STATE_INITIALIZED) { track.stop(); } if (track != null) { track.release(); } } if (dis != null) { dis.close(); }
Comparison of 4.AudioRecord and Audiotrack
The function of the two is just the opposite, one produces PCM a read PCM, and the operation of the process is very similar, all convection is a unique, all the time to eat so much, slowly eating,
mRecorder.startRecording(); //writeToFileHead(); while (isStart) { if (null != mRecorder) { bytesRecord = mRecorder.read(tempBuffer, 0, bufferSize); if (bytesRecord == AudioRecord.ERROR_INVALID_OPERATION || bytesRecord == AudioRecord.ERROR_BAD_VALUE) { continue; } if (bytesRecord != 0 && bytesRecord != -1) { //在此可以对录制音频的数据进行二次处理 比如变声,压缩,降噪,增益等操作 //我们这里直接将pcm音频原数据写入文件 这里可以直接发送至服务器 对方采用AudioTrack进行播放原数据 dos.write(tempBuffer, 0, bytesRecord); } else { break; } } }
Even the release of resources is similar
if (mRecorder != null) { if (mRecorder.getState() == AudioRecord.STATE_INITIALIZED) { mRecorder.stop(); } if (mRecorder != null) { mRecorder.release(); } } if (dos != null) { dos.flush(); dos.close(); }
Complete code please see the article First project address
Android Audio Video In-depth two audiotrack playback pcm (with source download)