使用此代码,我想创建自定义TextToSpeech文件,其中包含使用Textedit编写的文本。 用户可以在TextEdit中写一些内容,然后TextToSpeech将其转换为语音,然后我想将此语音创建为audiofile,我可以分享。 但现在我不知道我的错误在哪里,我不知道进一步。 你能帮帮我吗:)
import android.os.Build;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import java.io.File;
import java.util.HashMap;
import java.util.Locale;
import static de.fresenborg.aufeinwort4.R.id.editText;
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener, View.OnClickListener {
private TextToSpeech tts;
private static String fileLocation;
private static String text;
public TextToSpeechFile(String fLocation, String txt) {
fileLocation = fLocation;
text = txt;
}
@Override
public void onClick(View view) {
final EditText wassoll = (EditText) findViewById(editText);
tts.speak(wassoll.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
ImageButton Sharebutton = (ImageButton) findViewById(R.id.Sharebutton);
Sharebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
public static void createFile(){
TextToSpeech tts;
HashMap<String, String> myHashRender = new HashMap();
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, text);
tts.synthesizeToFile(text, myHashRender, fileLocation);
final int utteranceId = 1;
File destinationFile = new File(getCacheDir(), utteranceId + ".wav");
int utteranceId++;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
tts.synthesizeToFile(TextToSpeech.ACTION_TTS_QUEUE_PROCESSING_COMPLETED, null, destinationFile, utteranceId);
} else {
Bundle params = new Bundle();
params.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utteranceId);
tts.synthesizeToFile(TextToSpeech.ACTION_TTS_QUEUE_PROCESSING_COMPLETED, params, destinationFile.getPath());)
tts.setOnUtteranceCompletedListener(new TextToSpeech.OnUtteranceCompletedListener() {
@Override
public void onUtteranceCompleted(String s) {
if (s.equals(utteranceId)) {
}
}
});
}
}
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(this, this);
}
@Override
public void onInit(int arg0) {
tts.setLanguage(Locale.GERMAN);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
}
这是我的XML:
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/my_toolbar"
android:ems="10"
android:hint="Was soll ich sagen?"
android:inputType="textMultiLine"
android:lines="8"
android:minLines="1"
android:text="@string/wassoll" />
<ImageButton
android:id="@+id/Sharebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button"
android:layout_toEndOf="@+id/button"
android:layout_toRightOf="@+id/button"
android:background="@null"
app:srcCompat="@drawable/share" />
<Button
android:id="@+id/button"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_below="@+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="@string/button_sag_es" />