如何检测android中是否有声音?
我是android开发新手。我需要制作一个能够检测声音并播放音乐的应用程序。我已经注意到了音乐播放部分,但问题是我不知道如何设置一个条件来检查是否有声音 Im使用eclipse和android API 20以及语音识别。请看下面的代码如何检测android中是否有声音?,android,audio,voice-recognition,Android,Audio,Voice Recognition,我是android开发新手。我需要制作一个能够检测声音并播放音乐的应用程序。我已经注意到了音乐播放部分,但问题是我不知道如何设置一个条件来检查是否有声音 Im使用eclipse和android API 20以及语音识别。请看下面的代码 import android.app.Activity; import android.os.Bundle; import android.content.Intent; import android.content.pm.PackageManager; impo
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
/**
* A very simple application to handle Voice Recognition intents
* and display the results
*/
public class MainActivity extends Activity
{
private static final int REQUEST_CODE = 1234;
private ListView wordsList;
/**
* Called with the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.voice_recog);
Button speakButton = (Button) findViewById(R.id.speakButton);
wordsList = (ListView) findViewById(R.id.list);
// Disable button if no recognition service is present
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() == 0)
{
speakButton.setEnabled(false);
speakButton.setText("Recognizer not present");
}
else
{startVoiceRecognitionActivity();}
}
/**
* Handle the action of the button being clicked
*/
/**
* Fire an intent to start the voice recognition activity.
*/
private void startVoiceRecognitionActivity()
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice recognition Demo...");
startActivityForResult(intent, REQUEST_CODE);
}
/**
* Handle the results from the voice recognition activity.
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
{
// Populate the wordsList with the String values the recognition engine thought it heard
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
wordsList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
matches));
}
super.onActivityResult(requestCode, resultCode, data);
}
}
导入android.app.Activity;
导入android.os.Bundle;
导入android.content.Intent;
导入android.content.pm.PackageManager;
导入android.content.pm.ResolveInfo;
导入android.speech.RecognizerIntent;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.ListView;
导入java.util.ArrayList;
导入java.util.List;
/**
*一个处理语音识别意图的非常简单的应用程序
*并显示结果
*/
公共类MainActivity扩展了活动
{
私有静态最终整数请求_代码=1234;
私有列表视图单词列表;
/**
*首先创建使用活动调用的。
*/
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(右布局、语音记录);
按钮speakButton=(按钮)findViewById(R.id.speakButton);
wordsList=(ListView)findViewById(R.id.list);
//如果没有识别服务,则禁用按钮
PackageManager pm=getPackageManager();
列出活动=pm.querytentActivities(
新意图(识别者意图、行动、识别、言语),0);
如果(activities.size()==0)
{
speakButton.setEnabled(假);
speakButton.setText(“识别器不存在”);
}
其他的
{startVoiceRecognitionActivity();}
}
/**
*处理正在单击的按钮的操作
*/
/**
*激发启动语音识别活动的意图。
*/
私有void startVoiceRecognitionActivity()
{
意向意向=新意向(识别意向、行动、识别言语);
intent.putExtra(识别器intent.EXTRA_语言_模型,
识别者意图、语言、模型、自由形式);
intent.putExtra(RecognizerIntent.EXTRA_提示符,“语音识别演示…”);
startActivityForResult(意图、请求代码);
}
/**
*处理语音识别活动的结果。
*/
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据)
{
if(requestCode==REQUEST\u CODE&&resultCode==RESULT\u OK)
{
//用识别引擎认为听到的字符串值填充wordsList
ArrayList matches=data.getStringArrayListExtra(
识别者意图。额外结果);
setAdapter(新的ArrayAdapter)(这个,android.R.layout.simple\u list\u item\u 1,
火柴);
}
super.onActivityResult(请求代码、结果代码、数据);
}
}
寻找语音识别的可能副本是一件错误的事情,关于声音检测有很多问题,您可以使用搜索来解决它们。@puffles。我很高兴你也在寻找我的答案!你找到答案了吗?你能告诉我你的经验吗?