[英]How to get an Audio Stream from Media Recorder in android
I want to make streaming audio recorder in android.我想在android中制作流媒体录音机。 I am not sure how to fetch audio stream and how to set buffer size of audio stream.我不确定如何获取音频流以及如何设置音频流的缓冲区大小。
Below is my media recorder class下面是我的媒体记录器类
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();
}
}
on start of recording i need to fetch a buffer size and sent it to server parallely record audio.在录制开始时,我需要获取缓冲区大小并将其发送到服务器并行录制音频。 Should I use Audio Record and Audio track instaed of Media Recorder ?我应该使用 Media Recorder 的 Audio Record 和 Audio track instaed 吗? Please suggest what should i do请建议我该怎么做
You should use AudioRecord .您应该使用AudioRecord 。 Here is a simple code that shows how to use AudioRecord
class.这是一个简单的代码,展示了如何使用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();
If you need a real-time audio recorder and streamer, you should be careful about performance and read all data as fast as possible and manage them to send to the server.如果您需要实时录音机和流媒体,您应该注意性能并尽快读取所有数据并管理它们以发送到服务器。 There are lot's of projects in Github that you can reuse them and learn from their idea. Github 中有很多项目,你可以重用它们并从他们的想法中学习。 I share some of them (specially recorder class) here but you can search and find more.我在这里分享了其中的一些(特别是录音机类),但您可以搜索并找到更多。
1. AudioRecorder 1. 录音机
2. FullDuplexNetworkAudioCallService 2. FullDuplexNetworkAudioCallService
3. MicRecorder 3. 麦克风录音机
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.