Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions xr/src/main/java/com/example/xr/projected/ProjectedHardware.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ import androidx.camera.core.resolutionselector.ResolutionSelector
import androidx.camera.core.resolutionselector.ResolutionStrategy
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.getSystemService
import androidx.xr.projected.ProjectedContext
import androidx.xr.projected.experimental.ExperimentalProjectedApi
import java.io.File

private const val TAG = "ProjectedHardware"

Expand Down Expand Up @@ -199,13 +199,12 @@ private fun startBluetoothAudioRecording(context: Context) {
audioRecord.release()
}
}

/**
* Demonstrates how to record audio using the projected device context.
*/
@RequiresPermission(Manifest.permission.RECORD_AUDIO)
@OptIn(ExperimentalProjectedApi::class)
private fun startProjectedAudioRecording(context: Context) {
private fun startProjectedAudioRecording(context: Context, outputFile: File) {
val projectedDeviceContext = try {
ProjectedContext.createProjectedDeviceContext(context)
} catch (e: IllegalStateException) {
Expand All @@ -214,19 +213,22 @@ private fun startProjectedAudioRecording(context: Context) {
}

// [START androidxr_projected_context_audio_record]
// Initialize AudioRecord with projected device context
val audioRecord = AudioRecord.Builder()
.setAudioSource(MediaRecorder.AudioSource.CAMCORDER)
.setAudioFormat(audioFormat)
.setBufferSizeInBytes(bufferSize)
// pass in the projected device context
.setContext(projectedDeviceContext)
.build()

audioRecord.startRecording()
// [END androidxr_projected_context_audio_record]
// Initialize MediaRecorder with the projected device context
val recorder = MediaRecorder(projectedDeviceContext).apply {
setAudioSource(MediaRecorder.AudioSource.CAMCORDER)
setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
setAudioChannels(1)
setAudioSamplingRate(16000)
setAudioEncodingBitRate(32000)
setOutputFile(outputFile)

prepare()
start()
// [END androidxr_projected_context_audio_record]
}
Comment thread
devbridie marked this conversation as resolved.

// Stop and release when done.
audioRecord.stop()
audioRecord.release()
recorder.stop()
recorder.release()
}
Loading