本文主要是介绍Android下使用 AudioRecord 和 AudioTrack 实现音频 PCM 数据的采集和播放,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、原理
AudioRecord的执行过程:
int buffersize = AudioRecord.getMinBufferSize(11025, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,11025, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT, buffersize);
audioRecord.startRecording();//开始录音
audioRecord.read(buffer, 0, bufferReadSize);
AudioTrack的执行过程:
int bufsize = AudioTrack.getMinBufferSize(11025, AudioFormat.CHANNEL_IN_STEREO,AudioFormat.ENCODING_PCM_16BIT);audioplayer = new AudioTrack(AudioManager.STREAM_MUSIC, 11025,AudioFormat.CHANNEL_IN_STEREO,AudioFormat.ENCODING_PCM_16BIT, bufsize,AudioTrack.MODE_STREAM);audioplayer.play();//开始接受数据流播放audioplayer.write(buffer, 0, size);//不断播放数据
二、实现
1、添加权限:
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
2、MAudioPlayer.java文件:
package com.example.tongjiangsong.audiorecordtrackdemo;import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;public class MAudioPlayer {private PipedInputStream instream;private boolean isPlaying ;private AudioTrack audioplayer;private byte[] buffer;public MAudioPlayer() {isPlaying = false;instream = null;//初始化播音类int bufsize = AudioTrack.getMinBufferSize(11025, AudioFormat.CHANNEL_IN_STEREO,AudioFormat.ENCODING_PCM_16BIT);audioplayer = new AudioTrack(AudioManager.STREAM_MUSIC, 11025, AudioFormat.CHANNEL_IN_STEREO,AudioFormat.ENCODING_PCM_16BIT, bufsize,AudioTrack.MODE_STREAM);}//设置管道流,用于接受音频数据public void setOutputStream(PipedOutputStream out) throws IOException{instream = new PipedInputStream(out);}public void startPlayAudio(){ //调用之前先调用setOutputStream 函数isPlaying = true;audioplayer.play();//开始接受数据流播放buffer = new byte[1024];while (instream!=null&&isPlaying){try {while (instream.available()>0){int size = instream.read(buffer);audioplayer.write(buffer, 0, size);//不断播放数据}} catch (IOException e) {e.printStackTrace();}}}public void stopPlay(){//停止播放isPlaying = false ;try {instream.close();} catch (IOException e) {e.printStackTrace();}audioplayer.stop();}}
3、MAudioRecord.java文件:
package com.example.tongjiangsong.audiorecordtrackdemo;import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;/** To getaudio or play audio* */
public class MAudioRecord {private AudioRecord audioRecord;private Context context;private boolean isRecording = false ;private PipedOutputStream outstream ;//利用管道传输数据public MAudioRecord(Context context , PipedInputStream instream) throws IOException {this.context = context;//初始化管道流 用于向外传输数据outstream = new PipedOutputStream();outstream.connect(instream);}public void StartAudioData(){//得到录音数据int frequency = 11025;int channelConfiguration = AudioFormat.CHANNEL_IN_STEREO;int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;int buffersize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,frequency, channelConfiguration, audioEncoding, buffersize);byte[]buffer = new byte[buffersize];audioRecord.startRecording();//开始录音isRecording = true;int bufferReadSize = 1024;while (isRecording){audioRecord.read(buffer, 0, bufferReadSize);try {outstream.write(buffer, 0, bufferReadSize);} catch (IOException e) {e.printStackTrace();}}}public void stopRecord(){//停止录音isRecording = false;audioRecord.stop();try {outstream.close();} catch (IOException e) {e.printStackTrace();}}}
4、activity_main.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Record"tools:layout_editor_absoluteX="108dp"tools:layout_editor_absoluteY="162dp" /></LinearLayout>
这篇关于Android下使用 AudioRecord 和 AudioTrack 实现音频 PCM 数据的采集和播放的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!