2017-08-29 34 views
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)

我可以得到任何幫助嗎?

回答

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。您正在使用的版本在最新版本中已被棄用。

相關問題