您可能知道,在Android TTS中使用 addSpeech(),您可以将某个文本链接到声音文件。然后,tts引擎播放文件而不是合成文本的声音。
使用Google TTS(不是Samsung TTS),来自上面的Marshmallow(api 23),addSpeech&说不能在外部存储器中播放文件。在Marshmallow之前一切都很好。
那些使用过android tts和addSpeech的人,请看看下面的代码并帮我找到问题。
如果我将文件保存在内部存储器中(使用MODE_WORLD_READABLE),即使使用Google TTS也能正常工作。但是现在不推荐使用MODE_WORLD_READABLE,所以我更喜欢外部存储。
感谢您的关注,祝您度过愉快的一天。
MainActivity.java
package com.woojung.addspeechex;
import android.app.Activity;
import android.media.MediaScannerConnection;
import android.os.Bundle;
import android.os.Environment;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.util.Locale;
public class MainActivity extends Activity {
TextToSpeech t1;
EditText ed1;
Button b1, b2;
@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.button1);
b2 = (Button) findViewById(R.id.button2);
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);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String toSpeak = "boy";
Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File destinationPath = new File(dir, "boy.mp3");
MediaScannerConnection.scanFile(MainActivity.this, new String[]{destinationPath.toString()}, null, null);
t1.addSpeech(toSpeak, destinationPath.toString());
t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}
});
}
public void onPause() {
if (t1 != null) {
t1.stop();
t1.shutdown();
}
super.onPause();
}
}
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: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="Synthesize the editText."
android:id="@+id/button1"
android:layout_below="@+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AddSpeech and play boy.mp3"
android:id="@+id/button2"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp" />
</RelativeLayout>
build.grade(模块)的
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.woojung.addspeechex"
minSdkVersion 16
targetSdkVersion 22
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:22.2.0'
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.woojung.addspeechex">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>