2012-11-27 34 views
5

Tôi đang cố gắng ghi lại một số giọng nói bằng cách sử dụng lớp AudioRecord và sau đó ghi nó vào tệp .pcm đầu ra. Tôi muốn chương trình của tôi tiếp tục ghi cho đến khi nhấn nút dừng. Unfortunatelly không có vấn đề bao lâu tôi đang ghi âm, kích thước tập tin đầu ra là luôn luôn 3528 byte và nó kéo dài trong khoảng 20 ms. Cũng theo Toolsoft Audio Tools, tính thích hợp của tập tin đó là: 44100Hz, 16 bit, âm thanh nổi, ngay cả khi tôi đang sử dụng đơn âm với tần số lấy mẫu hoàn toàn khác nhau.AudioRecord - ghi tệp 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; 
    } 
} 

Tôi có thể yêu cầu bạn cho tôi biết có vấn đề gì không? Tôi hy vọng câu trả lời sẽ không 'mọi thứ' :)

+0

Tôi nghĩ rằng mã IO của bạn có thể là vấn đề. Hãy thử sử dụng Apache Commons IOUtils để sao chép các luồng: http://commons.apache.org/io/api-release/org/apache/commons/io/IOUtils.html#copy(java.io.InputStream,%20java.io .OutputStream) –

Trả lời

2

Change your sample rate to 8000, bởi vì trong giả lập bạn cant test with 44100 sample rate.

sử dụng AudioRecord nguồn như để chơi trong giả lập

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

Tôi đang sử dụng điện thoại của mình, không phải trình giả lập. – Arxas

+0

Đã sử dụng toàn bộ mã từ blog và ứng dụng đó sau khi tôi nhấn nút để khởi chạy bản ghi. – Arxas

+0

thay đổi tốc độ mẫu thành 44100 stereo encoding-16bit, hãy kiểm tra. Nó không nên sụp đổ, bạn có thể nhận được một tập tin đăng nhập lỗi. –

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

Đây là vấn đề. Bạn đang đóng FileOutputStream (os.close()) chỉ sau một lần viết. Di chuyển nó ra khỏi vòng lặp 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(); 
      } 
    } 
Các vấn đề liên quan