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? 😕
 

mrBrown

Super-Moderator
Mitarbeiter
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
 

der_it_typ

Mitglied
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?
 

s3rh47

Mitglied
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.
 
K

kneitzel

Gast
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
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
Meister-Yona Google Direction api / waze api Android & Cross-Platform Mobile Apps 2
O Google Admob Ad wird nicht geladen und App stürzt ab Android & Cross-Platform Mobile Apps 1
G Google Play Store Design Frage Android & Cross-Platform Mobile Apps 2
S Termin aus *.txt Datei in Google Kalender eintragen? Android & Cross-Platform Mobile Apps 1
F Probleme mit Google-Maps Android & Cross-Platform Mobile Apps 0
EisKaffee Android Google Maps in eigener App Android & Cross-Platform Mobile Apps 2
D Android Google In-App-Käufe Android & Cross-Platform Mobile Apps 4
I Google Werbung in Spiel einbinden Android & Cross-Platform Mobile Apps 6
A Android Daten in der Cloud speichern (Google) Android & Cross-Platform Mobile Apps 1
P Google Maps auf Android ab welcher Version Android & Cross-Platform Mobile Apps 4
U Android Android Google API Android & Cross-Platform Mobile Apps 5
W Text input into editText Android & Cross-Platform Mobile Apps 2
W Edit Text Drawable Icon ändern plus Funktion Android & Cross-Platform Mobile Apps 30
R wie verwende ich Cloud Text-to-Speech? Android & Cross-Platform Mobile Apps 4
R Android Text-To-Speech Android & Cross-Platform Mobile Apps 4
ruutaiokwu Android In einem Android-“Spinner”-Element GLEICHZEITIG Bild (links) UND Text (rechts) anzeigen Android & Cross-Platform Mobile Apps 0
J Android App - Browser öffnen und Text eingeben/Button click auslösen Android & Cross-Platform Mobile Apps 10
M ImageButton: Bild ausblenden und Text anzeigen Android & Cross-Platform Mobile Apps 2
Flynn Text-File auf externe Speicherkarte schreiben Android & Cross-Platform Mobile Apps 1
F Aufkalbbaren Text Android & Cross-Platform Mobile Apps 4
B Android Text von Android zu PC senden? Android & Cross-Platform Mobile Apps 5
C Android Bei Text "Popup" anzeigen Android & Cross-Platform Mobile Apps 2
Anfänger2011 Text to Speech Problem Android & Cross-Platform Mobile Apps 1
M Android Edittext width passend zum Text Android & Cross-Platform Mobile Apps 1
L Android Theorie: Umwandeln von Text in Morsecode Android & Cross-Platform Mobile Apps 15
R Android Layout Bild mit Text Android & Cross-Platform Mobile Apps 13
N Android Änderung von tabwidget height lässt text verschwinden Android & Cross-Platform Mobile Apps 3
L Android Text aus Textfeld speichern Android & Cross-Platform Mobile Apps 5
J in android app text automatisch kopieren Android & Cross-Platform Mobile Apps 2
S Android Zufällige Text wiedergabe Android & Cross-Platform Mobile Apps 6
G Android zur Laufzeit den Text im Menü ändern Android & Cross-Platform Mobile Apps 3
K Android schwarzer Bildschirm beim Rendern von Text und Dreiecken Android & Cross-Platform Mobile Apps 9
S Android Canvas - drawText - Update Text in ActionListener Android & Cross-Platform Mobile Apps 8
N Gibt es eine opensource Speech-to-Text engine? Android & Cross-Platform Mobile Apps 3
M Text in txt-Datei schreiben und nach ABC sortieren? Android & Cross-Platform Mobile Apps 2
M Text in Canvas scrollen lassen Android & Cross-Platform Mobile Apps 13
G Text parsen String to Double Android & Cross-Platform Mobile Apps 2

Ähnliche Java Themen

Neue Themen


Oben