以下代码应记录音频并以PCM格式存储到SD卡中.
代码正在与我合作,但PCM文件无法播放!!!!
代码正在与我合作,但PCM文件无法播放!!!!
我从这个链接获得了这个代码…. Android : recording audio using audiorecord class play as fast forwarded
我需要播放PCM文件我该怎么做???????
public class Audio_Record extends Activity { 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; private AudioRecord recorder = null; private Thread recordingThread = null; private boolean isRecording = false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setButtonHandlers(); enableButtons(false); int bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,RECORDER_CHANNELS,RECORDER_AUdio_ENCODING); System.out.println("BUFFER SIZE VALUE IS " + bufferSize); } private void setButtonHandlers() { ((Button) findViewById(R.id.btnStart)).setOnClickListener(btnClick); ((Button) findViewById(R.id.btnStop)).setOnClickListener(btnClick); } private void enableButton(int id,boolean isEnable) { ((Button) findViewById(id)).setEnabled(isEnable); } private void enableButtons(boolean isRecording) { enableButton(R.id.btnStart,!isRecording); enableButton(R.id.btnStop,isRecording); } int BufferElements2Rec = 1024; // want to play 2048 (2K) since 2 bytes we // use only 1024 int BytesPerElement = 2; // 2 bytes in 16bit format private void startRecording() { recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,RECORDER_SAMPLERATE,RECORDER_AUdio_ENCODING,BufferElements2Rec * BytesPerElement); recorder.startRecording(); isRecording = true; recordingThread = new Thread(new Runnable() { public void run() { writeAudioDataToFile(); } },"AudioRecorder Thread"); recordingThread.start(); } private byte[] short2byte(short[] sData) { int shortArrsize = sData.length; byte[] bytes = new byte[shortArrsize * 2]; for (int i = 0; i < shortArrsize; i++) { bytes[i * 2] = (byte) (sData[i] & 0x00FF); bytes[(i * 2) + 1] = (byte) (sData[i] >> 8); sData[i] = 0; } return bytes; } private void writeAudioDataToFile() { // Write the output audio in byte String filePath = "/sdcard/voice8K16bitmono.pcm"; short sData[] = new short[BufferElements2Rec]; FileOutputStream os = null; try { os = new FileOutputStream(filePath); } catch (FileNotFoundException e) { e.printStackTrace(); } while (isRecording) { // gets the voice output from microphone to byte format recorder.read(sData,BufferElements2Rec); System.out.println("Short wirting to file" + sData.toString()); try { // // writes the data to file from buffer // // stores the voice buffer byte bData[] = short2byte(sData); os.write(bData,BufferElements2Rec * BytesPerElement); } catch (IOException e) { e.printStackTrace(); } } try { os.close(); } catch (IOException e) { e.printStackTrace(); } } private void stopRecording() { // stops the recording activity if (null != recorder) { isRecording = false; recorder.stop(); recorder.release(); recorder = null; recordingThread = null; } } private View.OnClickListener btnClick = new View.OnClickListener() { public void onClick(View v) { switch (v.getId()) { case R.id.btnStart: { enableButtons(true); startRecording(); break; } case R.id.btnStop: { enableButtons(false); stopRecording(); break; } } } }; @Override public boolean onKeyDown(int keyCode,KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { finish(); } return super.onKeyDown(keyCode,event); } }
解决方法
Android的媒体播放器默认不播放PCM文件.或
>将其从SD卡复制到计算机并在那里播放
>使用AudioTrack编写自己的播放器
>安装播放PCM的应用程序
这是一个关于如何使用AudioTrack类播放PCM的教程:(http://jongladwin.blogspot.co.uk/2010/03/android-play-pcmwav-audio-buffer-using.html)
Windows Media Player应该能够播放PCM,这里提到了一些替代方案:(http://www.makeuseof.com/answers/play-pcm-file-pc/)
我猜Android上的大多数大型音乐播放器应用都支持PCM.