Skip to content

Commit

Permalink
Merge pull request #81 from android/audio-comms
Browse files Browse the repository at this point in the history
Refactor audio sample for comms devices only
  • Loading branch information
lukehopkinsdev authored Aug 14, 2023
2 parents db298ed + ae9c9b8 commit 53247c1
Show file tree
Hide file tree
Showing 13 changed files with 444 additions and 830 deletions.
4 changes: 2 additions & 2 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

- [App Widgets](user-interface/appwidgets/src/main/java/com/example/platform/ui/appwidgets/AppWidgets.kt):
Showcases how to pin widget within the app. Check the launcher widget menu for all the app widgets samples
- [Audio Manager](connectivity/audio/src/main/java/com/example/platform/connectivity/audio/AudioSample.kt):
This sample will show you how get all audio sources and set an audio device. Covers Bluetooth, LEA, Wired and internal speakers
- [Call Notification Sample](connectivity/callnotification/src/main/java/com/example/platform/connectivity/callnotification/CallNotificationSample.kt):
Sample demonstrating how to make incoming call notifications and in call notifications
- [Camera Preview](camera/camera2/src/main/java/com/example/platform/camera/preview/Camera2Preview.kt):
Demonstrates displaying processed pixel data directly from the camera sensor
- [Color Contrast](accessibility/src/main/java/com/example/platform/accessibility/ColorContrast.kt):
This sample demonstrates the importance of proper color contrast and how to
- [Communication Audio Manager Sample](connectivity/audio/src/main/java/com/example/platform/connectivity/audio/AudioCommsSample.kt):
This sample shows how to use audio manager to for Communication application that self-manage the call.
- [Companion Device Manager Sample](connectivity/bluetooth/companion/src/main/java/com/example/platform/connectivity/bluetooth/cdm/CompanionDeviceManagerSample.kt):
This samples shows how to use the CDM to pair and connect with BLE devices
- [Connect to a GATT server](connectivity/bluetooth/ble/src/main/java/com/example/platform/connectivity/bluetooth/ble/ConnectGATTSample.kt):
Expand Down
11 changes: 7 additions & 4 deletions samples/connectivity/audio/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Audio samples
# Connectivity Audio samples

This module includes Audio Samples around Android Platform
This module includes Audio Samples related to connectivity:

- [Audio Manager](src/main/java/com/example/platform/connectivity/audio/AudioSample.kt)
- [Communication Audio Manager Sample](src/main/java/com/example/platform/connectivity/audio/AudioCommsSample.kt):
Shows how to use the audio manager to manage the communication device. For example during a VoIP call.
- [AudioLoopSource](src/main/java/com/example/platform/connectivity/audio/AudioLoopSource.kt):
Simple utility class that showcases how to use AudioTrack and AudioRecord to loop audio from the
input source to the output source.

For more check out the documentation at
https://developer.android.com/guide/topics/media
Expand All @@ -24,4 +28,3 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.platform.connectivity.audio

import android.Manifest
import android.media.AudioDeviceInfo
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.annotation.RequiresPermission
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ListItem
import androidx.compose.material3.ListItemDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import com.example.platform.base.PermissionBox
import com.google.android.catalog.framework.annotations.Sample
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


@Sample(
name = "Communication Audio Manager Sample",
description = "This sample shows how to use audio manager to for Communication application that self-manage the call.",
documentation = "https://developer.android.com/guide/topics/media-apps/media-apps-overview",
tags = ["audio"],
)
@RequiresApi(Build.VERSION_CODES.S)
@RequiresPermission(Manifest.permission.RECORD_AUDIO)
@Composable
fun AudioCommsSample() {
// The record permission is only needed for looping the audio not for the AudioManager
PermissionBox(permission = Manifest.permission.RECORD_AUDIO) {
AudioCommsScreen()
}
}

@OptIn(ExperimentalFoundationApi::class, ExperimentalAnimationApi::class)
@RequiresPermission(Manifest.permission.RECORD_AUDIO)
@RequiresApi(Build.VERSION_CODES.S)
@Composable
private fun AudioCommsScreen() {
val scope = rememberCoroutineScope()

// Get the current state of the communication audio devices. Check [AudioDeviceState].
val state = rememberAudioDeviceState()
val availableDevices = remember(state.availableDevices, state.activeDevice) {
// Remove the active device from our list
state.availableDevices.filter { it.id != state.activeDevice?.id }
}
var audioIssue by remember {
mutableStateOf("")
}

// Only for testing purposes: open an audio loop that takes the active audio device, records the
// audio and loops it back with a small delay.
LaunchedEffect(Unit) {
try {
AudioLoopSource.openAudioLoop()
} catch (e: Exception) {
audioIssue = e.message ?: "Unknown error with audio loop"
}
}

LazyColumn(
Modifier
.fillMaxSize()
.padding(vertical = 16.dp),
) {
stickyHeader {
ActiveDeviceItem(state.activeDevice) {
scope.launch {
// On click switch back to default device
state.clearSelectedDevice()
}
}
}

if (audioIssue.isNotBlank()) {
item {
Text(
text = audioIssue,
modifier = Modifier.background(MaterialTheme.colorScheme.errorContainer),
style = TextStyle(color = MaterialTheme.colorScheme.error),
)
}
}

items(items = availableDevices, key = { it.id }) {
var isLoading by remember {
mutableStateOf(false)
}
AudioDeviceItem(deviceInfo = it, isLoading = isLoading) {
isLoading = true
scope.launch(Dispatchers.IO) {
// On item selected, switch and wait to the new device
audioIssue = if (state.selectDevice(it)) {
""
} else {
"Error while selecting device"
}
isLoading = false
}
}
}
}
}

@RequiresApi(Build.VERSION_CODES.P)
@Composable
@OptIn(ExperimentalAnimationApi::class)
private fun ActiveDeviceItem(device: AudioDeviceInfo?, onClick: () -> Unit) {
Column(
Modifier
.fillMaxWidth()
.background(MaterialTheme.colorScheme.primaryContainer),
) {
Text(text = "Active device:", modifier = Modifier.padding(8.dp))
AnimatedContent(targetState = device, label = "Active device") {
if (it == null) {
Text(text = "None")
} else {
AudioDeviceItem(it) {
onClick()
}
}
}
}
}

@RequiresApi(Build.VERSION_CODES.P)
@Composable
private fun AudioDeviceItem(
deviceInfo: AudioDeviceInfo,
isLoading: Boolean = false,
onItemSelected: (AudioDeviceInfo) -> Unit,
) {
ListItem(
modifier = Modifier.clickable { onItemSelected(deviceInfo) },
headlineContent = {
Text(deviceInfo.productName.toString())
},
leadingContent = {
Text(deviceInfo.id.toString(), style = MaterialTheme.typography.headlineMedium)
},
trailingContent = {
if (isLoading) {
CircularProgressIndicator()
}
},
supportingContent = {
Text(
when (deviceInfo.type) {
AudioDeviceInfo.TYPE_BUILTIN_SPEAKER -> "Speaker"
AudioDeviceInfo.TYPE_BUILTIN_EARPIECE -> "Phone"
AudioDeviceInfo.TYPE_BLE_HEADSET -> "BLE Headset"
AudioDeviceInfo.TYPE_BLUETOOTH_SCO -> "BT SCO"
AudioDeviceInfo.TYPE_BLUETOOTH_A2DP -> "BT A2DP"
AudioDeviceInfo.TYPE_HEARING_AID -> "Hearing aid"
else -> "Type: ${deviceInfo.type}"
},
)
},
colors = ListItemDefaults.colors(containerColor = Color.Transparent),
)
}
Loading

0 comments on commit 53247c1

Please sign in to comment.