Gui*_*Yôm 8 java google-text-to-speech
我无法在 Java 中使用 Google TTS 客户端库发出请求。每次它都会抛出一堆异常。
我只是尝试获取可用声音的列表。
GoogleCredentials creds = null;
TextToSpeechClient textToSpeechClient = null;
try {
creds = GoogleCredentials.fromStream(new FileInputStream(credsFile));
TextToSpeechSettings settings = TextToSpeechSettings.newBuilder().setCredentialsProvider(FixedCredentialsProvider.create(creds)).build();
textToSpeechClient = TextToSpeechClient.create(settings);
} catch (IOException e) {
e.printStackTrace();
System.exit(-2);
}
if (cmd.hasOption('l')) {
ListVoicesRequest request = ListVoicesRequest.getDefaultInstance();
ListVoicesResponse response = textToSpeechClient.listVoices(request);
List<Voice> voices = response.getVoicesList();
System.out.println("Available voices :");
for (Voice v : voices) {
System.out.printf(" - %s, [%d]: %s/%s", v.getName(), v.getLanguageCodesCount(), v.getLanguageCodes(0), v.getSsmlGender());
}
textToSpeechClient.close();
System.exit(0);
}
Run Code Online (Sandbox Code Playgroud)
我首先认为它来自凭据文件。但事实并非如此,该文件的位置正确。
我明白了。
avr. 02, 2019 11:36:46 PM io.grpc.internal.ManagedChannelImpl$1 uncaughtException
SEVERE: [Channel<1>: (texttospeech.googleapis.com:443)] Uncaught exception in the SynchronizationContext. Panic!
java.lang.IllegalStateException: Could not find policy 'pick_first'. Make sure its implementation is either registered to LoadBalancerRegistry or included in META-INF/services/io.grpc.LoadBalancerProvider from your jar files.
at io.grpc.internal.AutoConfiguredLoadBalancerFactory$AutoConfiguredLoadBalancer.<init>(AutoConfiguredLoadBalancerFactory.java:93)
at io.grpc.internal.AutoConfiguredLoadBalancerFactory.newLoadBalancer(AutoConfiguredLoadBalancerFactory.java:64)
at io.grpc.internal.ManagedChannelImpl.exitIdleMode(ManagedChannelImpl.java:357)
at io.grpc.internal.ManagedChannelImpl$ChannelTransportProvider$1ExitIdleModeForTransport.run(ManagedChannelImpl.java:455)
at io.grpc.SynchronizationContext.drain(SynchronizationContext.java:101)
at io.grpc.SynchronizationContext.execute(SynchronizationContext.java:130)
at io.grpc.internal.ManagedChannelImpl$ChannelTransportProvider.get(ManagedChannelImpl.java:459)
(...) a whole bunch of other lines
Run Code Online (Sandbox Code Playgroud)
如何修复此错误?
请注意,我使用的是最新的 google-cloud-texttospeech 库(版本 0.85.0-beta)。
另一个解决方案。希望可以帮助别人。
问题是 io.grpc.INTERNAL.PickFirstLoadBalacerProvider 类的包名称。在使用 grpc 之前插入以下代码行。
import com.google.cloud.internal.PickFirstLoadBalancer;
...
LoadBalancerRegistry.getDefaultRegistry().register(new PickFirstLoadBalancerProvider());
Run Code Online (Sandbox Code Playgroud)
LoadBalancerProviders 在 LoadBalancerRegistry 的映射中注册,映射的键是类的名称(不完全是,但并不重要)。因此LoadBalancerRegistry将新注册的类返回给grpc。
小智 6
该io.grpc库从您的META-INF/services.
因此,创建一个io.grpc.LoadBalancerProvider在该文件夹中命名的文件,内容如下:
io.grpc.internal.PickFirstLoadBalancerProvider
Run Code Online (Sandbox Code Playgroud)
图书馆应该以这种方式找到课程。
如果您将 Gradle 与 ShadowJar 插件一起使用,那么您只需使用它来合并来自各种 gRPC 库的服务文件的内容:
shadowJar {
mergeServiceFiles()
}
Run Code Online (Sandbox Code Playgroud)
从这里的一个线程中发现。