2012-11-27 20 views
5

Sto provando a registrare un po 'di voce utilizzando la classe AudioRecord e poi scriverlo sul file .pcm di output. Voglio che il mio programma continui a registrare fino a quando non viene premuto il pulsante stop. Sfortunatamente per quanto tempo sto registrando, la dimensione del file di output è sempre di 3528 byte e dura circa 20 ms. Sempre secondo Toolsoft Audio Tools, le proprietà di quel file sono: 44100Hz, 16 bit, stereo, anche se sto usando mono con frequenza di campionamento completamente diversa.AudioRecord - scrittura file PCM

Thread recordingThread; 
boolean isRecording = false; 


int audioSource = AudioSource.MIC; 
int sampleRateInHz = 44100; 
int channelConfig = AudioFormat.CHANNEL_IN_MONO; 
int audioFormat = AudioFormat.ENCODING_PCM_16BIT; 
int bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat); 

byte Data[] = new byte[bufferSizeInBytes]; 

AudioRecord audioRecorder = new AudioRecord(audioSource, 
              sampleRateInHz, 
              channelConfig, 
              audioFormat, 
              bufferSizeInBytes); 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
}  

public void startRecording(View arg0) { 
    audioRecorder.startRecording(); 
    isRecording = true; 
    recordingThread = new Thread(new Runnable() { 
     public void run() { 
      String filepath = Environment.getExternalStorageDirectory().getPath(); 
      FileOutputStream os = null; 
      try { 
       os = new FileOutputStream(filepath+"/record.pcm"); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } 
      while(isRecording) { 
       audioRecorder.read(Data, 0, Data.length); 
       try { 
        os.write(Data, 0, bufferSizeInBytes); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       try { 
        os.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    }); 
    recordingThread.start(); 
} 

public void stopRecording(View arg0) { 
    if (null != audioRecorder) { 
     isRecording = false; 
     audioRecorder.stop(); 
     audioRecorder.release(); 
     audioRecorder = null; 
     recordingThread = null; 
    } 
} 

Posso chiedere gentilmente di dirmi cosa c'è che non va? Spero che la risposta non sara 'tutto cio' :)

+0

Penso che il tuo codice IO potrebbe essere il problema. Prova a utilizzare Apache Commons IOUtils per copiare gli stream: http://commons.apache.org/io/api-release/org/apache/commons/io/IOUtils.html#copy(java.io.InputStream,%20java.io .OutputStream) –

risposta

2

Change your sample rate to 8000, perché in emulatore si cant test with 44100 sample rate.

uso AudioRecord fonte come mostrato a giocare in emulatore

private static final int RECORDER_SAMPLERATE = 8000; 
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO; 
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT; 

AudioRecord audio_record = new AudioRecord(MediaRecorder.AudioSource.MIC, 
      RECORDER_SAMPLERATE, RECORDER_CHANNELS, 
      RECORDER_AUDIO_ENCODING, BufferElements2Rec * BytesPerElement); 


int BufferElements2Play = 1024; // want to play 2048 (2K) since 2 bytes we use only 1024 
int BytesPerElement = 2; // 2 bytes in 16bit format 

If it din't get worked, then go to this blog. It will work fine for recording and storing the data in .pcm file.

+0

Sto usando il mio telefono, non l'emulatore. – Arxas

+0

Utilizzato l'intero codice da quel blog e l'app si è bloccata dopo aver premuto il pulsante che avvia la registrazione. – Arxas

+0

cambia la frequenza di campionamento in 44100 stereo codifica-16 bit, controlla. Non dovrebbe andare in crash puoi ottenere un file di log degli errori. –

1
try { 
       os.write(Data, 0, bufferSizeInBytes); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      try { 
       os.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

Questo è il problema. Stai chiudendo FileOutputStream (os.close()) dopo una sola scrittura. Sposta il ciclo while:

while(isRecording) { 
      audioRecorder.read(Data, 0, Data.length); 
      try { 
       os.write(Data, 0, bufferSizeInBytes); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

     } 
     try { 
       os.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
    }