I'm kind of new to Android I'm working
Android documentation says it supports USB mic since Preview 2, but I couldn't find any example.
https://developer.android.com/things/preview/releases.html
So I'm on i2s microphone for now and stuck here.
Code
// 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);
}
Problem
At line:
mDevice.read()
android monitor says
I2S1 error: Cannot read from output-only device (Operation not permitted) (code 1)
Can I get any help?
Android documentation says it supports USB mic since Preview 2, but I couldn't find any example.
A USB microphone is automatically detected and set up as the default mic input on the device. You can reference any standard Android audio recording sample that sets the audio source to MIC. As one example, here is the API Guide for MediaRecorder.
I2S1 error: Cannot read from output-only device (Operation not permitted) (code 1)
What version of the Android Things support library are you using in your code? If you aren't on the latest (0.5.1 for both the OS image and the library) I would recommend updating first. You might also try changing your code to use the version of openI2sDevice()
that accepts direction flags. The version you are using has been deprecated in the latest releases.