为什么即使初始化后,文本语音转换(TTS)对象也变为空?

时间:2019-03-09 01:11:03

标签: android kotlin nullpointerexception text-to-speech

我遇到tts variable的问题。即使“文字转语音”对象已初始化,它也为空。

ShowLabel()函数中,我想对tts变量的文本使用firstObjectShowLabel()函数接收List<>个对象中的FirebaseVisionImageLabel个。我在说ShowLabel()内的文字时遇到问题。尽管控制台将布尔值记录为true,但speak()内部的OnInit()仍然有效。

我已将检查器ttsIsInitialized设置为布尔型,因此在speak()上的showLabel()运行之前,它必须为true。但这给了我空。任何帮助都会很棒。如果有最佳方法,请告诉我。

所以,现在我的问题是tts中的Showlabel()总是为空。有人可以指出我的错误在哪里吗?

这是FirebaseQuickStart上的项目
https://github.com/firebase/quickstart-android/tree/master/mlkit

LivePreviewActivity

class LivePreviewActivity : AppCompatActivity(), OnRequestPermissionsResultCallback, TextToSpeech.OnInitListener {

    private var cameraSource: CameraSource? = null
    private var tts: TextToSpeech? = null
    var firstObject = ""


 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_live_preview)

        tts = TextToSpeech(this, this)

    }
fun showLabel(labels: List<FirebaseVisionImageLabel>?) {
        if (!ttsIsInitialized) {
            Log.d(TAG, "TTS not Initialized")
            Log.d("Boolean", ttsIsInitialized.toString())
        } else {
            firstObject = labels!!.first().text
            tts!!.speak(firstObject, TextToSpeech.QUEUE_FLUSH, null, "")
            Log.d("Boolean", ttsIsInitialized.toString())
        }
    }

    override fun onInit(status: Int) {

        if (status == TextToSpeech.SUCCESS) {
            val result = tts!!.setLanguage(Locale.US)

            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS","The Language specified is not supported!")
            } else {
                Log.d("TTS", "Initilization Successful")
                tts!!.speak("This is a test", TextToSpeech.QUEUE_FLUSH, null, "")
                cameraSource?.setMachineLearningFrameProcessor(ImageLabelingProcessor())
                ttsIsInitialized = true
            }
        } else {
            Log.e("TTS", "Initilization Failed!")
        }

    }

ImageLabelingProcessor

class ImageLabelingProcessor : VisionProcessorBase<List<FirebaseVisionImageLabel>>() {

    private val detector: FirebaseVisionImageLabeler = FirebaseVision.getInstance().onDeviceImageLabeler

    override fun stop() {
        try {
            detector.close()
        } catch (e: IOException) {
            Log.e(TAG, "Exception thrown while trying to close Text Detector: $e")
        }
    }

    override fun detectInImage(image: FirebaseVisionImage): Task<List<FirebaseVisionImageLabel>> {
        return detector.processImage(image)
    }

    override fun onSuccess(
        originalCameraImage: Bitmap?,
        labels: List<FirebaseVisionImageLabel>,
        frameMetadata: FrameMetadata,
        graphicOverlay: GraphicOverlay
    ) {
        graphicOverlay.clear()
        originalCameraImage.let { image ->
            val imageGraphic = CameraImageGraphic(graphicOverlay, image)
            graphicOverlay.add(imageGraphic)
        }
        val labelGraphic = LabelGraphic(graphicOverlay, labels)
        graphicOverlay.add(labelGraphic)
        graphicOverlay.postInvalidate()

        val livePreview = LivePreviewActivity()
        livePreview.showLabel(labels)
        livePreview.labels = labels
    }

    override fun onFailure(e: Exception) {
        Log.w(TAG, "Label detection failed.$e")
    }

    companion object {
        private const val TAG = "ImageLabelingProcessor"
    }
}

Logcat

2019-03-09 14:54:50.979 9658-9658/com.google.firebase.samples.apps.mlkit E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.google.firebase.samples.apps.mlkit, PID: 9658
    kotlin.KotlinNullPointerException
        at com.google.firebase.samples.apps.mlkit.kotlin.LivePreviewActivity.showLabel(LivePreviewActivity.kt:76)
        at com.google.firebase.samples.apps.mlkit.kotlin.imagelabeling.ImageLabelingProcessor.onSuccess(ImageLabelingProcessor.kt:51)
        at com.google.firebase.samples.apps.mlkit.kotlin.imagelabeling.ImageLabelingProcessor.onSuccess(ImageLabelingProcessor.kt:19)
        at com.google.firebase.samples.apps.mlkit.kotlin.VisionProcessorBase$detectInVisionImage$1.onSuccess(VisionProcessorBase.kt:98)
        at com.google.android.gms.tasks.zzn.run(Unknown Source:4)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-03-09 14:54:50.983 1881-1952/system_process W/ActivityManager:   Force finishing activity com.google.firebase.samples.apps.mlkit/.kotlin.LivePreviewActivity
2019-03-09 14:54:50.997 9658-9658/com.google.firebase.samples.apps.mlkit I/Process: Sending signal. PID: 9658 SIG: 9
2019-03-09 14:54:51.004 1596-1596/? E/lowmemorykiller: Error writing /proc/9658/oom_score_adj; errno=22
2019-03-09 14:54:51.026 1712-9704/? E/Camera3-OutputStream: getBufferLockedCommon: Stream 0: Can't dequeue next output buffer: Broken pipe (-32)
2019-03-09 14:54:51.026 1712-9704/? E/Camera3-Device: RequestThread: Can't get output buffer, skipping request: Broken pipe (-32)

0 个答案:

没有答案
相关问题