Android:监听器在任何声音发生时录制声音
Posted
技术标签:
【中文标题】Android:监听器在任何声音发生时录制声音【英文标题】:Android: Listener to record sound if any sound occurs 【发布时间】:2012-03-08 13:40:13 【问题描述】:在我的应用程序中,我想实现在发生任何声音时记录声音的方法。
类似这个应用的东西:Talking Tom Cat
那我该怎么办? 有没有什么demo可以监听声音,有声音就录下来?
【问题讨论】:
【参考方案1】:在您的应用程序中尝试此代码...
private int RECORDER_CHANNELS = AudioFormat.CHANNEL_CONFIGURATION_MONO;
private int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
private int RECORDER_SAMPLERATE = 44100;
private byte RECORDER_BPP = (byte) 16;
private AudioRecord audioRecorder;
public void arm()
// Get the minimum buffer size required for the successful creation of an AudioRecord object.
int bufferSizeInBytes = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE, RECORDER_CHANNELS,
RECORDER_AUDIO_ENCODING);
// Initialize Audio Recorder.
audioRecorder = new AudioRecord(MediaRecorder.Audiosource.MIC, RECORDER_SAMPLERATE,
RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, bufferSizeInBytes);
// Start Recording.
audioRecorder.startRecording();
int numberOfReadBytes = 0;
byte audioBuffer[] = new byte[bufferSizeInBytes];
boolean recording = false;
float tempFloatBuffer[] = new float[3];
int tempIndex = 0;
int totalReadBytes = 0;
byte totalByteBuffer[] = new byte[60 * 44100 * 2];
// While data come from microphone.
while (true)
float totalAbsValue = 0.0f;
short sample = 0;
numberOfReadBytes = audioRecorder.read(audioBuffer, 0, bufferSizeInBytes);
// Analyze Sound.
for (int i = 0; i < bufferSizeInBytes; i += 2)
sample = (short) ((audioBuffer[i]) | audioBuffer[i + 1] << 8);
totalAbsValue += Math.abs(sample) / (numberOfReadBytes / 2);
// Analyze temp buffer.
tempFloatBuffer[tempIndex % 3] = totalAbsValue;
float temp = 0.0f;
for (int i = 0; i < 3; ++i)
temp += tempFloatBuffer[i];
if ((temp >= 0 && temp <= 350) && recording == false)
Log.i("TAG", "1");
tempIndex++;
continue;
if (temp > 350 && recording == false)
Log.i("TAG", "2");
recording = true;
if ((temp >= 0 && temp <= 350) && recording == true)
Log.i("TAG", "Save audio to file.");
// Save audio to file.
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath, "AudioRecorder");
if (!file.exists())
file.mkdirs();
String fn = file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".wav";
long totalAudioLen = 0;
long totalDataLen = totalAudioLen + 36;
long longSampleRate = RECORDER_SAMPLERATE;
int channels = 1;
long byteRate = RECORDER_BPP * RECORDER_SAMPLERATE * channels / 8;
totalAudioLen = totalReadBytes;
totalDataLen = totalAudioLen + 36;
byte finalBuffer[] = new byte[totalReadBytes + 44];
finalBuffer[0] = 'R'; // RIFF/WAVE header
finalBuffer[1] = 'I';
finalBuffer[2] = 'F';
finalBuffer[3] = 'F';
finalBuffer[4] = (byte) (totalDataLen & 0xff);
finalBuffer[5] = (byte) ((totalDataLen >> 8) & 0xff);
finalBuffer[6] = (byte) ((totalDataLen >> 16) & 0xff);
finalBuffer[7] = (byte) ((totalDataLen >> 24) & 0xff);
finalBuffer[8] = 'W';
finalBuffer[9] = 'A';
finalBuffer[10] = 'V';
finalBuffer[11] = 'E';
finalBuffer[12] = 'f'; // 'fmt ' chunk
finalBuffer[13] = 'm';
finalBuffer[14] = 't';
finalBuffer[15] = ' ';
finalBuffer[16] = 16; // 4 bytes: size of 'fmt ' chunk
finalBuffer[17] = 0;
finalBuffer[18] = 0;
finalBuffer[19] = 0;
finalBuffer[20] = 1; // format = 1
finalBuffer[21] = 0;
finalBuffer[22] = (byte) channels;
finalBuffer[23] = 0;
finalBuffer[24] = (byte) (longSampleRate & 0xff);
finalBuffer[25] = (byte) ((longSampleRate >> 8) & 0xff);
finalBuffer[26] = (byte) ((longSampleRate >> 16) & 0xff);
finalBuffer[27] = (byte) ((longSampleRate >> 24) & 0xff);
finalBuffer[28] = (byte) (byteRate & 0xff);
finalBuffer[29] = (byte) ((byteRate >> 8) & 0xff);
finalBuffer[30] = (byte) ((byteRate >> 16) & 0xff);
finalBuffer[31] = (byte) ((byteRate >> 24) & 0xff);
finalBuffer[32] = (byte) (2 * 16 / 8); // block align
finalBuffer[33] = 0;
finalBuffer[34] = RECORDER_BPP; // bits per sample
finalBuffer[35] = 0;
finalBuffer[36] = 'd';
finalBuffer[37] = 'a';
finalBuffer[38] = 't';
finalBuffer[39] = 'a';
finalBuffer[40] = (byte) (totalAudioLen & 0xff);
finalBuffer[41] = (byte) ((totalAudioLen >> 8) & 0xff);
finalBuffer[42] = (byte) ((totalAudioLen >> 16) & 0xff);
finalBuffer[43] = (byte) ((totalAudioLen >> 24) & 0xff);
for (int i = 0; i < totalReadBytes; ++i)
finalBuffer[44 + i] = totalByteBuffer[i];
FileOutputStream out;
try
out = new FileOutputStream(fn);
try
out.write(finalBuffer);
out.close();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (FileNotFoundException e1)
// TODO Auto-generated catch block
e1.printStackTrace();
// */
tempIndex++;
break;
// -> Recording sound here.
Log.i("TAG", "Recording Sound.");
for (int i = 0; i < numberOfReadBytes; i++)
totalByteBuffer[totalReadBytes + i] = audioBuffer[i];
totalReadBytes += numberOfReadBytes;
// */
tempIndex++;
有关更多详细信息,您还可以查看此演示...
http://musicg.googlecode.com/files/musicg_android_demo.zip
【讨论】:
真的很好的例子。但它只检测哨声。你知道在这种情况下如何检测正常的声音吗? 值得更多的赞成票非常有帮助。感谢您花时间创建此示例。 @iDroidExplorer 和其他任何感兴趣的人 - 我已经修改了代码以包含缺失的变量。它适用于我,但希望其他人也验证它适用于他们(如果需要,重新编辑您的更改) @kape123 我有兴趣看到您的更改。可以分享一下吗? @nikib3ro 你好。我怎样才能让这个听众工作更多时间?例如,当我说“一二三”时,它只记录“一”【参考方案2】:请访问有关Audio Capture的Android开发者页面。
它有一个示例代码,演示如何捕捉和播放声音。
【讨论】:
请仔细阅读标题。我说过我想要监听声音的演示,如果有任何声音,它会记录那个声音。 你先看到那个链接了吗? 是的,我看到了那个链接。我不想在按钮单击时实现记录开始。我想要的是如果有声音应该自动录制。 我没有代码,但想要让设备监听任何声音发生的代码。【参考方案3】:我正在尝试类似的东西。根据我所见,您必须先开始录制,然后才能读取音频电平。所以我尝试测量幅度,然后增加记录的长度,如果 MaxAmplitude 达到可听点,则将文件保存到不同的位置。
话虽如此,我还是个编程新手,非常感谢任何其他提示或技巧。
【讨论】:
以上是关于Android:监听器在任何声音发生时录制声音的主要内容,如果未能解决你的问题,请参考以下文章