Skip to content
This repository has been archived by the owner on Jan 7, 2025. It is now read-only.

Commit

Permalink
remove lifecycle event on_start to enable using the client as library (
Browse files Browse the repository at this point in the history
  • Loading branch information
igorpolt authored Dec 29, 2022
1 parent 15b00f0 commit d6339f1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.speechly.client.speech

import android.Manifest
import android.app.Activity
import android.content.pm.PackageManager
import android.media.AudioFormat
import android.media.AudioRecord
import android.media.MediaRecorder
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import android.util.Log
import androidx.core.app.ActivityCompat
import androidx.core.app.ActivityCompat.requestPermissions
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
Expand All @@ -15,21 +17,14 @@ import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch

class AudioRecorder(var activity: AppCompatActivity, val sampleRate: Int) {
class AudioRecorder(var activity: Activity, val sampleRate: Int) {

val RECORD_AUDIO_REQUEST_CODE = 15001
private var recording = false
private var recorder: AudioRecord? = null
private val channelMask = AudioFormat.CHANNEL_IN_MONO
private val bufferSize = AudioRecord.getMinBufferSize(sampleRate, channelMask, AudioFormat.ENCODING_PCM_16BIT) * 4

private val requestPermissionLauncher = activity.registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean ->
if (isGranted) {
buildRecorder()
} else {
throw Exception("Need access to microphone")
}
}

fun buildRecorder() {
if (activity.checkSelfPermission(Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) {
recorder = AudioRecord.Builder()
Expand All @@ -42,7 +37,15 @@ class AudioRecorder(var activity: AppCompatActivity, val sampleRate: Int) {
.setBufferSizeInBytes(bufferSize)
.build()
} else {
requestPermissionLauncher.launch(Manifest.permission.RECORD_AUDIO)
requestPermissions(activity,
arrayOf(Manifest.permission.RECORD_AUDIO),
RECORD_AUDIO_REQUEST_CODE)
ActivityCompat.OnRequestPermissionsResultCallback { code, permissions, results ->
if (code == RECORD_AUDIO_REQUEST_CODE
&& results[0] == PackageManager.PERMISSION_GRANTED ) {
buildRecorder()
}
}
}
}

Expand Down
39 changes: 17 additions & 22 deletions android-client/src/main/kotlin/com/speechly/client/speech/Client.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.speechly.client.speech

import android.app.Activity
import android.content.Context
import android.media.AudioManager
import androidx.appcompat.app.AppCompatActivity
Expand Down Expand Up @@ -105,33 +106,27 @@ class Client (

companion object {
fun fromActivity(
activity: AppCompatActivity,
appId: UUID,
language: StreamConfig.LanguageCode = StreamConfig.LanguageCode.EN_US,
target: String = "api.speechly.com",
secure: Boolean = true
activity: Activity,
appId: UUID,
language: StreamConfig.LanguageCode = StreamConfig.LanguageCode.EN_US,
target: String = "api.speechly.com",
secure: Boolean = true
): Client {
val audioRecorder = AudioRecorder(activity, 16000)
val cachingIdProvider = CachingIdProvider()
val cachingIdentityService = CachingIdentityService.forTarget(target, secure)
activity.lifecycle.addObserver(object : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun connectListener() {
val cache = SharedPreferencesCache.fromContext(activity.getApplicationContext())
cachingIdProvider.cacheService = cache
cachingIdentityService.cacheService = cache

val audioManager = activity.getSystemService(Context.AUDIO_SERVICE) as AudioManager
if (audioManager.isBluetoothScoAvailableOffCall) {
if (!audioManager.isBluetoothScoOn) {
audioManager.startBluetoothSco()
}
} else {
println("SCO ist not available")
}
audioRecorder.buildRecorder()
val cache = SharedPreferencesCache.fromContext(activity.getApplicationContext())
cachingIdProvider.cacheService = cache
cachingIdentityService.cacheService = cache
val audioManager = activity.getSystemService(Context.AUDIO_SERVICE) as AudioManager
if (audioManager.isBluetoothScoAvailableOffCall) {
if (!audioManager.isBluetoothScoOn) {
audioManager.startBluetoothSco()
}
})
} else {
println("SCO ist not available")
}
audioRecorder.buildRecorder()

return Client(
appId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ val repoList = listOf(

class MainActivity : AppCompatActivity() {

private val speechlyClient: Client = Client.fromActivity(
activity = this,
appId = UUID.fromString("312137a2-1c8b-47f8-a553-b88b1a884e08")
)

private var speechlyClient: Client? = null
private var button: SpeechlyButton? = null
private var textView: TextView? = null
private var recyclerView: RecyclerView? = null
Expand Down Expand Up @@ -69,6 +65,10 @@ class MainActivity : AppCompatActivity() {

super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
speechlyClient = Client.fromActivity(
activity = this,
appId = UUID.fromString("312137a2-1c8b-47f8-a553-b88b1a884e08")
)
this.button = findViewById(R.id.speechly)
this.recyclerView = findViewById(R.id.recycler_view)
this.textView = findViewById(R.id.textView)
Expand Down

0 comments on commit d6339f1

Please sign in to comment.