- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
大家好,我正在尝试将文本转语音功能放入我的 CountDownTimer。我希望它在一定时间后说“还剩 x 秒”。我刚开始使用 TextToSpeech,我不太确定我在做什么..
package com.android.countdown;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.speech.tts.TextToSpeech;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class countdown extends Activity implements TextToSpeech.OnInitListener{
CountDownTimer Counter1;
CountDownTimer Counter2;
CountDownTimer Counter3;
int Interval = 1;
TextToSpeech tts;
public String formatTime(long millis) {
String output = "0:00";
long seconds = millis / 1000;
long minutes = seconds / 60;
seconds = seconds % 60;
minutes = minutes % 60;
String secondsD = String.valueOf(seconds);
String minutesD = String.valueOf(minutes);
if (seconds < 10)
secondsD = "0" + seconds;
if (minutes < 10)
minutesD = "0" + minutes;
output = minutesD + " : " + secondsD;
return output;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Declare Start/Stop timer
Button btnstart = (Button)findViewById(R.id.btnstart);
Button btnstop = (Button)findViewById(R.id.btnstop);
//Text field to show time left
final TextView mCounter1TextField=(TextView)findViewById(R.id.counter1);
final TextView mCounter2TextField = (TextView)findViewById(R.id.counter2);
final TextView mCounter3TextField=(TextView)findViewById(R.id.counter3);
//Counter 1
Counter1 = new CountDownTimer(20000 , Interval) {
public void onTick(long millisUntilFinished){
mCounter1TextField.setText("Seconds left: " + formatTime(millisUntilFinished));
if (millisUntilFinished == 10000) {
instantiate();
}
}
public void onFinish() {
Counter1.start();
}
};
//Counter 2
Counter2 = new CountDownTimer(80000 , Interval) {
public void onTick(long millisUntilFinished) {
mCounter2TextField.setText("Seconds left: " + formatTime(millisUntilFinished));
}
public void onFinish() {
mCounter2TextField.setText("Finished!");
Counter2.start();
}
};
//Counter 3
Counter3 = new CountDownTimer(3000 , Interval) {
public void onTick(long millisUntilFinished) {
mCounter3TextField.setText("Seconds left: " + formatTime(millisUntilFinished));
}
public void onFinish() {
mCounter3TextField.setText("Finished!");
Counter3.start();
}
};
//Start Button
btnstart.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Counter1.start();
Counter2.start();
Counter3.start();
}
});
//Stop Button
btnstop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Counter1.cancel();
Counter2.cancel();
Counter3.cancel();
if (tts != null) {
tts.stop();
tts.shutdown();
}
}
});
}
public void instantiate() {
tts = new TextToSpeech(this, this);
tts.setLanguage(Locale.US);
tts.speak("You have 10 seconds remaining", TextToSpeech.QUEUE_ADD, null);
}
@Override
public void onInit(int status) {
}
@Override
public void onDestroy() {
// Don't forget to shutdown!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
}
最佳答案
tts = new TextToSpeech(this, this)
中的第二个参数没有实现 TextToSpeech.OnInitListener
。
你需要有 countdown
或其他类实现 TextToSpeech.OnInitListener
:
public class countdown extends Activity implements TextToSpeech.OnInitListener {
然后在所述类中实现 onInit():
void onInit(int status){
// implementation
}
最后将实现 OnInitListener 的类传递给 TextToSpeech
构造函数:
// The second 'this' will be replaced with another class if you
// decide to use a class other than countdown to implement the interface.
tts = new TextToSpeech(this, this);
查看 TextToSpeechActivity.java完整工作示例教程。
编辑
如 NickT 所述,您还需要在 onTick 中的 if 语句中添加大括号:
if (millisUntilFinished == 10000) {
tts = new TextToSpeech(this, this);
tts.setLanguage(Locale.US);
tts.speak("You have 10 seconds remaining", TextToSpeech.QUEUE_ADD, null);
}
否则您将始终执行setLanguage
和speak
,这会给您一个NullPointerException
除非millisUntilFinished == 10000
为真。
http://developer.android.com/reference/android/speech/tts/TextToSpeech.OnInitListener.html
关于android - 我的 CountDownTimer 中的文本转语音有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4798189/
我有一个说一些短语的音板应用程序,但是现在我希望能够从男声/女声中改变出来,问题是我不知道该怎么做。任何帮助,将不胜感激。 我正在使用AVFoundation/AVAudioPlayer播放声音。 谢
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 4 年前。
因为我想在后台录制音频,所以我使用了服务..但是我无法在服务中录制音频。 我在 Activity 中尝试了相同的代码,它对我有用。但是如何在输入语音/语音时在后台进行录音,这意味着如果有语音输入就应该
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 6 年前。
我有一个音频流,我会从中提取单词(语音)。因此,例如使用 audio.wav 我会得到 001.wav、002.wav、003.wav 等,其中每个 XXX.wav 是一个词。 我正在寻找一个库或程序
不幸的是,我只能说四种语言,那么如果我知道文本的语言,我如何知道我必须使用哪种 OS X 语音?我在Apple的文档中找不到任何有关它的信息。至少有一张有语音/语言的 table 吗? 最佳答案 您可
有没有办法从命令行使用 MS Speech 实用程序?我可以在 Mac 上完成,但在 Windows XP 上找不到任何引用。 最佳答案 我在这个主题上的 2 美分,命令行单行: 在 Win 上使用
所以我开始了我的不和谐机器人的音乐部分。现在,正如我在上一个问题中所做的那样,这里只是音乐命令的片段:Pastebin #1 在显示 if (msg.member.voiceConnection ==
有谁知道有什么好的 API 或库可以听(语音)文本。我尝试听三种语言的(语音)文本,我想知道最好从哪里开始以及如何开始。我可以对所有三种语言使用通用语音吗?我将使用 eclipse 和 java 作为
首先,我只是一个爱好者,如果这是一个愚蠢的问题或者我太天真了,我很抱歉。 (这也意味着我买不起昂贵的库) 情况是这样的:我正在使用 C#.NET 构建一个简单的语音聊天应用程序(类似于 Ventril
我正在制作一个模块,可以生成和传输语音 IP 数据包。我知道我必须为服务类型字段设置一些值。这个值是多少? 最佳答案 该值应该是x。 关于c - 语音 ip 的服务类型字段集,我们在Stack Ove
有人能帮帮我吗?我使用 SAPI 语音文本,但我不能设置女声,这是代码,它用男声说话,但我想改变它,我想要女声 #include "stdafx.h" using namespace std; voi
我正在寻找一种方法来为一个项目在 Java 中识别预注册的语音命令,但我还没有想出一个好的方法,我研究了快速傅里叶 和处理 wave 文件 的不同方法,但我无法决定我应该如何实现它。 这个想法很简单,
我在 android 的语音识别 API 工作。 我是 Speech Recognition Api 的新手,我的要求是西类牙语语音,并从 Android 的语音识别 API 中获得西类牙语的最佳匹配
我在 Java 中使用一组名为(MaryTTS[实际上还有更多])的库来将 text to speech 转换为该目的,使用以下代码: public class TextToSpeech {
我正在尝试使用webRTC和php作为服务器端来实现单向语音传输。 查看samples ,我无法理解webRTC机制。 在我看来,流程应该是这样的: 调用者和接收者在服务器上注册 接收者监听来电 调用
我的名字是 Joey,我想知道是否有一种在 C++ 中使用语音的方法,如果有人可以给我指出引用资料和书籍,非常感谢...... 最佳答案 你应该看看 Windows Text-To-Speech AP
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我正在使用 Java 语音识别 API - Jarvis,位于 https://github.com/lkuza2/java-speech-api 但是,当我运行应用程序时,出现错误:服务器返回 HT
我们正在做一个需要讲阿拉伯语的项目,我们找到了一个开源工具,Mbrola project , 可以做到这一点。 但是,我还需要一些方法将阿拉伯语文本转换为 SAMPA 语音。那么有人可以帮助我将阿拉伯
我是一名优秀的程序员,十分优秀!