本文整理匯總了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);
}
}