Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: progressHandler not working on new devices & handlers not responding #474

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ class FlutterTtsPlugin : MethodCallHandler, FlutterPlugin {
invokeMethod("speak.onStart", true)
}
}
if (Build.VERSION.SDK_INT < 26) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is progressHandler not working for you on newer devices? This was added because onRangeStart was added in SDK version 26 which will add to onProgress. This onStart will get called regardless since it was added in version 15, so we only need to add to onProgress here if less than 26. Basically if your device is 26 or greater, the progressHandler should work via the onRangeStart listener.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not receiving on Progress Events from native to flutter side on Android 14.
When I removed this, It was receiving on Flutter Side.
Then I was getting issue of NULL functions and when I called setChannelMethodCallHandler after setting all of the handlers, It started working i.e. sending data to project.
cc @dlutton

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@theatifwaheed that's very odd, I'll have to test this before merging in since I don't want the issue of having onProgress being called multiple times initially.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@theatifwaheed I apologize for the extremely late reply. I tested on a pixel 5 android version 14 device and it's working in the master branch without this change. Can you provide an example of it not working? This is an example of the code I used to confirm it's working:
https://gist.githubusercontent.com/dlutton/30b065449cb2eb941dfd6bef86aeefaf/raw/5ee94f80f0cbd678c5645c27caaaa078c307376f/ttsSetProgressHandler.dart

onProgress(utteranceId, 0, utterances[utteranceId]!!.length)
}
onProgress(utteranceId, 0, utterances[utteranceId]!!.length)
}

override fun onDone(utteranceId: String) {
Expand Down Expand Up @@ -328,18 +326,29 @@ class FlutterTtsPlugin : MethodCallHandler, FlutterPlugin {
}

"synthesizeToFile" -> {
Log.d(tag, "synthesizeToFile 1")
val text: String? = call.argument("text")
Log.d(tag, "synthesizeToFile 2")
if (synth) {
Log.d(tag, "synthesizeToFile 3")
result.success(0)
return
}
Log.d(tag, "synthesizeToFile 4")
val fileName: String? = call.argument("fileName")
Log.d(tag, "synthesizeToFile 5")
synthesizeToFile(text!!, fileName!!)
Log.d(tag, "synthesizeToFile 5")
if (awaitSynthCompletion) {
Log.d(tag, "synthesizeToFile 6")
synth = true
Log.d(tag, "synthesizeToFile 7")
synthResult = result
Log.d(tag, "synthesizeToFile 8")
} else {
Log.d(tag, "synthesizeToFile 9")
result.success(1)
Log.d(tag, "synthesizeToFile 10")
}
}

Expand Down Expand Up @@ -651,37 +660,46 @@ class FlutterTtsPlugin : MethodCallHandler, FlutterPlugin {
}

private fun synthesizeToFile(text: String, fileName: String) {
Log.d(tag, "synthesizeToFile 5a")
val fullPath: String
val uuid: String = UUID.randomUUID().toString()
Log.d(tag, "synthesizeToFile 5b")
bundle!!.putString(
TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,
SYNTHESIZE_TO_FILE_PREFIX + uuid
)
Log.d(tag, "synthesizeToFile 5c")

val result: Int =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Log.d(tag, "synthesizeToFile 5d")
val resolver = this.context?.contentResolver
val contentValues = ContentValues().apply {
put(MediaStore.MediaColumns.DISPLAY_NAME, fileName)
put(MediaStore.MediaColumns.MIME_TYPE, "audio/wav")
put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_MUSIC)
}
Log.d(tag, "synthesizeToFile 5e")
val uri = resolver?.insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, contentValues)
this.parcelFileDescriptor = resolver?.openFileDescriptor(uri!!, "rw")
fullPath = uri?.path + File.separatorChar + fileName

Log.d(tag, "synthesizeToFile 5f")
tts!!.synthesizeToFile(text, bundle!!, parcelFileDescriptor!!, SYNTHESIZE_TO_FILE_PREFIX + uuid)
} else {
Log.d(tag, "synthesizeToFile 5g")
val musicDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)
val file = File(musicDir, fileName)
fullPath = file.path

Log.d(tag, "synthesizeToFile 5h")
tts!!.synthesizeToFile(text, bundle!!, file!!, SYNTHESIZE_TO_FILE_PREFIX + uuid)
}
Log.d(tag, "synthesizeToFile 5i")

if (result == TextToSpeech.SUCCESS) {
Log.d(tag, "synthesizeToFile 5j")
Log.d(tag, "Successfully created file : $fullPath")
} else {
Log.d(tag, "synthesizeToFile 5k")
Log.d(tag, "Failed creating file : $fullPath")
}
}
Expand Down
4 changes: 4 additions & 0 deletions lib/flutter_tts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,10 @@ class FlutterTts {
ErrorHandler? errorHandler;

FlutterTts() {
setChannelMethodCallHandler();
}

void setChannelMethodCallHandler() {
_channel.setMethodCallHandler(platformCallHandler);
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_tts
description: A flutter plugin for Text to Speech. This plugin is supported on iOS, macOS, Android, Web, & Windows.
version: 4.0.2
version: 4.0.3
homepage: https://github.com/dlutton/flutter_tts

dependencies:
Expand Down