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
chenxupro
- 粉丝: 50
- 资源: 16
最新资源
- Matlab根据flac、pfc或其他软件导出的坐标及应力、位移数据再现云图 案例包括导出在flac6.0中导出位移的fish代码(也可以自己先准备软件导出的坐标数据及对应点的位移或应力数据,可根据需
- 拳皇97.exe拳皇972.exe拳皇973.exe
- 捕鱼达人1.exe捕鱼达人2.exe捕鱼达人3.exe
- 医疗骨折摄像检测29-YOLO(v5至v9)、COCO、CreateML、Darknet、Paligemma数据集合集.rar
- ks滑块加密算法与源代码
- 医护人员检测23-YOLOv8数据集合集.rar
- 1.电力系统短路故障引起电压暂降 2.不对称短路故障分析 包括:共两份自编word+相应matlab模型 1.短路故障的发生频次以及不同类型短路故障严重程度,本文选取三类典型的不对称短路展开研究
- C#连接sap NCO组件 X64版
- 开源基于51单片机的多功能智能闹钟设计,课设毕设借鉴参考
- 深度强化学习电气工程复现文章,适合小白学习 关键词:能量管理 深度学习 强化学习 深度强化学习 能源系统 优化调度 编程语言:python平台 主题:用于能源系统优化调度的深度强化学习算法的性能比较
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈