在微调器中显示 TTS 可用语言
displaying TTS available languages in spinner
我最近开始 android 编程,我正在尝试查看设备上可以使用哪些语言的文本转语音并将它们显示在微调器中,但我 运行真是奇怪的问题。
代码的相关部分如下:
package com.example.dshawn.ttsapp;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.*;
public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemSelectedListener {
private TextToSpeech mTTS;
Locale[] locales = Locale.getAvailableLocales();
List<Locale> localeList = new ArrayList<Locale>();
List<String> country= new ArrayList<String>();
List<String> asdf= new ArrayList<String>();
int sum=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int i) {
if (i==TextToSpeech.SUCCESS){
for (Locale locale : locales) {
int res = mTTS.isLanguageAvailable(locale);
if (res == TextToSpeech.LANG_COUNTRY_AVAILABLE) {
sum++;
localeList.add(locale);
country.add(locale.getDisplayName());
}
}
asdf.add(Integer.toString(sum));
}
}
});
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) findViewById(R.id.spinner);
spin.setOnItemSelectedListener(this);
//Creating the ArrayAdapter instance having the country list
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,country);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
}
//Performing action onItemSelected and onNothing selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
Toast.makeText(getApplicationContext(),country.get(position) , Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
在此 TTS 初始化之后,我尝试打印总和,它显示 0。我尝试通过执行类似 String=country.get(1) 的操作来访问 localeList 和国家/地区中的元素;它会在编译器中给出一个错误,说我试图访问的索引不存在并且列表中有 0 个元素。所以此时似乎设备上可能没有可用的语言,但奇怪的是当我通过 ArrayAdapter 将国家/地区 ArrayList 放入微调器下拉菜单时,我设备上的实际可用国家/地区确实显示在微调器中菜单,但它坏了,这是我的意思的 gif:
微调器永远不会进入我的 onItemSelected 侦听器,并且在单击该项目后该项目不会显示在微调器中。我进入了设备的设置,显示的设置确实是该设备上 TTS 的可用语言环境。当我删除似乎从未访问过的代码的 "if (res == TextToSpeech.LANG_COUNTRY_AVAILABLE)" 部分时,微调器下拉菜单中将没有任何内容。所以它显然以某种方式抓住了可用的语言环境但是当我尝试访问数组列表时它表现得好像它是空的,但它以某种方式使用 "empty" 数组列表并将正确的国家放入微调框但是微调框可能坏了因为 ArrayList 坏了,因为我用手动填充的 arraylist 尝试了同样的事情,元素正常显示,我可以 select 它正常。
这可能是什么问题?我已经尝试了所有我能想到的方法,我所知道的是问题出在 TTS 初始化部分。
基本上我无法访问在 TTS 的 "onInit" 方法中设置或添加的任何项目,这也会导致我的旋转器损坏。
你打电话给哪里String=country.get(1);
?因为这是一个回调,所以您不知道它何时会被调用。如果将代码放在 if
语句块之后的 onInit
中,您看到值了吗?
我认为问题在于您在填充列表之前创建适配器(使用国家/地区列表)(因为 TTS 尚未初始化)。
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private TextToSpeech mTTS;
Locale[] locales = Locale.getAvailableLocales();
List<Locale> localeList = new ArrayList<Locale>();
List<String> country = new ArrayList<String>();
// * - Move these two variables out here
ArrayAdapter<String> aa;
Spinner spin;
int sum = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i("XXX", "onCreate() was called");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int i) {
if (i == TextToSpeech.SUCCESS) {
// * - delay important tasks until TTS is initialized
startWhenTTSIsInitialized();
}
}
});
//Getting the instance of Spinner and applying OnItemSelectedListener on it
spin = (Spinner) findViewById(R.id.spinner);
}
private void startWhenTTSIsInitialized() {
Log.i("XXX", "startWhenTTSIsInitialized() was called");
for (Locale locale : locales) {
int res = mTTS.isLanguageAvailable(locale);
if (res == TextToSpeech.LANG_COUNTRY_AVAILABLE) {
sum++;
localeList.add(locale);
// * - 'country' is used to populate the adapter, so
// this line must come first
country.add(locale.getDisplayName());
}
}
//Creating the ArrayAdapter instance having the country list
// * - now it's safe to set the adapter because the country list has been populated
aa = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, country);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
spin.setOnItemSelectedListener(this);
}
//Performing action onItemSelected and onNothing selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
Log.i("XXX", "onItemSelected() was called");
Toast.makeText(getApplicationContext(), country.get(position), Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
我最近开始 android 编程,我正在尝试查看设备上可以使用哪些语言的文本转语音并将它们显示在微调器中,但我 运行真是奇怪的问题。
代码的相关部分如下:
package com.example.dshawn.ttsapp;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.*;
public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemSelectedListener {
private TextToSpeech mTTS;
Locale[] locales = Locale.getAvailableLocales();
List<Locale> localeList = new ArrayList<Locale>();
List<String> country= new ArrayList<String>();
List<String> asdf= new ArrayList<String>();
int sum=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int i) {
if (i==TextToSpeech.SUCCESS){
for (Locale locale : locales) {
int res = mTTS.isLanguageAvailable(locale);
if (res == TextToSpeech.LANG_COUNTRY_AVAILABLE) {
sum++;
localeList.add(locale);
country.add(locale.getDisplayName());
}
}
asdf.add(Integer.toString(sum));
}
}
});
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) findViewById(R.id.spinner);
spin.setOnItemSelectedListener(this);
//Creating the ArrayAdapter instance having the country list
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,country);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
}
//Performing action onItemSelected and onNothing selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
Toast.makeText(getApplicationContext(),country.get(position) , Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
在此 TTS 初始化之后,我尝试打印总和,它显示 0。我尝试通过执行类似 String=country.get(1) 的操作来访问 localeList 和国家/地区中的元素;它会在编译器中给出一个错误,说我试图访问的索引不存在并且列表中有 0 个元素。所以此时似乎设备上可能没有可用的语言,但奇怪的是当我通过 ArrayAdapter 将国家/地区 ArrayList 放入微调器下拉菜单时,我设备上的实际可用国家/地区确实显示在微调器中菜单,但它坏了,这是我的意思的 gif:
微调器永远不会进入我的 onItemSelected 侦听器,并且在单击该项目后该项目不会显示在微调器中。我进入了设备的设置,显示的设置确实是该设备上 TTS 的可用语言环境。当我删除似乎从未访问过的代码的 "if (res == TextToSpeech.LANG_COUNTRY_AVAILABLE)" 部分时,微调器下拉菜单中将没有任何内容。所以它显然以某种方式抓住了可用的语言环境但是当我尝试访问数组列表时它表现得好像它是空的,但它以某种方式使用 "empty" 数组列表并将正确的国家放入微调框但是微调框可能坏了因为 ArrayList 坏了,因为我用手动填充的 arraylist 尝试了同样的事情,元素正常显示,我可以 select 它正常。
这可能是什么问题?我已经尝试了所有我能想到的方法,我所知道的是问题出在 TTS 初始化部分。
基本上我无法访问在 TTS 的 "onInit" 方法中设置或添加的任何项目,这也会导致我的旋转器损坏。
你打电话给哪里String=country.get(1);
?因为这是一个回调,所以您不知道它何时会被调用。如果将代码放在 if
语句块之后的 onInit
中,您看到值了吗?
我认为问题在于您在填充列表之前创建适配器(使用国家/地区列表)(因为 TTS 尚未初始化)。
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private TextToSpeech mTTS;
Locale[] locales = Locale.getAvailableLocales();
List<Locale> localeList = new ArrayList<Locale>();
List<String> country = new ArrayList<String>();
// * - Move these two variables out here
ArrayAdapter<String> aa;
Spinner spin;
int sum = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i("XXX", "onCreate() was called");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int i) {
if (i == TextToSpeech.SUCCESS) {
// * - delay important tasks until TTS is initialized
startWhenTTSIsInitialized();
}
}
});
//Getting the instance of Spinner and applying OnItemSelectedListener on it
spin = (Spinner) findViewById(R.id.spinner);
}
private void startWhenTTSIsInitialized() {
Log.i("XXX", "startWhenTTSIsInitialized() was called");
for (Locale locale : locales) {
int res = mTTS.isLanguageAvailable(locale);
if (res == TextToSpeech.LANG_COUNTRY_AVAILABLE) {
sum++;
localeList.add(locale);
// * - 'country' is used to populate the adapter, so
// this line must come first
country.add(locale.getDisplayName());
}
}
//Creating the ArrayAdapter instance having the country list
// * - now it's safe to set the adapter because the country list has been populated
aa = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, country);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
spin.setOnItemSelectedListener(this);
}
//Performing action onItemSelected and onNothing selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
Log.i("XXX", "onItemSelected() was called");
Toast.makeText(getApplicationContext(), country.get(position), Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}