package prox.AndroidPocketSphinxPro;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.pm.ActivityInfo;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
//Android自身录音噪声太大!!!
public class AndroidPocketSphinxPro extends Activity
{
private Button StartRecordButton=null;
//it needs to be a single-channel (monaural),
//little-endian, unheadered 16-bit signed PCM audio file sampled at 16000 Hz.
static final int frequency = 16000;
static final int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
int recBufSize=0;
private AudioRecord audioRecord=null;
int playBufSize=0;
private AudioTrack audioTrack=null;
public MediaPlayer AnswerPlayer=null;
public Handler AudioRecordHandler=null;
public Runnable AudioRecordTask=null;
public boolean AudioRecordTimerRun=false;
public String AudioRecogResPath="/sdcard/VoiceRecogPro";
public String RawFilePath="/sdcard/VoiceRecogPro/TmpAudioFile.raw";
public File RawFile = null;
public FileOutputStream RawFOStream=null;
public BufferedOutputStream RawBOStream=null;
public InputStream AnswerIStream = null;
public FileInputStream AnswerFIStream=null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
//默认竖屏!!!
super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
recBufSize = AudioRecord.getMinBufferSize(frequency,channelConfiguration, audioEncoding);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency, channelConfiguration, audioEncoding, recBufSize);
playBufSize=AudioTrack.getMinBufferSize(frequency,channelConfiguration,audioEncoding);
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,frequency,channelConfiguration,audioEncoding,playBufSize,AudioTrack.MODE_STREAM);
//--->>>
//AnswerPlayer=new MediaPlayer();
StartRecordButton=(Button)findViewById(R.id.StartRecordButton);
StartRecordButton.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
AudioRecordPD=new ProgressDialog(AndroidPocketSphinxPro.this);
AudioRecordPD.setMessage("Please Speak Out Your Command...");
AudioRecordPD.show();
new AudioRecordTask().execute("Start");
}
});
AudioRecordHandler = new Handler();
AudioRecordTask = new Runnable()
{
public void run()
{
//Stop!!!
if (AudioRecordTimerRun)
{
//HideList();
AudioRecordTimerRun = false;
}
}
};
//Toast.makeText(AndroidPocketSphinxPro.this, VoiceRecogByRawFile(RawFilePath), Toast.LENGTH_SHORT).show();
}
static
{
System.loadLibrary("AndroidPocketSphinx_jni");
}
public native String GetTestValue(int TestParam);
//VoiceRecogByRawFile
public native String VoiceRecogByRawFile(String CurrentRawFilePath);
//录小Q的声音播放!!!计时3秒!!!直接用raw进去就能认!!!->数3秒钟把它关掉!!!直接用linux播放即可!!!
//AudioRecordRecog
private ProgressDialog AudioRecordPD=null;
private ProgressDialog AudioRecogPD=null;
// 设置三种类型参数分别为String,Integer,String
class AudioRecordTask extends AsyncTask<String, Integer, String>
{
// 可变长的输入参数,与AsyncTask.exucute()对应
@Override
protected String doInBackground(String... params)
{
//Start!!!
//Create New Raw File
RawFile=new File(RawFilePath);
try
{
RawFOStream = new FileOutputStream(RawFile);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
RawBOStream = new BufferedOutputStream(RawFOStream);
byte[] RecordBuffer = new byte[recBufSize];
audioRecord.startRecording();
AudioRecordHandler.removeCallbacks(AudioRecordTask);
AudioRecordTimerRun = true;
AudioRecordHandler.postDelayed(AudioRecordTask, 5000);
while(AudioRecordTimerRun)
{
int bufferReadResult = audioRecord.read(RecordBuffer, 0,recBufSize);
Log.d("bufferReadResult", String.valueOf(bufferReadResult));
byte[] TmpBuf = new byte[bufferReadResult];
System.arraycopy(RecordBuffer, 0, TmpBuf, 0,bufferReadResult);
try
{
RawBOStream.write(TmpBuf, 0, bufferReadResult);
}
catch (IOException e)
{
e.printStackTrace();
}
}
try
{
RawBOStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
RawFOStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
audioRecord.stop();
AudioRecordPD.dismiss();
//VoiceRecogByRawFile(RawFilePath);
return "OK!";
}
@Override
protected void onCancelled()
{
super.onCancelled();
}
@Override
protected void onPostExecute(String result)
{
AudioRecogPD=new ProgressDialog(AndroidPocketSphinxPro.this);
AudioRecogPD.setMessage("Recoging...");
AudioRecogPD.show();
new AudioRecogTask().execute("Start");
}
@Override
protected void onPreExecute()
{
//任务启动
}
@Override
protected void onProgressUpdate(Integer... values)
{
//更新进度
}
}
// 设置三种类型参数分别为String,Integer,String
class AudioRecogTask extends AsyncTask<String, Integer, String>
{
// 可变长的输入参数,与AsyncTask.exucute()对应
@Override
protected String doInBackground(String... params)
{
String RecogResult=VoiceRecogByRawFile(RawFilePath);
return RecogResult;
}
@Override
protected void onCancelled()
{
super.onCancelled();
}
@Override
protected void onPostExecute(String result)
{
AudioRecogPD.dismiss();
String RecogFinalResult=result;
Log.d("chenxupro", RecogFinalResult);
Toast.makeText(AndroidPocketSphinxPro.this, RecogFinalResult, Toast.LENGTH_SHORT).show();
//继续!!!
AnswerPlayer=new MediaPlayer();
if(AnswerPlayer.isPlaying()==true)
{
AnswerPlayer.reset();
}
try
{
//MediaPlayer错误!!!
AnswerPlayer.setDataSource(AudioRecogResPath+RecogFinalResult);
//AnswerPlayer.setDataSource("/sdcard/MyMusics/WindowsXP.wav");
AnswerPlayer.prepare();
AnswerPlayer.start();
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
catch (IllegalStateException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
/*
//AnswerFIStream
byte[] AnswerFileBuf=new byte[1600];
int AnswerFileBufBytesRead=0;
boolean ReadFileNotFinishFlag=true;
try
{
AnswerFIStream = new FileInputStream(AudioRecogResPath+RecogFinalResult);
//Skip 44 bytes Wave Header
AnswerFIStream.skip(44);
while(ReadFileNotFinishFlag)
{
AnswerFileBufBytesRead=AnswerFIStream.re
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ProxAndroidSphinx.tar.gz (3个子文件)
ProxAndroidSphinx
Android.mk 4KB
AndroidPocketSphinx.c 5KB
AndroidPocketSphinxPro.java 9KB
共 3 条
- 1
资源评论
- LoveAnnuoa2013-05-29作用不是很大。
chenxupro
- 粉丝: 50
- 资源: 16
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功