Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just add this code to your application and you will detect when user start to speak and when he stops.</p> <pre><code>public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 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. AudioRecord 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&lt;bufferSizeInBytes; i+=2 ) { sample = (short)( (audioBuffer[i]) | audioBuffer[i + 1] &lt;&lt; 8 ); totalAbsValue += Math.abs( sample ) / (numberOfReadBytes/2); } // Analyze temp buffer. tempFloatBuffer[tempIndex%3] = totalAbsValue; float temp = 0.0f; for( int i=0; i&lt;3; ++i ) temp += tempFloatBuffer[i]; if( (temp &gt;=0 &amp;&amp; temp &lt;= 350) &amp;&amp; recording == false ) { Log.i("TAG", "1"); tempIndex++; continue; } if( temp &gt; 350 &amp;&amp; recording == false ) { Log.i("TAG", "2"); recording = true; } if( (temp &gt;= 0 &amp;&amp; temp &lt;= 350) &amp;&amp; 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 &amp; 0xff); finalBuffer[5] = (byte) ((totalDataLen &gt;&gt; 8) &amp; 0xff); finalBuffer[6] = (byte) ((totalDataLen &gt;&gt; 16) &amp; 0xff); finalBuffer[7] = (byte) ((totalDataLen &gt;&gt; 24) &amp; 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 &amp; 0xff); finalBuffer[25] = (byte) ((longSampleRate &gt;&gt; 8) &amp; 0xff); finalBuffer[26] = (byte) ((longSampleRate &gt;&gt; 16) &amp; 0xff); finalBuffer[27] = (byte) ((longSampleRate &gt;&gt; 24) &amp; 0xff); finalBuffer[28] = (byte) (byteRate &amp; 0xff); finalBuffer[29] = (byte) ((byteRate &gt;&gt; 8) &amp; 0xff); finalBuffer[30] = (byte) ((byteRate &gt;&gt; 16) &amp; 0xff); finalBuffer[31] = (byte) ((byteRate &gt;&gt; 24) &amp; 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 &amp; 0xff); finalBuffer[41] = (byte) ((totalAudioLen &gt;&gt; 8) &amp; 0xff); finalBuffer[42] = (byte) ((totalAudioLen &gt;&gt; 16) &amp; 0xff); finalBuffer[43] = (byte) ((totalAudioLen &gt;&gt; 24) &amp; 0xff); for( int i=0; i&lt;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; } // -&gt; Recording sound here. Log.i( "TAG", "Recording Sound." ); for( int i=0; i&lt;numberOfReadBytes; i++ ) totalByteBuffer[totalReadBytes + i] = audioBuffer[i]; totalReadBytes += numberOfReadBytes; //*/ tempIndex++; } } </code></pre> <p>Check this <a href="http://vitobrothersoft.com/2011/11/04/voice-detection-for-android/">link</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload