I2S1错误:不能只从输出设备读取(不允许的操作)(代码1)

问题描述:

我有点新到Android我工作I2S1错误:不能只从输出设备读取(不允许的操作)(代码1)

  1. 大多I2S Adafruit的麦克风
  2. 也典型的USB麦克风 与Raspberry Pi上的Android事物。

Android文档称自从Preview 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的标准Android音频录音示例。举一个例子,这里是API Guide for MediaRecorder

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

您在代码中使用了哪种版本的Android Things支持库?如果你不是最新的(操作系统映像和库都是0.5.1),我会建议先更新。您也可以尝试更改您的代码以使用openI2sDevice()的版本accepts direction flags。您正在使用的版本在最新版本中已被弃用。