我想在android中制作流式音频录像机。我不知道如何拉取音频流,以及如何设置音频流的缓冲区大小。
下面是我的媒体录制器类
public class MyMediaRecorder {
final MediaRecorder recorder = new MediaRecorder();
final File path;
/**
* Creates a new audio recording at the given path (relative to root of SD
* card).
*/
public MyMediaRecorder(File path) {
this.path = path;
}
/**
* Starts a new recording.
*/
public void start() throws IOException {
String state = android.os.Environment.getExternalStorageState();
if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
throw new IOException("SD Card is not mounted. It is " + state
+ ".");
}
// make sure the directory we plan to store the recording in exists
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path.getAbsolutePath());
recorder.prepare();
recorder.start();
}
/**
* Stops a recording that has been previously started.
*/
public void stop() throws IOException {
recorder.stop();
recorder.release();
}
}在开始录制时,我需要获取一个缓冲区大小,并将其发送到服务器并行录制音频。我应该使用媒体录像机的录音和音轨吗?请建议我该怎么做
发布于 2020-01-25 08:41:46
您应该使用AudioRecord。下面是一个简单的代码,它展示了如何使用AudioRecord类。
final int sampleRate = 48000;
final int channelConfig = AudioFormat.CHANNEL_IN_MONO;
final int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
int minBufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
AudioRecord microphone = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, channelConfig, audioFormat, minBufferSize * 10);
microphone.startRecording();
//Since audioformat is 16 bit, we need to create a 16 bit (short data type) buffer
short[] buffer = new short[1024];
while(!stopped) {
int readSize = microphone.read(buffer, 0, buffer.length);
sendDataToServer(buffer, readSize);
}
microphone.stop();
microphone.release();如果你需要一个实时录音器和拖放器,你应该注意性能,尽可能快地读取所有数据,并管理它们发送到服务器。在Github中有很多项目,你可以重用它们,并从他们的想法中学习。我在这里分享了其中的一些(特别是recorder类),但你可以搜索并找到更多。
https://stackoverflow.com/questions/59336100
复制相似问题