Configuring Audio, Video Input and Output devices - Android 5.x
Configure audio and video input and output devices from your Twilio Video Rooms API application to build a better end-user experience. This guide covers selecting camera and audio inputs and routing audio output for the Android 5.x SDK.
The CameraCapturer provides video frames via the Camera API for a LocalVideoTrack from a given CameraCapturer.CameraSource.
1// Share your camera2CameraCapturer cameraCapturer = new CameraCapturer(context, CameraSource.FRONT_CAMERA);3LocalVideoTrack localVideoTrack = LocalVideoTrack.create(context, true, cameraCapturer);4VideoView primaryVideoView = (VideoView) findViewById(R.id.local_video);56// Mirror front camera7primaryVideoView.setMirror(true);89// Render camera to view10localVideoTrack.addRenderer(primaryVideoView);1112// Switch the camera source13CameraSource cameraSource = cameraCapturer.getCameraSource();14cameraCapturer.switchCamera();15primaryVideoView.setMirror(cameraSource == CameraSource.BACK_CAMERA);
The Camera2Capturer provides video frames via the Camera 2 API for a LocalVideoTrack from a given camera ID.
1// Get the device camera IDs2CameraManager cameraManager =3(CameraManager) cameraCapturerActivity.getSystemService(Context.CAMERA_SERVICE);4String[] cameraIds = cameraManager.getCameraIdList();56// This example uses the first Camera ID but applications can make their own decisions7// about which Camera ID to use8String cameraId = cameraIds[0];910// Share your camera11Camera2Capturer camera2Capturer = new Camera2Capturer(context, cameraId, camera2Listener);12LocalVideoTrack localVideoTrack = LocalVideoTrack.create(context, true, camera2Capturer);1314// Render camera to a view15VideoView primaryVideoView = (VideoView) findViewById(R.id.local_video);16localVideoTrack.addRenderer(primaryVideoView);1718// Switch the camera source from the first camera ID to the second camera ID19camera2Capturer.switchCamera(cameraIds[1]);
For more details and other best practices capturing video from the camera capturers, please reference the Video Android Quickstart.
The Twilio Video Android SDK does not officially support switching the AudioSource and just defaults to the native Android API MediaRecorder.AudioSource.VOICE_COMMUNICATION
Using the AudioManager class provided by Android, you can specify if audio is routed through the headset or speaker.
1// Get AudioManager2AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);34// Route audio through speaker5audioManager.setSpeakerphoneOn(true);67// Route audio through headset8audioManager.setSpeakerphoneOn(false);