Android Text-To-Speech

RezaScript

Bekanntes Mitglied
Hallo, ich möchte mit TextToSpeech den Text "Hello" als Audiosignal ausgeben und habe es so probiert:

Java:
package ch.yourclick.kitt.fragments;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.util.Locale;
import ch.yourclick.kitt.R;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link GeneralFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class GeneralFragment extends Fragment {
    private TextToSpeech tts;

    public GeneralFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment General.
     */
    // TODO: Rename and change types and number of parameters
    public static GeneralFragment newInstance() {
        GeneralFragment fragment = new GeneralFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Text to speech
        tts = new TextToSpeech(getActivity(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int i) {
                if (i == TextToSpeech.SUCCESS) {
                    int result = tts.setLanguage(Locale.GERMAN);
                    // Language is not supported
                    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Log.e("TTS", "Language not supported");
                    }
                }
                else {
                    Log.e("TTS", "Initialization failed");
                }
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_general, container, false);
        Button hello = (Button) view.findViewById(R.id.hello);

        hello.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                speak();
            }
        });
        return view;
    }

    /**
     * Speak
     */
    private void speak() {
        String text = "Hello";
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }

    /**
     * Turn off
     */
    @Override
    public void onDestroy() {
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }
}
Den Code habe ich nicht in MainActivity, sondern in einem Fragment.

Ich bekomme dabei die Meldung:
W/TextToSpeech: speak failed: not bound to TTS engine

Was mache ich genau falsch?
 
Hallo, ich möchte mit TextToSpeech den Text "Hello" als Audiosignal ausgeben und habe es so probiert:

Java:
package ch.yourclick.kitt.fragments;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.util.Locale;
import ch.yourclick.kitt.R;

/**
* A simple {@link Fragment} subclass.
* Use the {@link GeneralFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class GeneralFragment extends Fragment {
    private TextToSpeech tts;

    public GeneralFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment General.
     */
    // TODO: Rename and change types and number of parameters
    public static GeneralFragment newInstance() {
        GeneralFragment fragment = new GeneralFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Text to speech
        tts = new TextToSpeech(getActivity(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int i) {
                if (i == TextToSpeech.SUCCESS) {
                    int result = tts.setLanguage(Locale.GERMAN);
                    // Language is not supported
                    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Log.e("TTS", "Language not supported");
                    }
                }
                else {
                    Log.e("TTS", "Initialization failed");
                }
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_general, container, false);
        Button hello = (Button) view.findViewById(R.id.hello);

        hello.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                speak();
            }
        });
        return view;
    }

    /**
     * Speak
     */
    private void speak() {
        String text = "Hello";
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }

    /**
     * Turn off
     */
    @Override
    public void onDestroy() {
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }
}
Den Code habe ich nicht in MainActivity, sondern in einem Fragment.

Ich bekomme dabei die Meldung:


Was mache ich genau falsch?
Es könnte daran liegen https://stackoverflow.com/questions/18129712/speak-failed-not-bound-to-tts-engine
 
@TM69 ich habe nun die Funktion onInit() erstellt und mein Code da reingepackt aber dann bekomme ich die Fehlermeldung:
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.speech.tts.TextToSpeech.speak(java.lang.String, int, java.util.HashMap)' on a null object reference

Wenn ich mich nicht täusche, wird die Funktion onInit() nur bei Activities verwendet aber nicht für Fragments.
 
Oh ich habe grad gesehen, dass ich die Meldung "Initialization failed" bekomme. Das Steht so in meinem Code: Log.e("TTS", "Initialization failed"); ... aber warum ist i nicht gleich TextToSpeech.SUCCESS?
 
Hmm, so langsam bekomme ich das Gefühl, dass mit meinem Code alles OK ist. Muss ich vielleicht irgendetwas installieren? In meinem Emulator ist Text-To-Speech (von Google) bereits installiert. Ich wäre sehr dankbar, wenn irgendjemand mein Code bei sich mit Android Studio testen würde.
 

Zurück
Oben