Search in sources :

Example 1 with AudioDeviceInfo

use of android.media.AudioDeviceInfo in project vlc-android by videolan.

the class MediaPlayer method createAudioDeviceCallback.

@TargetApi(Build.VERSION_CODES.M)
private AudioDeviceCallback createAudioDeviceCallback() {
    return new AudioDeviceCallback() {

        private SparseArray<Long> mEncodedDevices = new SparseArray<>();

        private void onAudioDevicesChanged() {
            long encodingFlags = 0;
            for (int i = 0; i < mEncodedDevices.size(); ++i) encodingFlags |= mEncodedDevices.valueAt(i);
            updateAudioOutputDevice(encodingFlags, "pcm");
        }

        @RequiresApi(Build.VERSION_CODES.M)
        @Override
        public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
            for (AudioDeviceInfo info : addedDevices) {
                if (!info.isSink())
                    continue;
                long encodingFlags = getEncodingFlags(info.getEncodings());
                if (encodingFlags != 0)
                    mEncodedDevices.put(info.getId(), encodingFlags);
            }
            onAudioDevicesChanged();
        }

        @RequiresApi(Build.VERSION_CODES.M)
        @Override
        public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) {
            for (AudioDeviceInfo info : removedDevices) {
                if (!info.isSink())
                    continue;
                mEncodedDevices.remove(info.getId());
            }
            onAudioDevicesChanged();
        }
    };
}
Also used : SparseArray(android.util.SparseArray) AudioDeviceCallback(android.media.AudioDeviceCallback) AudioDeviceInfo(android.media.AudioDeviceInfo) TargetApi(android.annotation.TargetApi)

Example 2 with AudioDeviceInfo

use of android.media.AudioDeviceInfo in project sample-googleassistant by androidthings.

the class AssistantActivity method findAudioDevice.

private AudioDeviceInfo findAudioDevice(int deviceFlag, int deviceType) {
    AudioManager manager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
    AudioDeviceInfo[] adis = manager.getDevices(deviceFlag);
    for (AudioDeviceInfo adi : adis) {
        if (adi.getType() == deviceType) {
            return adi;
        }
    }
    return null;
}
Also used : AudioManager(android.media.AudioManager) AudioDeviceInfo(android.media.AudioDeviceInfo)

Example 3 with AudioDeviceInfo

use of android.media.AudioDeviceInfo in project sample-googleassistant by androidthings.

the class AssistantActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i(TAG, "starting assistant demo");
    setContentView(R.layout.activity_main);
    ListView assistantRequestsListView = findViewById(R.id.assistantRequestsListView);
    mAssistantRequestsAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mAssistantRequests);
    mMainHandler = new Handler(getMainLooper());
    assistantRequestsListView.setAdapter(mAssistantRequestsAdapter);
    mButtonWidget = findViewById(R.id.assistantQueryButton);
    mButtonWidget.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            mEmbeddedAssistant.startConversation();
        }
    });
    // Audio routing configuration: use default routing.
    AudioDeviceInfo audioInputDevice = null;
    AudioDeviceInfo audioOutputDevice = null;
    if (USE_VOICEHAT_I2S_DAC) {
        audioInputDevice = findAudioDevice(AudioManager.GET_DEVICES_INPUTS, AudioDeviceInfo.TYPE_BUS);
        if (audioInputDevice == null) {
            Log.e(TAG, "failed to find I2S audio input device, using default");
        }
        audioOutputDevice = findAudioDevice(AudioManager.GET_DEVICES_OUTPUTS, AudioDeviceInfo.TYPE_BUS);
        if (audioOutputDevice == null) {
            Log.e(TAG, "failed to found I2S audio output device, using default");
        }
    }
    try {
        if (USE_VOICEHAT_I2S_DAC) {
            Log.i(TAG, "initializing DAC trigger");
            mDac = VoiceHat.openDac();
            mDac.setSdMode(Max98357A.SD_MODE_SHUTDOWN);
            mButton = VoiceHat.openButton();
            mLed = VoiceHat.openLed();
        } else {
            PeripheralManager pioManager = PeripheralManager.getInstance();
            mButton = new Button(BoardDefaults.getGPIOForButton(), Button.LogicState.PRESSED_WHEN_LOW);
            mLed = pioManager.openGpio(BoardDefaults.getGPIOForLED());
        }
        mButton.setDebounceDelay(BUTTON_DEBOUNCE_DELAY_MS);
        mButton.setOnButtonEventListener(this);
        mLed.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
        mLed.setActiveType(Gpio.ACTIVE_HIGH);
    } catch (IOException e) {
        Log.e(TAG, "error configuring peripherals:", e);
        return;
    }
    // Set volume from preferences
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    int initVolume = preferences.getInt(PREF_CURRENT_VOLUME, DEFAULT_VOLUME);
    Log.i(TAG, "setting audio track volume to: " + initVolume);
    UserCredentials userCredentials = null;
    try {
        userCredentials = EmbeddedAssistant.generateCredentials(this, R.raw.credentials);
    } catch (IOException | JSONException e) {
        Log.e(TAG, "error getting user credentials", e);
    }
    mEmbeddedAssistant = new EmbeddedAssistant.Builder().setCredentials(userCredentials).setDeviceInstanceId(DEVICE_INSTANCE_ID).setDeviceModelId(DEVICE_MODEL_ID).setLanguageCode(LANGUAGE_CODE).setAudioInputDevice(audioInputDevice).setAudioOutputDevice(audioOutputDevice).setAudioSampleRate(SAMPLE_RATE).setAudioVolume(initVolume).setDeviceModelId(DEVICE_MODEL_ID).setDeviceInstanceId(DEVICE_INSTANCE_ID).setLanguageCode(LANGUAGE_CODE).setRequestCallback(new RequestCallback() {

        @Override
        public void onRequestStart() {
            Log.i(TAG, "starting assistant request, enable microphones");
            mButtonWidget.setText(R.string.button_listening);
            mButtonWidget.setEnabled(false);
        }

        @Override
        public void onSpeechRecognition(List<SpeechRecognitionResult> results) {
            for (final SpeechRecognitionResult result : results) {
                Log.i(TAG, "assistant request text: " + result.getTranscript() + " stability: " + Float.toString(result.getStability()));
                mAssistantRequestsAdapter.add(result.getTranscript());
            }
        }
    }).setConversationCallback(new ConversationCallback() {

        @Override
        public void onResponseStarted() {
            super.onResponseStarted();
            // When bus type is switched, the AudioManager needs to reset the stream volume
            if (mDac != null) {
                try {
                    mDac.setSdMode(Max98357A.SD_MODE_LEFT);
                } catch (IOException e) {
                    Log.e(TAG, "error enabling DAC", e);
                }
            }
        }

        @Override
        public void onResponseFinished() {
            super.onResponseFinished();
            if (mDac != null) {
                try {
                    mDac.setSdMode(Max98357A.SD_MODE_SHUTDOWN);
                } catch (IOException e) {
                    Log.e(TAG, "error disabling DAC", e);
                }
            }
            if (mLed != null) {
                try {
                    mLed.setValue(false);
                } catch (IOException e) {
                    Log.e(TAG, "cannot turn off LED", e);
                }
            }
        }

        @Override
        public void onError(Throwable throwable) {
            Log.e(TAG, "assist error: " + throwable.getMessage(), throwable);
        }

        @Override
        public void onVolumeChanged(int percentage) {
            Log.i(TAG, "assistant volume changed: " + percentage);
            // Update our shared preferences
            Editor editor = PreferenceManager.getDefaultSharedPreferences(AssistantActivity.this).edit();
            editor.putInt(PREF_CURRENT_VOLUME, percentage);
            editor.apply();
        }

        @Override
        public void onConversationFinished() {
            Log.i(TAG, "assistant conversation finished");
            mButtonWidget.setText(R.string.button_new_request);
            mButtonWidget.setEnabled(true);
        }

        @Override
        public void onAssistantResponse(final String response) {
            if (!response.isEmpty()) {
                mMainHandler.post(new Runnable() {

                    @Override
                    public void run() {
                        mAssistantRequestsAdapter.add("Google Assistant: " + response);
                    }
                });
            }
        }

        public void onDeviceAction(String intentName, JSONObject parameters) {
            if (parameters != null) {
                Log.d(TAG, "Get device action " + intentName + " with parameters: " + parameters.toString());
            } else {
                Log.d(TAG, "Get device action " + intentName + " with no paramete" + "rs");
            }
            if (intentName.equals("action.devices.commands.OnOff")) {
                try {
                    boolean turnOn = parameters.getBoolean("on");
                    mLed.setValue(turnOn);
                } catch (JSONException e) {
                    Log.e(TAG, "Cannot get value of command", e);
                } catch (IOException e) {
                    Log.e(TAG, "Cannot set value of LED", e);
                }
            }
        }
    }).build();
    mEmbeddedAssistant.connect();
}
Also used : SpeechRecognitionResult(com.google.assistant.embedded.v1alpha2.SpeechRecognitionResult) ListView(android.widget.ListView) Button(com.google.android.things.contrib.driver.button.Button) ArrayList(java.util.ArrayList) List(java.util.List) PeripheralManager(com.google.android.things.pio.PeripheralManager) AudioDeviceInfo(android.media.AudioDeviceInfo) SharedPreferences(android.content.SharedPreferences) Handler(android.os.Handler) JSONException(org.json.JSONException) ConversationCallback(com.example.androidthings.assistant.EmbeddedAssistant.ConversationCallback) IOException(java.io.IOException) View(android.view.View) ListView(android.widget.ListView) RequestCallback(com.example.androidthings.assistant.EmbeddedAssistant.RequestCallback) JSONObject(org.json.JSONObject) OnClickListener(android.view.View.OnClickListener) UserCredentials(com.google.auth.oauth2.UserCredentials) Editor(android.content.SharedPreferences.Editor)

Example 4 with AudioDeviceInfo

use of android.media.AudioDeviceInfo in project ring-client-android by savoirfairelinux.

the class HardwareServiceImpl method hasSpeakerphone.

private boolean hasSpeakerphone() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        // Check FEATURE_AUDIO_OUTPUT to guard against false positives.
        PackageManager packageManager = mContext.getPackageManager();
        if (!packageManager.hasSystemFeature(PackageManager.FEATURE_AUDIO_OUTPUT)) {
            return false;
        }
        AudioDeviceInfo[] devices = mAudioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
        for (AudioDeviceInfo device : devices) {
            if (device.getType() == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) {
                return true;
            }
        }
        return false;
    }
    return true;
}
Also used : PackageManager(android.content.pm.PackageManager) AudioDeviceInfo(android.media.AudioDeviceInfo)

Example 5 with AudioDeviceInfo

use of android.media.AudioDeviceInfo in project vlc-android by GeoffreyMetais.

the class MediaPlayer method createAudioDeviceCallback.

@TargetApi(Build.VERSION_CODES.M)
private AudioDeviceCallback createAudioDeviceCallback() {
    return new AudioDeviceCallback() {

        private SparseArray<Long> mEncodedDevices = new SparseArray<>();

        private void onAudioDevicesChanged() {
            long encodingFlags = 0;
            for (int i = 0; i < mEncodedDevices.size(); ++i) encodingFlags |= mEncodedDevices.valueAt(i);
            updateAudioOutputDevice(encodingFlags, "pcm");
        }

        @RequiresApi(Build.VERSION_CODES.M)
        @Override
        public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
            for (AudioDeviceInfo info : addedDevices) {
                if (!info.isSink())
                    continue;
                long encodingFlags = getEncodingFlags(info.getEncodings());
                if (encodingFlags != 0)
                    mEncodedDevices.put(info.getId(), encodingFlags);
            }
            onAudioDevicesChanged();
        }

        @RequiresApi(Build.VERSION_CODES.M)
        @Override
        public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) {
            for (AudioDeviceInfo info : removedDevices) {
                if (!info.isSink())
                    continue;
                mEncodedDevices.remove(info.getId());
            }
            onAudioDevicesChanged();
        }
    };
}
Also used : SparseArray(android.util.SparseArray) AudioDeviceCallback(android.media.AudioDeviceCallback) AudioDeviceInfo(android.media.AudioDeviceInfo) TargetApi(android.annotation.TargetApi)

Aggregations

AudioDeviceInfo (android.media.AudioDeviceInfo)5 TargetApi (android.annotation.TargetApi)2 AudioDeviceCallback (android.media.AudioDeviceCallback)2 SparseArray (android.util.SparseArray)2 SharedPreferences (android.content.SharedPreferences)1 Editor (android.content.SharedPreferences.Editor)1 PackageManager (android.content.pm.PackageManager)1 AudioManager (android.media.AudioManager)1 Handler (android.os.Handler)1 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 ListView (android.widget.ListView)1 ConversationCallback (com.example.androidthings.assistant.EmbeddedAssistant.ConversationCallback)1 RequestCallback (com.example.androidthings.assistant.EmbeddedAssistant.RequestCallback)1 Button (com.google.android.things.contrib.driver.button.Button)1 PeripheralManager (com.google.android.things.pio.PeripheralManager)1 SpeechRecognitionResult (com.google.assistant.embedded.v1alpha2.SpeechRecognitionResult)1 UserCredentials (com.google.auth.oauth2.UserCredentials)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1