Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
tanzimfh committed Aug 7, 2024
1 parent eec9636 commit 8e6dafc
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 189 deletions.
1 change: 1 addition & 0 deletions vertexai/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ dependencies {
implementation("androidx.activity:activity-compose:1.9.1")
implementation("androidx.navigation:navigation-compose:2.7.7")

implementation("androidx.compose.runtime:runtime-livedata:1.3.0")
implementation(platform("androidx.compose:compose-bom:2024.06.00"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
Expand Down
2 changes: 2 additions & 0 deletions vertexai/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.RECORD_AUDIO"/>

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ val GenerativeViewModelFactory = object : ViewModelProvider.Factory {
// Initialize a GenerativeModel with the `gemini-flash` AI model
// for multimodal text generation
val generativeModel = Firebase.vertexAI.generativeModel(
modelName = "gemini-1.5-flash-preview-0514",
modelName = "gemini-1.5-pro-001",
generationConfig = config
)
PhotoReasoningViewModel(generativeModel)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.google.firebase.quickstart.vertexai.feature.multimodal

import android.content.ContentValues
import android.content.Context
import android.media.MediaRecorder
import android.os.Build
import android.os.Environment
import android.provider.MediaStore
import androidx.core.app.ActivityCompat
import android.Manifest
import android.content.pm.PackageManager
import java.io.File
import java.io.IOException

class AudioRecorder(private val context: Context) {
private var recorder: MediaRecorder? = null
private var outputFilePath: String? = null

fun startRecording() {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
println("Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED")
}
val audioFileName = "recording_${System.currentTimeMillis()}.m4a"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val values = ContentValues().apply {
put(MediaStore.Audio.Media.RELATIVE_PATH, Environment.DIRECTORY_MUSIC)
put(MediaStore.Audio.Media.TITLE, audioFileName)
put(MediaStore.Audio.Media.MIME_TYPE, "audio/mp4")
put(MediaStore.Audio.Media.IS_PENDING, 1)
}

val audioCollection = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
val audioUri = context.contentResolver.insert(audioCollection, values)
audioUri?.let { uri ->
outputFilePath = uri.toString()
context.contentResolver.openFileDescriptor(uri, "w")?.use { pfd ->
recorder = MediaRecorder(context).apply {
setAudioSource(MediaRecorder.AudioSource.MIC)
setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
setOutputFile(pfd.fileDescriptor)
try {
prepare()
} catch (e: IOException) {
e.printStackTrace()
}
start()
}
}
}
} else {
outputFilePath = "${context.getExternalFilesDir(Environment.DIRECTORY_MUSIC)}/$audioFileName"
recorder = MediaRecorder().apply {
setAudioSource(MediaRecorder.AudioSource.MIC)
setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
setOutputFile(outputFilePath)
try {
prepare()
} catch (e: IOException) {
e.printStackTrace()
}
start()
}
}
}

fun stopRecording(): ByteArray? {
recorder?.apply {
stop()
release()
}
recorder = null

outputFilePath?.let { path ->
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val uri = android.net.Uri.parse(path)
val values = ContentValues().apply {
put(MediaStore.Audio.Media.IS_PENDING, 0)
}
context.contentResolver.update(uri, values, null, null)
context.contentResolver.openInputStream(uri)?.readBytes()
} else {
File(path).readBytes()
}
}
return null
}
}
Loading

0 comments on commit 8e6dafc

Please sign in to comment.