我创建了一个简单的语音识别服务:为此,我创建了一个 android.speech.RecognitionService 的子类,并创建了一个 Activity 来启动和停止该服务。
我的自定义语音识别服务通常使用默认语音识别器,因为我的目标只是了解 RecognitionService 和 RecognitionService.Callback 类的工作原理。
public class SimpleVoiceService extends RecognitionService {
private SpeechRecognizer m_EngineSR;
@Override
public void onCreate() {
super.onCreate();
Log.i("SimpleVoiceService", "Service started");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("SimpleVoiceService", "Service stopped");
}
@Override
protected void onCancel(Callback listener) {
m_EngineSR.cancel();
}
@Override
protected void onStartListening(Intent recognizerIntent, Callback listener) {
m_EngineSR.setRecognitionListener(new VoiceResultsListener(listener));
m_EngineSR.startListening(recognizerIntent);
}
@Override
protected void onStopListening(Callback listener) {
m_EngineSR.stopListening();
}
/**
*
*/
private class VoiceResultsListener implements RecognitionListener {
private Callback m_UserSpecifiedListener;
/**
*
* @param userSpecifiedListener
*/
public VoiceResultsListener(Callback userSpecifiedListener) {
m_UserSpecifiedListener = userSpecifiedListener;
}
@Override
public void onBeginningOfSpeech() {
try {
m_UserSpecifiedListener.beginningOfSpeech();
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onBufferReceived(byte[] buffer) {
try {
m_UserSpecifiedListener.bufferReceived(buffer);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onEndOfSpeech() {
try {
m_UserSpecifiedListener.endOfSpeech();
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onError(int error) {
try {
m_UserSpecifiedListener.error(error);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onEvent(int eventType, Bundle params) { ; }
@Override
public void onPartialResults(Bundle partialResults) {
try {
m_UserSpecifiedListener.partialResults(partialResults);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onReadyForSpeech(Bundle params) {
try {
m_UserSpecifiedListener.readyForSpeech(params);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onResults(Bundle results) {
try {
m_UserSpecifiedListener.results(results);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onRmsChanged(float rmsdB) {
try {
m_UserSpecifiedListener.rmsChanged(rmsdB);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
}
我使用以下 Activity 启动和停止服务。
public class VoiceServiceStarterActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button startButton = new Button(this);
startButton.setText("Start the service");
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { startVoiceService(); }
});
Button stopButton = new Button(this);
stopButton.setText("Stop the service");
stopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { stopVoiceService(); }
});
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(startButton);
layout.addView(stopButton);
setContentView(layout);
}
private void startVoiceService() {
startService(new Intent(this, SimpleVoiceService.class));
}
private void stopVoiceService() {
stopService(new Intent(this, SimpleVoiceService.class));
}
}
最后,我在 AndroidManifest.xml 上声明了我的服务(请参阅 Android SDK 文件夹中的 VoiceRecognition 示例)。
<service android:name="SimpleVoiceService"
android:label="@string/service_name" >
<intent-filter>
<action android:name="android.speech.RecognitionService" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
然后我在 Android 设备上安装了这个应用程序并启动了它: - 当我启动服务时,它正常启动; - 当我停止它时,它会正常停止。
但是如果我在另一个 Activity 中启动以下代码,activities List 仅包含一个元素,即默认语音识别器。
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
为什么系统中存在的语音识别器没有返回我的语音识别器?
最佳答案
如果您希望 queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0) 获取您的 Activity (VoiceServiceStarterActivity),那么您必须在应用的 list 中声明此 Activity 处理 RecognizerIntent.ACTION_RECOGNIZE_SPEECH,像这样
<activity android:name="VoiceServiceStarterActivity">
<intent-filter>
<action android:name="android.speech.action.RECOGNIZE_SPEECH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
...
</activity>
更多具体代码请查看项目Kõnele ( source code ) 本质上是在 Android 上提供语音识别的接口(interface)的开源实现,即它包括:
并使用开源语音识别服务器。
关于android - 如何注册自定义语音识别服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9997720/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack