Google Text-to-Speech

RezaScript

Bekanntes Mitglied
Hallo,

ich möchte gerne Google Text-to-Speech verwenden. Das Ziel ist es, dass ich auf einen Knopf klicke und ich "Hello World" höre.

So sieht mein Code aus (das meiste ist von Google selbst):
Java:
package ch.yourclick.kitt;

import android.os.Build;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.tabs.TabLayout;
import com.google.cloud.texttospeech.v1.AudioConfig;
import com.google.cloud.texttospeech.v1.AudioEncoding;
import com.google.cloud.texttospeech.v1.SsmlVoiceGender;
import com.google.cloud.texttospeech.v1.SynthesisInput;
import com.google.cloud.texttospeech.v1.SynthesizeSpeechResponse;
import com.google.cloud.texttospeech.v1.TextToSpeechClient;
import com.google.cloud.texttospeech.v1.VoiceSelectionParams;
import com.google.protobuf.ByteString;
import androidx.annotation.RequiresApi;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import android.os.StrictMode;
import android.view.View;
import java.io.FileOutputStream;
import java.io.OutputStream;
import ch.yourclick.kitt.ui.main.SectionsPagerAdapter;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
        ViewPager viewPager = findViewById(R.id.view_pager);
        viewPager.setAdapter(sectionsPagerAdapter);
        TabLayout tabs = findViewById(R.id.tabs);
        tabs.setupWithViewPager(viewPager);
        FloatingActionButton fab = findViewById(R.id.fab);

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }



    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onClick(View view) {
        int SDK_INT = android.os.Build.VERSION.SDK_INT;
        if (SDK_INT > 8)
        {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                    .permitAll().build();
            StrictMode.setThreadPolicy(policy);

            try {
                hello();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }


    /** Demonstrates using the Text-to-Speech API. */
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void hello() throws Exception {
        // Instantiates a client
        try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
            // Set the text input to be synthesized
            SynthesisInput input = SynthesisInput.newBuilder().setText("Hello, World!").build();

            // Build the voice request, select the language code ("en-US") and the ssml voice gender
            // ("neutral")
            VoiceSelectionParams voice =
                    VoiceSelectionParams.newBuilder()
                            .setLanguageCode("en-US")
                            .setSsmlGender(SsmlVoiceGender.NEUTRAL)
                            .build();

            // Select the type of audio file you want returned
            AudioConfig audioConfig =
                    AudioConfig.newBuilder().setAudioEncoding(AudioEncoding.MP3).build();

            // Perform the text-to-speech request on the text input with the selected voice parameters and
            // audio file type
            SynthesizeSpeechResponse response =
                    textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);

            // Get the audio contents from the response
            ByteString audioContents = response.getAudioContent();

            // Write the response to the output file.
            try (OutputStream out = new FileOutputStream("output.mp3")) {
                out.write(audioContents.toByteArray());
                System.out.println("Audio content written to file \"output.mp3\"");
            }
        }
    }

}

Wenn ich den Code ausführe, höre ich nichts, bekomme aber den folgenden Log:
2020-10-29 19:45:42.135 17423-17423/ch.yourclick.kitt W/System.err: 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.

Google meint, ich soll $env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\my-key.json" in Powershell eingeben. Das habe ich gemacht. Passiert aber gar nichts.

In Google Cloud SDK Shell habe ich mich so versucht zu authentifizieren:
Code:
gcloud auth application-default login

Das wird ausgegeben:
The environment variable [GOOGLE_APPLICATION_CREDENTIALS] is set to:
[C:\Users\rsaad\AndroidStudioProjects\Kitt\Kitt-0a9a71966cc8.json]
Credentials will still be generated to the default location:
[C:\Users\rsaad\AppData\Roaming\gcloud\application_default_credentia
ls.json]
To use these credentials, unset this environment variable before
running your application.

Do you want to continue (Y/n)?
Wenn ich Y tippe, kann ich mich erfolgreich authentifizieren und lande dann auf diese Seite: https://cloud.google.com/sdk/auth_success

Und wenn ich dann die App neu starte, bekomme ich wieder dieselbe Meldung im Log. Ich arbeite schon seit ein paar Tagen daran und bekomme es einfach nicht hin. Kann mir jemand bitte weiterhelfen? 😕
 
So ins blaue geraten würde ich denken, das in der Powershell gesetzte Variablen keinerlei Einfluss auf die emulierte Android-App hat, da muss man das vermutlich anders lösen
 
Ich würde auch mal in's Blaue raten und vllt mal schauen, ob du überhaupt Zugriff auf diesen Google-Dienst hast. Oder ob da irgendwie eine Zugriffsverweigerung ist?
 
Hallo Könnten Sie mir auch helfen. Ich möchte auch Google - Text - To Speech verwenden. Ich brauch wirklich Hilfe , wie ich das verwenden kann.
 
Hallo Könnten Sie mir auch helfen. Ich möchte auch Google - Text - To Speech verwenden. Ich brauch wirklich Hilfe , wie ich das verwenden kann.
Bitte eröffne doch einen eigenen Thread und gib uns genaue Details, was Du genau versuchst zu machen.

Willst Du wie der TE die Google Cloud Dienste dazu nutzen? => https://cloud.google.com/text-to-speech

Einfache Textausgabe auf Android? => https://developer.android.com/reference/android/speech/tts/TextToSpeech und https://www.tutorialspoint.com/android/android_text_to_speech.htm
 

Zurück
Oben