0

我使用以下代码记录来自麦克风的数据,然后播放。我了解到缓冲区大小必须匹配。问题是录制后什么都没有播放。相反,我在日志上收到如下消息:

10-07 00:12:09.187:WARN/AudioTrack(3719):obtainBuffer() 轨道 0x1df1a8 已禁用,正在重新启动 10-07 00:12:10.351:WARN/AudioTrack(3719):obtainBuffer() 轨道 0x1df1a8 已禁用,正在重新启动

这是代码。我做错了什么?

  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.w("DEBUG", "Audio record");

    int frequency = 44100;
    int channelConfiguration = AudioFormat.CHANNEL_IN_STEREO;
    int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
    AudioRecord audioRecord = null;


    final int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);

    audioPlayer = new AudioTrack(AudioManager.STREAM_MUSIC, frequency, AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM);


    Log.w("DEBUG", "Buffer size: " + bufferSize);
    //capture data and record to file
    int readBytes = 0, writtenBytes = 0;


    try
    {

      audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
              frequency, channelConfiguration,
              audioEncoding, bufferSize);
      // audioRecord = findAudioRecord();

      short[] buffer = new short[bufferSize];
      byte[] data = new byte[bufferSize];
      if (audioRecord == null || (audioRecord != null && audioRecord.getState() != AudioRecord.STATE_INITIALIZED))
      {
        Log.w("DEBUG", "Can't start");
        //Log.w("DEBUG", "status: " + audioRecord.getState());
        return;
      }

      audioRecord.startRecording();


      int sampleNumber = 0;


      // while (sampleNumber < 30)
      {
        readBytes = audioRecord.read(buffer, 0, bufferSize);
        if (AudioRecord.ERROR_INVALID_OPERATION != readBytes)
        {
          Log.w("DEBUG", "Writing");
          writtenBytes += audioPlayer.write(data, 0, readBytes);
        }
        Log.w("DEBUG", "Sample number" + sampleNumber);


        sampleNumber++;
      }
      if (audioPlayer.getPlayState() != AudioTrack.PLAYSTATE_PLAYING)
      {
        Log.w("DEBUG", "Playing");
        audioPlayer.play();
      }

    } catch (IllegalArgumentException e)
    {
      e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    } catch (IllegalStateException e)
    {
      e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    } finally
    {
      if (audioRecord != null && audioRecord.getState() == AudioRecord.STATE_INITIALIZED)
      {
        audioRecord.stop();
        audioRecord.release();
      }

    }
  }
4

1 回答 1

1

电话的音量不够高,而且我离麦克风说话的距离也不够近 :) 回声仍然是个问题,但至少我终于能听到声音了。

于 2013-10-10T19:24:16.250 回答