1

我正在尝试访问 Android 手机上的麦克风并检索原始数据(不写入文件)。但是在我将原始数据写入文件后,我无法播放我录制的音频(使用 Matlab 重播)。我想知道是否需要调整任何参数或过程有问题?非常感谢。

     AudioManager audio_service = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        audio_service.setSpeakerphoneOn(true);
        audio_service.setMode(AudioManager.MODE_IN_CALL);
        audio_service.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL);

        int iSampleRate = AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_SYSTEM);
        int iAudioBufferSize = AudioRecord.getMinBufferSize(iSampleRate, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT);
         recorder = new AudioRecord(AudioSource.CAMCORDER, iSampleRate, AudioFormat.CHANNEL_IN_STEREO, AudioFormat.ENCODING_PCM_16BIT, iAudioBufferSize);
         recorder.startRecording();
         bRecording = true;
         recordThread.start();



     public void run()
            {
                    short[] buffer = new short[iAudioBufferSize];
                android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);

            while(!interrupted())
            {
                // Make sure the audiorecorder get the enough data
                ReadFully(buffer, 0, iAudioBufferSize,recorder);                  

                try{                 
                    for (int i = 0; i < buffer.length-1; i++)
                    {                      
                            if(rawAudioWriter1 != null){
                                rawAudioWriter1.append(Short.toString(buffer[i]));
                                rawAudioWriter1.append('\n');                            
                            }                                                                               
                    }
                    if(rawAudioWriter1 !=null){                     
                        rawAudioWriter1.flush();
                    }


                }catch(IOException e){
                    e.printStackTrace();
                }
}

        void readFully(short[] data, int off, int length, AudioRecord mrecorder) {
            int read;
            while (length > 0) {
                read = mrecorder.read(data, off, length);
                length -= read;
                off += read;
            }
        }
4

0 回答 0