android中的多线程编程
我是android编程新手。 所以我有一个android中的多线程编程,android,multithreading,text-to-speech,Android,Multithreading,Text To Speech,我是android编程新手。 所以我有一个ArrayList,我想在一个循环中演讲,当我点击一个按钮时,演讲停止,如果我点击同一个按钮,演讲再次开始 我的布局是这样的: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
ArrayList
,我想在一个循环中演讲,当我点击一个按钮时,演讲停止,如果我点击同一个按钮,演讲再次开始
我的布局是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnPauseResume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause" />
</LinearLayout>
public class Activity_test extends Activity {
Button btnPauseResume = null;
boolean IsPaused = false;
private TextToSpeech tts = null;
ArrayList<String> Texts = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
btnPauseResume = (Button) findViewById(R.id.btnPauseResume);
Texts = new ArrayList<String>();
btnPauseResume.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
Activity_test.this.runOnUiThread(new Runnable() {
@Override
public void run() {
IsPaused = !IsPaused;
if (IsPaused) {
btnPauseResume.setText("Resume");
} else {
btnPauseResume.setText("Pause");
Start();
}
}
});
}
}).start();
}
});
Start();
}
public void Start() {
new Thread(new Runnable() {
public void run() {
Texts.clear();
Texts.add("Long Text 1");
Texts.add("Long Text 2");
Texts.add("Long Text 3");
Speech();
}
}).start();
}
public void Speech() {
tts = new TextToSpeech(Activity_test.this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.ENGLISH);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(Activity_test.this, "This Language is not supported", Toast.LENGTH_SHORT).show();
} else {
for (String Text : Texts) {
if (!IsPaused) {
tts.speak(Text, TextToSpeech.SUCCESS, null);
while (tts.isSpeaking()) {
//wait till speech finish
}
}
}
if (!IsPaused) {
Start();
}
}
} else
Toast.makeText(Activity_test.this, "Initilization Failed!", Toast.LENGTH_SHORT).show();
}
});
}
}
我的java代码如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnPauseResume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause" />
</LinearLayout>
public class Activity_test extends Activity {
Button btnPauseResume = null;
boolean IsPaused = false;
private TextToSpeech tts = null;
ArrayList<String> Texts = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
btnPauseResume = (Button) findViewById(R.id.btnPauseResume);
Texts = new ArrayList<String>();
btnPauseResume.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
Activity_test.this.runOnUiThread(new Runnable() {
@Override
public void run() {
IsPaused = !IsPaused;
if (IsPaused) {
btnPauseResume.setText("Resume");
} else {
btnPauseResume.setText("Pause");
Start();
}
}
});
}
}).start();
}
});
Start();
}
public void Start() {
new Thread(new Runnable() {
public void run() {
Texts.clear();
Texts.add("Long Text 1");
Texts.add("Long Text 2");
Texts.add("Long Text 3");
Speech();
}
}).start();
}
public void Speech() {
tts = new TextToSpeech(Activity_test.this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.ENGLISH);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(Activity_test.this, "This Language is not supported", Toast.LENGTH_SHORT).show();
} else {
for (String Text : Texts) {
if (!IsPaused) {
tts.speak(Text, TextToSpeech.SUCCESS, null);
while (tts.isSpeaking()) {
//wait till speech finish
}
}
}
if (!IsPaused) {
Start();
}
}
} else
Toast.makeText(Activity_test.this, "Initilization Failed!", Toast.LENGTH_SHORT).show();
}
});
}
}
公共类活动\u测试扩展活动{
按钮btnPauseResume=null;
布尔值IsPaused=false;
私有texttospeechtts=null;
ArrayList文本=null;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_测试);
btnPauseResume=(按钮)findViewById(R.id.btnPauseResume);
text=新的ArrayList();
btnPauseResume.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
新线程(newrunnable()){
公开募捐{
Activity\u test.this.rununuithread(新的Runnable()命令){
@凌驾
公开募捐{
IsPaused=!IsPaused;
如果(i暂停){
btnPauseResume.setText(“简历”);
}否则{
btnPauseResume.setText(“暂停”);
Start();
}
}
});
}
}).start();
}
});
Start();
}
公开作废开始(){
新线程(newrunnable()){
公开募捐{
文本。清除();
文本。添加(“长文本1”);
文本。添加(“长文本2”);
文本。添加(“长文本3”);
言语();
}
}).start();
}
公开演说{
tts=new TextToSpeech(Activity\u test.this,new TextToSpeech.OnInitListener(){
@凌驾
公共无效onInit(int状态){
//TODO自动生成的方法存根
if(status==TextToSpeech.SUCCESS){
int result=tts.setLanguage(Locale.ENGLISH);
if(result==TextToSpeech.LANG_缺少数据| | result==TextToSpeech.LANG_不受支持){
Toast.makeText(Activity_test.this,“此语言不受支持”,Toast.LENGTH_SHORT.show();
}否则{
用于(字符串文本:文本){
如果(!i暂停){
tts.speak(Text,TextToSpeech.SUCCESS,null);
while(tts.isSpeaking()){
//等到演讲结束
}
}
}
如果(!i暂停){
Start();
}
}
}否则
Toast.makeText(Activity_test.this,“初始化失败!”,Toast.LENGTH_SHORT.show();
}
});
}
}
在real code中,文本的数组列表被更改,因此这就是我调用方法Speach()
将新的ArrayList
加载到speech的原因
因此,问题是当我单击按钮时,它不起作用(返回按钮也不起作用)。尝试用处理程序和可运行程序替换线程。 看
我还要确保OnClickListener中的代码被实际调用(使用debug/log)。您需要所有这些线程做什么?我曾尝试在文本说话时单击一个按钮,但它无论如何都不起作用。因此我尝试了所有类型的线程,结果都是一样的。我的代码正确吗?有更好的解决方案吗?而(tts.isSpeaking()){//wait Toll speech finish}似乎是错误的,因为您正在阻止执行。可能有一个侦听器会在语音完成时告诉您。您可能应该使用SetOnAtternanceProgressListener()。我非常确定您不需要所有线程。请确保您不阻止UI线程。