IllegalStateException: Unable to retrieve AudioTrack pointer for write() - android

I am using a third-party text-to-speech tool to make my app ADA compliant. I am using this service to speak text every time text needs to be dictated:
class TTSService : Service() {
private val ttsUtils = TTSUtils.getInstance(this)
private val voiceText = ttsUtils.voiceText
private lateinit var audioTrack:AudioTrack
override fun onBind(intent: Intent?): IBinder? {
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
audioTrack = AudioTrack(
AudioManager.USE_DEFAULT_STREAM_TYPE,
IVTDefine.SAMPLE_RATE_16000,
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT,
71296,
AudioTrack.MODE_STREAM
)
if (ttsUtils.isEnabled) {
ttsUtils.getEngineVoice()
if (null == voiceText) {
return START_STICKY
}
audioTrack.play()
try {
Thread(Runnable {
voiceText.vtapiTextToBuffer(intent?.getStringExtra("TextToSpeak"), //Library method that turns text string into audioTrack of
object : VoiceTextListener {
override fun onReadBufferWithWordInfo(output: ByteArray?, outputSize: Int, wordInfo: MutableList<SyncWordInfo>?) {
}
override fun onReadBuffer(output: ByteArray?, outputSize: Int) {
if (outputSize > 0) {
val audioData = ByteBuffer.wrap(output)
audioTrack.write(audioData, audioData.remaining(), AudioTrack.WRITE_BLOCKING)
}
}
override fun onReadBufferWithMarkInfo(output: ByteArray?, outputSize: Int, markInfo: MutableList<SyncMarkInfo>?) {
}
override fun onError(error: String?) {
Log.e(TAG, "Error: $error")
}
})
}).start()
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
}
return START_REDELIVER_INTENT
}
override fun onDestroy() {
super.onDestroy()
voiceText.vtapiStopBuffer()
if (audioTrack.playState == AudioTrack.PLAYSTATE_PLAYING) {
try {
audioTrack.pause()
} catch (e: Exception) {
AppLog.e(TAG, e.message!!)
}
}
try {
audioTrack.flush()
audioTrack.release()
} catch (e: Exception) {
AppLog.e(TAG, e.message!!)
}
}
}
The service is always started with this method in another class:
fun speakText(textToSpeak: String) {
val i = Intent(mContext, TTSService::class.java)
i.putExtra("TextToSpeak", textToSpeak)
mContext.stopService(i)
mContext.startService(i)
}
I'm getting the above error when audioTrack.write() is called in the service's runnable, but I don't know what the error means. How do I make sure it's retrieving the pointer?

Related

Android: Kotlin getting data from callback and emit with flow

Everyone, I have this method and want to emit listmessage but list is unable to get the list of values, How can I do it?
fun ReadMessage(dialog: QBChatDialog) = flow{
try {
var list: ArrayList<QBChatMessage> = ArrayList()
chatHelper.ReadChatHistory(dialog, object : QBEntityCallback<ArrayList<QBChatMessage>>{
override fun onSuccess(listmessage: ArrayList<QBChatMessage>?, p1: Bundle?) {
Log.e(TAG, "Reading Message: $p0")
}
override fun onError(p0: QBResponseException?) {
Log.e(TAG, "Reading Message Exception: $p0")
}
})
Log.e(TAG, "Reading Messageeeeeeeeee: $list")
emit(list)
}catch (e: Exception){
Log.e(TAG, "Reading Message Exceptionn: $e")
}
}
You should use CompletableDeferred. You can do something like this:
fun readMessage(dialog: QBChatDialog): Flow<ArrayList<QBChatMessage>> {
val historyDeferred = CompletableDeferred<ArrayList<QBChatMessage>>()
chatHelper.ReadChatHistory(dialog, object : QBEntityCallback<ArrayList<QBChatMessage>> {
override fun onSuccess(listmessage: ArrayList<QBChatMessage>?, p1: Bundle?) {
historyDeferred.complete(listmessage ?: arrayListOf())
}
override fun onError(p0: QBResponseException?) {
historyDeferred.completeExceptionally(p0 ?: CancellationException())
}
})
return flow {
try {
emit(historyDeferred.await())
} catch (e: Exception) {
Log.e(TAG, "Reading Message Exceptionn: $e")
}
}
}

Nested Callback function with coroutines

I want to get a response from callback function async/await style of javascript using kotlin coroutines.
Here is my callback functions
offlineCatalog.findOfflineVideoById(id, object : OfflineCallback<Video> {
override fun onSuccess(video: Video?) {
video?.let {
//Return This Video
} ?: kotlin.run {
findVideoOnline(id, state)
}
}
override fun onFailure(throwable: Throwable?) {
findVideoOnline(id, state)
}
})
onlineCatalog.findVideoByID(id, object : VideoListener() {
override fun onVideo(video: Video?) {
video?.let {
//Return This Video
} ?: kotlin.run {
Log.e("Return Error")
}
}
override fun onError(errors: MutableList<CatalogError>) {
super.onError(errors)
Log.e("Return Error")
}
})
I want to call function that will return video object from OfflineCatalog if error in OfflineCatalog then search from OnlineCatalog.
such as
try{
val video:Video? = getVideo(id:String)
//do something
}catch(throwable:Throwable){
Log.e("Video not found")
}
Update: My Implementation
this is what I came up with
suspend fun getVideo(id: String): Video? = withContext(Dispatchers.IO) {
var video = getVideoOffline(id)
video?.let { video } ?: kotlin.run { getVideoOnline(id) }
}
suspend fun getVideoOffline(id: String): Video? = suspendCancellableCoroutine { cont ->
(offlineCatalog.findOfflineVideoById(id, object : OfflineCallback<Video> {
override fun onSuccess(video: Video?) = cont.resume(video)
override fun onFailure(throwable: Throwable?) = cont.resume(null)
}))
}
suspend fun getVideoOnline(id: String): Video? = suspendCancellableCoroutine { cont ->
catalog.findVideoByID(id, object : VideoListener() {
override fun onVideo(video: Video?) = cont.resume(video)
override fun onError(errors: MutableList<CatalogError>) = cont.resume(null)
})
}
Usage-
CoroutineScope(Dispatchers.Main).launch {
getVideo(id)?.let {
//Do Something
} ?: kotlin.run{
//Video Not Found
}
}
you have to do something like this
#ExperimentalCoroutinesApi
suspend fun getVideo(id: String): Video? = coroutineScope {
val offlineVideo: Video? = suspendCancellableCoroutine { cont ->
offlineCatalog.findOfflineVideoById(id, object : OfflineCallback<Video> {
override fun onSuccess(video: Video?) {
cont.resume(video)
}
override fun onFailure(throwable: Throwable?) {
cont.resume(null)
}
})
}
offlineVideo ?: suspendCancellableCoroutine { cont ->
// offlineVideo not found so search from onlineCatalog
onlineCatalog.findVideoByID(id, object : VideoListener() {
override fun onVideo(video: Video?) {
cont.resume(video)
}
override fun onError(errors: MutableList<CatalogError>) {
super.onError(errors)
cont.resumeWithException(someException)
}
})
}
}
then you can call it as you wanted
someScope.launch {
try {
val video: Video? = getVideo(id)
//do something
} catch (throwable: Throwable) {
Log.e("Video not found")
}
}
Read more about suspendCancellableCoroutine here

How to play background music through all activities using Kotlin?

How can I play a music file in the background through all activities by using Kotlin? I have looked everywhere but I only found solutions for Java not for Kotlin.
I tried using this function but I don't know how to use it:
fun playSound() {
try {
if (m.isPlaying()) {
m.stop()
m.release()
//m = MediaPlayer()
}
val descriptor = assets.openFd("backgroundsound1.mp3")
m.setDataSource(descriptor.fileDescriptor, descriptor.startOffset, descriptor.length)
descriptor.close()
m.prepare()
m.setVolume(1f, 1f)
m.setLooping(true)
m.start()
} catch (e: Exception) {
e.printStackTrace()
}
}
Here' code in kotlin code Play background music in all activities
class BackgroundSoundService : Service() {
internal lateinit var player: MediaPlayer
override fun onBind(arg0: Intent): IBinder? {
return null
}
override fun onCreate() {
super.onCreate()
val afd = applicationContext.assets.openFd("backgroundsound1.wav") as AssetFileDescriptor
val player = MediaPlayer()
player.setDataSource(afd.fileDescriptor)
player.isLooping = true // Set looping
player.setVolume(100f, 100f)
}
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
player.start()
return 1
}
override fun onStart(intent: Intent, startId: Int) {
// TO DO
}
fun onUnBind(arg0: Intent): IBinder? {
// TO DO Auto-generated method
return null
}
fun onStop() {
}
fun onPause() {
}
override fun onDestroy() {
player.stop()
player.release()
}
override fun onLowMemory() {
}
companion object {
private val TAG: String? = null
}
}

BoundaryCallback Not Called With DataSource

I created a boundary callback and a data source, the data source reads from the DB and the boundary callback is supposed to make the API calls and persist to the DB.
However, the overridden methods in boundary callback are never called, I don't know if there is a rule that says you can't mix both. But it is driving me crazy and I don't get it.
private val postsDataFactory = object: DataSource.Factory<String, Post>() {
override fun create(): DataSource<String, Post> {
return ItemDataSource(application)
}
}
private val postsBoundaryCallback = object: PagedList.BoundaryCallback<Post>(){
override fun onZeroItemsLoaded() {
super.onZeroItemsLoaded()
viewModelScope.launch {
try {
val posts = postsRepo.findPosts(mutableMapOf("limit" to "1"))
database.postDao().create(posts)
} catch (e: IOException) {
Log.d("NETWORK_ERROR", e.message)
} catch (e: Exception) {
Log.d("UNCAUGHT_ERROR", e.message)
}
}
}
override fun onItemAtEndLoaded(itemAtEnd: Post) {
viewModelScope.launch {
try {
val posts = postsRepo.findPosts(mutableMapOf(
"before" to itemAtEnd.id,
"limit" to "1"
))
database.postDao().create(posts)
} catch (e: IOException) {
Log.d("NETWORK_ERROR", e.message)
} catch (e: Exception) {
Log.d("UNCAUGHT_ERROR", e.message)
}
}
}
}
private val postPageConfig = PagedList
.Config
.Builder()
.setPageSize(10)
.setInitialLoadSizeHint(10)
.build()
val postsLiveData = LivePagedListBuilder(
postsDataFactory,
postPageConfig
)
.setBoundaryCallback(postsBoundaryCallback)
.build()

Android NsdManager not able to discover services

I'm running into a problem with Androids NsdManager when following their tutorial Using Network Service Discovery.
I have a few zeroconf/bonjour hardware devices on my network. From my mac I can discover all of them as expected from my terminal with the following
dns-sd -Z _my-mesh._tcp.
From my Android app's first run I can flawlessly discover these services using NsdManager. However if I restart the application and try again none of the services are found. onDiscoveryStarted gets called successfully but then nothing else after. While waiting I can confirm from my mac that the services are still successfully there.
I can then turn on my Zeroconf app (on Android) and it will show the services like my mac. When I return to my app I see it immediately receive all the callbacks I expected previously. So I believe something is wrong with my approach, however I'm not sure what. Below is the code I use to discover and resolve services. The view is a giant textview (in a scroll view) I keep writing text to for debugging easier.
import android.annotation.SuppressLint
import android.content.Context
import android.net.nsd.NsdManager
import android.net.nsd.NsdServiceInfo
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
class MainActivity : AppCompatActivity(),
NsdManager.DiscoveryListener {
private var nsdManager: NsdManager? = null
private var text: TextView? = null
private var isResolving = false
private val services = ArrayList<ServiceWrapper>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
this.text = findViewById(R.id.text)
this.nsdManager = application.getSystemService(Context.NSD_SERVICE) as NsdManager
}
override fun onResume() {
super.onResume()
this.nsdManager?.discoverServices("_my-mesh._tcp.", NsdManager.PROTOCOL_DNS_SD, this)
write("Resume Discovering Services")
}
override fun onPause() {
super.onPause()
this.nsdManager?.stopServiceDiscovery(this)
write("Pause Discovering Services")
}
override fun onServiceFound(serviceInfo: NsdServiceInfo?) {
write("onServiceFound(serviceInfo = $serviceInfo))")
if (serviceInfo == null) {
return
}
add(serviceInfo)
}
override fun onStopDiscoveryFailed(serviceType: String?, errorCode: Int) {
write("onStopDiscoveryFailed(serviceType = $serviceType, errorCode = $errorCode)")
}
override fun onStartDiscoveryFailed(serviceType: String?, errorCode: Int) {
write("onStartDiscoveryFailed(serviceType = $serviceType, errorCode = $errorCode)")
}
override fun onDiscoveryStarted(serviceType: String?) {
write("onDiscoveryStarted(serviceType = $serviceType)")
}
override fun onDiscoveryStopped(serviceType: String?) {
write("onDiscoveryStopped(serviceType = $serviceType)")
}
override fun onServiceLost(serviceInfo: NsdServiceInfo?) {
write("onServiceLost(serviceInfo = $serviceInfo)")
}
private fun createResolveListener(): NsdManager.ResolveListener {
return object : NsdManager.ResolveListener {
override fun onResolveFailed(serviceInfo: NsdServiceInfo?, errorCode: Int) {
write("onResolveFailed(serviceInfo = $serviceInfo, errorCode = $errorCode)")
isResolving = false
resolveNext()
}
override fun onServiceResolved(serviceInfo: NsdServiceInfo?) {
write("onServiceResolved(serviceInfo = $serviceInfo)")
if (serviceInfo == null) {
return
}
for (servicewrapper in services) {
if (servicewrapper.serviceInfo.serviceName == serviceInfo.serviceName) {
servicewrapper.resolve(serviceInfo)
}
}
isResolving = false
resolveNext()
}
}
}
#SuppressLint("SetTextI18n")
private fun write(text: String?) {
this.text?.let {
it.post({
it.text = it.text.toString() + "\n" + text + "\n"
})
}
}
fun add(serviceInfo: NsdServiceInfo) {
for (servicewrapper in services) {
if (servicewrapper.serviceInfo.serviceName == serviceInfo.serviceName) {
return
}
}
services.add(ServiceWrapper(serviceInfo))
resolveNext()
}
#Synchronized
fun resolveNext() {
if (isResolving) {
return
}
isResolving = true
for (servicewrapper in services) {
if (servicewrapper.isResolved) {
continue
}
write("resolving")
this.nsdManager?.resolveService(servicewrapper.serviceInfo, createResolveListener())
return
}
isResolving = false
}
inner class ServiceWrapper(var serviceInfo: NsdServiceInfo) {
var isResolved = false
fun resolve(serviceInfo: NsdServiceInfo) {
isResolved = true
this.serviceInfo = serviceInfo
}
}
}
Better late than never. Did not realize other people were having this issue too until now.
What we discovered was some routers were blocking or not correctly forwarding the packets back and forth. Our solution to this was using wire shark to detect what other popular apps were doing to get around the issue. Androids NsdManager has limited customizability so it required manually transmitting the packet over a MulticastSocket.
interface NsdDiscovery {
suspend fun startDiscovery()
suspend fun stopDiscovery()
fun setListener(listener: Listener?)
fun isDiscovering(): Boolean
interface Listener {
fun onServiceFound(ip:String, local:String)
fun onServiceLost(event: ServiceEvent)
}
}
#Singleton
class ManualNsdDiscovery #Inject constructor()
: NsdDiscovery {
//region Fields
private val isDiscovering = AtomicBoolean(false)
private var socketManager: SocketManager? = null
private var listener: WeakReference<NsdDiscovery.Listener> = WeakReference<NsdDiscovery.Listener>(null)
//endregion
//region NsdDiscovery
override suspend fun startDiscovery() = withContext(Dispatchers.IO) {
if (isDiscovering()) return#withContext
this#ManualNsdDiscovery.isDiscovering.set(true)
val socketManager = SocketManager()
socketManager.start()
this#ManualNsdDiscovery.socketManager = socketManager
}
override suspend fun stopDiscovery() = withContext(Dispatchers.IO) {
if (!isDiscovering()) return#withContext
this#ManualNsdDiscovery.socketManager?.stop()
this#ManualNsdDiscovery.socketManager = null
this#ManualNsdDiscovery.isDiscovering.set(false)
}
override fun setListener(listener: NsdDiscovery.Listener?) {
this.listener = WeakReference<NsdDiscovery.Listener>(listener)
}
#Synchronized
override fun isDiscovering(): Boolean {
return this.isDiscovering.get()
}
//endregion
private inner class SocketManager {
//region Fields
private val group = InetAddress.getByName("224.0.0.251")
?: throw IllegalStateException("Can't setup group")
private val incomingNsd = IncomingNsd()
private val outgoingNsd = OutgoingNsd()
//endregion
//region Constructors
//endregion
//region Methods
suspend fun start() {
this.incomingNsd.startListening()
this.outgoingNsd.send()
}
fun stop() {
this.incomingNsd.stopListening()
}
//endregion
private inner class OutgoingNsd {
//region Fields
private val socketMutex = Mutex()
private var socket = MulticastSocket(5353)
suspend fun setUpSocket() {
this.socketMutex.withLock {
try {
this.socket = MulticastSocket(5353)
this.socket.reuseAddress = true
this.socket.joinGroup(group)
} catch (e: SocketException) {
return
}
}
}
suspend fun tearDownSocket() {
this.socketMutex.withLock {
this#OutgoingNsd.socket.close()
}
}
//ugly code but here is the packet
private val bytes = byteArrayOf(171.toByte(), 205.toByte(), 1.toByte(), 32.toByte(),
0.toByte(), 1.toByte(), 0.toByte(), 0.toByte(),
0.toByte(), 0.toByte(), 0.toByte(), 0.toByte(),
9.toByte(), 95.toByte(), 101.toByte(), 118.toByte(),
97.toByte(), 45.toByte(), 109.toByte(), 101.toByte(),
115.toByte(), 104.toByte(), 4.toByte(), 95.toByte(),
116.toByte(), 99.toByte(), 112.toByte(), 5.toByte(),
108.toByte(), 111.toByte(), 99.toByte(), 97.toByte(),
108.toByte(), 0.toByte(), 0.toByte(), 12.toByte(),
0.toByte(), 1.toByte())
private val outPacket = DatagramPacket(bytes,
bytes.size,
this#SocketManager.group,
5353)
//endregion
//region Methods
#Synchronized
suspend fun send() {
withContext(Dispatchers.Default) {
setUpSocket()
try {
this#OutgoingNsd.socket.send(this#OutgoingNsd.outPacket)
delay(1500L)
tearDownSocket()
} catch (e: Exception) {
}
}
}
//endregion
}
private inner class IncomingNsd {
//region Fields
private val isRunning = AtomicBoolean(false)
private var socket = MulticastSocket(5353)
//endregion
//region Any
fun setUpSocket() {
try {
this.socket = MulticastSocket(5353)
this.socket.reuseAddress = true
this.socket.joinGroup(group)
} catch (e: SocketException) {
} catch (e: BindException) {
}
}
fun run() {
GlobalScope.launch(Dispatchers.Default) {
setUpSocket()
try {
while (this#IncomingNsd.isRunning.get()) {
val bytes = ByteArray(4096)
val inPacket = DatagramPacket(bytes, bytes.size)
this#IncomingNsd.socket.receive(inPacket)
val incoming = DNSIncoming(inPacket)
for (answer in incoming.allAnswers) {
if (answer.key.contains("_my_mesh._tcp")) {
this#ManualNsdDiscovery.listener.get()?.onServiceFound(answer.recordSource.hostAddress, answer.name)
return#launch
}
}
}
this#IncomingNsd.socket.close()
} catch (e: Exception) {
}
}
}
//endregion
//region Methods
#Synchronized
fun startListening() {
if (this.isRunning.get()) {
return
}
this.isRunning.set(true)
run()
}
#Synchronized
fun stopListening() {
if (!this.isRunning.get()) {
return
}
this.isRunning.set(false)
}
//endregion
}
}
}

Categories

Resources