I2S1 error: Cannot read from output-only device (Operation not permitted) (code 1)

I2S1 error: Cannot read from output-only device (Operation not permitted) (code 1)

我有点新手 Android 我在工作

  1. 主要在 i2s adafruit 麦克风上
  2. 也适用于典型的 USB 麦克风 在 Raspberry pi.
  3. 上有 Android 东西

Android 文档说它从预览版 2 开始支持 USB 麦克风,但我找不到任何示例。

https://developer.android.com/things/preview/releases.html

所以我现在使用 i2s 麦克风并卡在此处。

代码

// I2S Device Name
private static final String I2S_DEVICE_NAME = "I2S1";

private static final AudioFormat AUDIO_FORMAT_STEREO =
        new AudioFormat.Builder()
                .setChannelMask(AudioFormat.CHANNEL_IN_STEREO)
                .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
                .setSampleRate(44100)
                .build();

private I2sDevice mDevice;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String str = "";

    // Attempt to access the I2C device
    try {
        PeripheralManagerService manager = new PeripheralManagerService();
        mDevice = manager.openI2sDevice(I2S_DEVICE_NAME, AUDIO_FORMAT_STEREO, I2sDevice.PCM_FORMAT_16_BIT);
    } catch (IOException e) {
        Log.w(TAG, "Unable to access I2S device", e);
    }

    // Set up the audio playback sink
    int bufferSize = AudioTrack.getMinBufferSize(
            AUDIO_FORMAT_STEREO.getSampleRate(),
            AUDIO_FORMAT_STEREO.getChannelMask(),
            AUDIO_FORMAT_STEREO.getEncoding());

    str += String.valueOf(bufferSize) + "    ";

    // Transfer data from input to output
    ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
    try{
        int read = mDevice.read(buffer, bufferSize);
        str += String.valueOf(read);
    } catch (IOException e) {
        Log.w(TAG, "Unable to access I2S1 device", e);
    }
    TextView myText = (TextView) findViewById(R.id.mytextview);

    myText.setText(str);
}

问题

行:

mDevice.read()

android班长说

I2S1 error: Cannot read from output-only device (Operation not permitted) (code 1)

我能得到什么帮助吗?

Android documentation says it supports USB mic since Preview 2, but I couldn't find any example.

自动检测 USB 麦克风并将其设置为设备上的默认麦克风输入。您可以参考将音频源设置为 MIC. As one example, here is the API Guide for MediaRecorder.

的任何标准 Android 录音示例

I2S1 error: Cannot read from output-only device (Operation not permitted) (code 1)

您在代码中使用的 Android Things 支持库是什么版本?如果您不是最新版本(OS 图像和库均为 0.5.1),我建议您先进行更新。您也可以尝试更改代码以使用 openI2sDevice()accepts direction flags 版本。您使用的版本已在最新版本中弃用。