Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用android的google语音云api在google_应用程序_凭据中给出错误_Android_Kotlin_Google Cloud Platform_Google Text To Speech - Fatal编程技术网

使用android的google语音云api在google_应用程序_凭据中给出错误

使用android的google语音云api在google_应用程序_凭据中给出错误,android,kotlin,google-cloud-platform,google-text-to-speech,Android,Kotlin,Google Cloud Platform,Google Text To Speech,我正在尝试在android kotlin中构建一个使用谷歌语音云api的应用程序 这是我的密码 launch { googleTextToSpeech = TextToSpeechClient.create() googleTextToSpeech?.let { viewModel.speakGoogle(googleTextToSpeech!!, totalMessage, player) }

我正在尝试在android kotlin中构建一个使用谷歌语音云api的应用程序

这是我的密码

launch {
             googleTextToSpeech = TextToSpeechClient.create()
             googleTextToSpeech?.let {
             viewModel.speakGoogle(googleTextToSpeech!!, totalMessage, player)
          }
       }


 fun speakGoogle(textToSpeech: com.google.cloud.texttospeech.v1.TextToSpeechClient, message: String, player: MediaPlayer) {

        val filePath = Environment.getExternalStorageDirectory().absolutePath + "/google_" + System.currentTimeMillis() + ".mp3"

        launch {

            var msg = android.text.Html.fromHtml(message).toString()
            msg = msg.replace("\"", "")


            var input = SynthesisInput.newBuilder().setText(msg).build()
            var voice = VoiceSelectionParams.newBuilder().setLanguageCode("en-US").setSsmlGender(SsmlVoiceGender.FEMALE).build()
            var audio = AudioConfig.newBuilder().setAudioEncoding(AudioEncoding.MP3).build()

            var response = textToSpeech.synthesizeSpeech(input, voice, audio)

            response?.let {

                try {
                    val inputStream = response.audioContent.toByteArray()

                    File(filePath).outputStream().use { inputStream }

                    player.setDataSource(filePath)
                    player.prepare()
                    player.start()
                } catch (e: IOException) {
                    Log.i("AMIRA00000", e.toString())
                } catch (e: IllegalStateException) {
                    Log.i("AMIRA00000", e.toString())
                }
            }
        }
    }
我的google-service.json文件也包含在应用程序中

但是我得到了以下错误

java.io.IOException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.
at com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:134)
at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:119)
at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:91)
at com.google.api.gax.core.GoogleCredentialsProvider.getCredentials(GoogleCredentialsProvider.java:67)
at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:135)
at com.google.cloud.texttospeech.v1.stub.GrpcTextToSpeechStub.create(GrpcTextToSpeechStub.java:74)
at com.google.cloud.texttospeech.v1.stub.TextToSpeechStubSettings.createStub(TextToSpeechStubSettings.java:100)
at com.google.cloud.texttospeech.v1.TextToSpeechClient.<init>(TextToSpeechClient.java:128)
at com.google.cloud.texttospeech.v1.TextToSpeechClient.create(TextToSpeechClient.java:109)
at com.google.cloud.texttospeech.v1.TextToSpeechClient.create(TextToSpeechClient.java:101)
at com.sbs16.ensofia.view.main.MainFragment$setupBinding$3$$special$$inlined$let$lambda$2.invokeSuspend(MainFragment.kt:186)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:238)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)
java.io.IOException:应用程序默认凭据不可用。如果在谷歌计算引擎中运行,它们是可用的。否则,必须定义指向定义凭据的文件的环境变量GOOGLE\u APPLICATION\u CREDENTIALS。看见https://developers.google.com/accounts/docs/application-default-credentials 了解更多信息。
位于com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:134)
位于com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:119)
位于com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:91)
位于com.google.api.gax.core.GoogleCredentialsProvider.getCredentials(GoogleCredentialsProvider.java:67)
在com.google.api.gax.rpc.ClientContext.create(ClientContext.java:135)上
位于com.google.cloud.texttospeech.v1.stub.GrpcTextToSpeechStub.create(GrpcTextToSpeechStub.java:74)
位于com.google.cloud.texttospeech.v1.stub.TextToSpeechStubSettings.createStub(TextToSpeechStubSettings.java:100)
位于com.google.cloud.texttospeech.v1.TextToSpeechClient.(TextToSpeechClient.java:128)
位于com.google.cloud.texttospeech.v1.TextToSpeechClient.create(TextToSpeechClient.java:109)
位于com.google.cloud.texttospeech.v1.TextToSpeechClient.create(TextToSpeechClient.java:101)
在com.sbs16.ensofia.view.main.MainFragment$setupBinding$3$$special$$inlined$let$lambda$2.invokeSuspend(MainFragment.kt:186)
位于kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
在kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:238)
在kotlinx.coroutines.scheduling.CoroutineScheduler.runsafe(CoroutineScheduler.kt:594)
在kotlinx.coroutines.scheduling.CoroutineScheduler.access$runsafe(CoroutineScheduler.kt:60)
在kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)

我还有另一个json文件,它有私钥和其他凭据设置,但我不知道如何使用它

我不是Android开发者,但我的感觉是,你不应该直接从Android应用程序调用TextToSpeech。您应该通过后端调用它(例如在AppEngine或Firebase函数上)

此后端由TextToSpeech API进行身份验证,您的Android客户端在后端进行身份验证

因此,您可以控制谁使用您的应用程序,以及您的TextToSpeech功能。在任何情况下,都不要将服务帐户密钥文件放在你的应用程序中,下载你的应用程序的任何人都可以窃取密钥并代表服务帐户执行呼叫,你将支付账单