本文整理汇总了Java中android.media.AudioRecord.read方法的典型用法代码示例。如果您正苦于以下问题:Java AudioRecord.read方法的具体用法?Java AudioRecord.read怎么用?Java AudioRecord.read使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.media.AudioRecord
的用法示例。
在下文中一共展示了AudioRecord.read方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: read
import android.media.AudioRecord; //导入方法依赖的package包/类
private int read(AudioRecord recorder) {
int numberOfBytes = recorder.read(mBuffer, 0, mBuffer.length); // Fill buffer
// Some error checking
if (numberOfBytes == AudioRecord.ERROR_INVALID_OPERATION) {
Log.e(LOG_TAG, "The AudioRecord object was not properly initialized");
return -1;
} else if (numberOfBytes == AudioRecord.ERROR_BAD_VALUE) {
Log.e(LOG_TAG, "The parameters do not resolve to valid data and indexes.");
return -2;
} else if (numberOfBytes > mBuffer.length) {
Log.e(LOG_TAG, "Read more bytes than is buffer length:" + numberOfBytes + ": " + mBuffer.length);
return -3;
} else if (numberOfBytes == 0) {
Log.e(LOG_TAG, "Read zero bytes");
return -4;
}
// Everything seems to be OK, adding the buffer to the recording.
add(mBuffer);
return 0;
}
示例2: run
import android.media.AudioRecord; //导入方法依赖的package包/类
@Override
public void run() {
byte[] buffer = new byte[audioBufferSize];
recorder = new AudioRecord(audioSource, sampleRate, channel, audioFormat, audioBufferSize);
try {
while (recorder.getState() != AudioRecord.STATE_INITIALIZED)
Thread.sleep(100, 0);
} catch (InterruptedException e) {
recorder.release();
return;
}
recorder.startRecording();
for (; ; ) {
int length = recorder.read(buffer, 0, buffer.length);
if (length < 0)
Log.e("Record", "error: " + Integer.toString(length));
else {
for (WebSocket ws : wss)
ws.sendBinary(buffer);
}
if (Thread.interrupted()) {
recorder.stop();
return;
}
}
}
示例3: StartRecord
import android.media.AudioRecord; //导入方法依赖的package包/类
public void StartRecord() {
Log.i(TAG,"开始录音");
isRecording=true;
//生成PCM文件
File file = audioFile;//new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/reverseme.pcm");
Log.i(TAG,"生成文件");
//如果存在,就先删除再创建
if (file.exists())
file.delete();
Log.i(TAG,"删除文件");
try {
file.createNewFile();
Log.i(TAG,"创建文件");
} catch (IOException e) {
Log.i(TAG,"未能创建");
throw new IllegalStateException("未能创建" + file.toString());
}
try {
//输出流
OutputStream os = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream dos = new DataOutputStream(bos);
int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency, channelConfiguration, audioEncoding, bufferSize);
byte[] buffer = new byte[bufferSize];
audioRecord.startRecording();
Log.i(TAG, "开始录音");
isRecording = true;
while (isRecording) {
int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
dos.write(buffer,0,bufferReadResult);
}
audioRecord.stop();
dos.close();
} catch (Throwable t) {
Log.e(TAG, "录音失败");
}
}
示例4: handleCodecInput
import android.media.AudioRecord; //导入方法依赖的package包/类
private void handleCodecInput(AudioRecord audioRecord, byte[] audioRecordData,
MediaCodec mediaCodec, ByteBuffer[] codecInputBuffers,
boolean running)
{
int length = audioRecord.read(audioRecordData, 0, audioRecordData.length);
int codecInputBufferIndex = mediaCodec.dequeueInputBuffer(10 * 1000);
if (codecInputBufferIndex >= 0) {
ByteBuffer codecBuffer = codecInputBuffers[codecInputBufferIndex];
codecBuffer.clear();
codecBuffer.put(audioRecordData);
mediaCodec.queueInputBuffer(codecInputBufferIndex, 0, length, 0, running ? 0 : MediaCodec.BUFFER_FLAG_END_OF_STREAM);
}
}