2017-11-24 1 views

Antwort

1

Sie können ein sehr detailliertes Beispiel finden, das zeigt, wie man Text in Sprache umwandelt site. HTH.

Ihr MainActivity.java so etwas wie dieses

import android.app.Activity; 
import android.os.Bundle; 
import android.speech.tts.TextToSpeech; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import java.util.Locale; 
import android.widget.Toast; 

public class MainActivity extends Activity { 
    TextToSpeech t1; 
    EditText ed1; 
    Button b1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ed1=(EditText)findViewById(R.id.editText); 
     b1=(Button)findViewById(R.id.button); 

     t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() { 
     @Override 
     public void onInit(int status) { 
      if(status != TextToSpeech.ERROR) { 
       t1.setLanguage(Locale.UK); 
      } 
     } 
     }); 

     b1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String toSpeak = ed1.getText().toString(); 
      Toast.makeText(getApplicationContext(), toSpeak,Toast.LENGTH_SHORT).show(); 
      t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null); 
     } 
     }); 
    } 

    public void onPause(){ 
     if(t1 !=null){ 
     t1.stop(); 
     t1.shutdown(); 
     } 
     super.onPause(); 
    } 
} 

aussehen sollte, und hier ist der Inhalt der activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" 
    android:transitionGroup="true"> 

    <TextView android:text="Text to Speech" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/textview" 
     android:textSize="35dp" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Tutorials point" 
     android:id="@+id/textView" 
     android:layout_below="@+id/textview" 
     android:layout_centerHorizontal="true" 
     android:textColor="#ff7aff24" 
     android:textSize="35dp" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView" 
     android:src="@drawable/abc" 
     android:layout_below="@+id/textView" 
     android:layout_centerHorizontal="true" 
     android:theme="@style/Base.TextAppearance.AppCompat" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/editText" 
     android:layout_below="@+id/imageView" 
     android:layout_marginTop="46dp" 
     android:hint="Enter Text" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:textColor="#ff7aff10" 
     android:textColorHint="#ffff23d1" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Text to Speech" 
     android:id="@+id/button" 
     android:layout_below="@+id/editText" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="46dp" /> 

</RelativeLayout> 
+0

Danke für die Hilfe! – HackNode

+1

@Akjun Teh Sie können keine Links zur Verfügung stellen. Links können in der Zukunft ausgelaufen sein, also setzen Sie den passenden Code für zukünftige Leser, wenn Sie nicht korrekten Code zur Verfügung stellen würden, den Sie downvotes erhalten können. –

+0

@AbhishekSingh ok notiert .. wird versuchen, den Schritt zu bearbeiten ... – Akjun

2

Try this ...

TextToSpeech TextToSpeech;

onCreate

textToSpeech = new TextToSpeech(this, this); 

Überschreibung Methode

ein Rückruf aufgerufen werden, um den Abschluss der Initialisierung TextToSpeech Motors anzeigt.

@see android.speech.tts.TextToSpeech.OnInitListener # onInit (int)

@Override 
public void onInit(int status) { 
    if (status == TextToSpeech.SUCCESS) { 
    int result = textToSpeech.setLanguage(Locale.US); 
    if (result == TextToSpeech.LANG_MISSING_DATA 
    || result == TextToSpeech.LANG_NOT_SUPPORTED) { 
    Log.e("error", "This Language is not supported"); 
    } else { 
    convertTextToSpeech(); 
    } 
    } else { 
    Log.e("error", "Initilization Failed!"); 
    } 
} 

Spricht die Zeichenfolge die angegebenen Warteschlangenstrategie und Sprachparameter verwendet wird.

private void convertTextToSpeech() { 
    String text = inputText.getText().toString(); 

    text = "Hello World"; 

    textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null); 
} 

Sehen Sie sich diese sample