实例介绍
【实例简介】
【实例截图】
【实例截图】
【核心代码】
package com.example.sinvoicedemo; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.os.PowerManager; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.TextView; import android.widget.Toast; import com.libra.sinvoice.LogHelper; import com.libra.sinvoice.SinVoicePlayer; import com.libra.sinvoice.SinVoiceRecognition; public class MainActivity extends Activity implements SinVoiceRecognition.Listener, SinVoicePlayer.Listener { private final static String TAG = "MainActivity"; private final static int MSG_SET_RECG_TEXT = 1; private final static int MSG_RECG_START = 2; private final static int MSG_RECG_END = 3; private final static int MSG_PLAY_TEXT = 4; private final static int TOKEN_LEN = 16; private final static String BAKCUP_LOG_PATH = "/sinvoice_backup"; private final static String CODEBOOK = "abcdefghijklmnopq"; private Handler mHanlder; private SinVoicePlayer mSinVoicePlayer; private SinVoiceRecognition mRecognition; private boolean mIsReadFromFile; private String mSdcardPath; private PowerManager.WakeLock mWakeLock; private TextView mPlayTextView; private TextView mRecognisedTextView; private String mPlayText; static { System.loadLibrary("sinvoice"); LogHelper.d("SinVoice", "sinvoice loadlibraryxxxxxxxxxxxxxxxxxxxxxxx"); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mIsReadFromFile = false; PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, TAG); mSdcardPath = Environment.getExternalStorageDirectory().getPath(); mSinVoicePlayer = new SinVoicePlayer(); mSinVoicePlayer.setListener(this); mRecognition = new SinVoiceRecognition(CODEBOOK); mRecognition.setListener(this); mRecognition.init(); mPlayTextView = (TextView) findViewById(R.id.playtext); mRecognisedTextView = (TextView) findViewById(R.id.regtext); mHanlder = new RegHandler(this); Button playStart = (Button) findViewById(R.id.start_play); playStart.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { mSinVoicePlayer.play(TOKEN_LEN, true, 2000); } }); Button playStop = (Button) findViewById(R.id.stop_play); playStop.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { mSinVoicePlayer.stop(); } }); Button recognitionStart = (Button) findViewById(R.id.start_reg); recognitionStart.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { mRecognition.start(TOKEN_LEN, mIsReadFromFile); } }); Button recognitionStop = (Button) findViewById(R.id.stop_reg); recognitionStop.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { mRecognition.stop(); } }); CheckBox cbReadFromFile = (CheckBox) findViewById(R.id.fread_from_file); cbReadFromFile.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton arg0, boolean isChecked) { mIsReadFromFile = isChecked; } }); Button btBackup = (Button) findViewById(R.id.back_debug_info); btBackup.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { backup(); } }); Button btClearBackup = (Button) findViewById(R.id.clear_debug_info); btClearBackup.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { new AlertDialog.Builder(MainActivity.this).setTitle("information").setMessage("Sure to clear?").setPositiveButton("yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { clearBackup(); } }).setNegativeButton("no", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }).show(); } }); } @Override public void onResume() { super.onResume(); mWakeLock.acquire(); } @Override public void onPause() { super.onPause(); mWakeLock.release(); mSinVoicePlayer.stop(); mRecognition.stop(); } @Override public void onDestroy() { super.onDestroy(); mRecognition.uninit(); mSinVoicePlayer.uninit(); } private void clearBackup() { delete(new File(mSdcardPath BAKCUP_LOG_PATH)); Toast.makeText(this, "clear backup log info successful", Toast.LENGTH_SHORT).show(); } private static void delete(File file) { if (file.isFile()) { file.delete(); return; } if (file.isDirectory()) { File[] childFiles = file.listFiles(); if (childFiles == null || childFiles.length == 0) { file.delete(); return; } for (int i = 0; i < childFiles.length; i ) { delete(childFiles[i]); } file.delete(); } } private void backup() { mRecognition.stop(); String timestamp = getTimestamp(); String destPath = mSdcardPath BAKCUP_LOG_PATH "/back_" timestamp; try { copyDirectiory(destPath, mSdcardPath "/sinvoice"); copyDirectiory(destPath, mSdcardPath "/sinvoice_log"); FileOutputStream fout = new FileOutputStream(destPath "/text.txt"); String str = mPlayTextView.getText().toString(); byte[] bytes = str.getBytes(); fout.write(bytes); str = mRecognisedTextView.getText().toString(); bytes = str.getBytes(); fout.write(bytes); fout.close(); } catch (IOException e) { e.printStackTrace(); } Toast.makeText(this, "backup log info successful", Toast.LENGTH_SHORT).show(); } private static class RegHandler extends Handler { private StringBuilder mTextBuilder = new StringBuilder(); private MainActivity mAct; public RegHandler(MainActivity act) { mAct = act; } @Override public void handleMessage(Message msg) { switch (msg.what) { case MSG_SET_RECG_TEXT: char ch = (char) msg.arg1; mTextBuilder.append(ch); if (null != mAct ) { mAct.mRecognisedTextView.setText(mTextBuilder.toString()); } break; case MSG_RECG_START: mTextBuilder.delete(0, mTextBuilder.length()); break; case MSG_RECG_END: LogHelper.d(TAG, "recognition end"); break; case MSG_PLAY_TEXT: mAct.mPlayTextView.setText(mAct.mPlayText); break; } super.handleMessage(msg); } } @Override public void onRecognitionStart() { mHanlder.sendEmptyMessage(MSG_RECG_START); } @Override public void onRecognition(char ch) { mHanlder.sendMessage(mHanlder.obtainMessage(MSG_SET_RECG_TEXT, ch, 0)); } @Override public void onRecognitionEnd() { mHanlder.sendEmptyMessage(MSG_RECG_END); } @Override public void onPlayStart() { LogHelper.d(TAG, "start play"); } @Override public void onPlayEnd() { LogHelper.d(TAG, "stop play"); } private static String getTimestamp() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss"); return sdf.format(new Date()); } private static void copyFile(File targetFile, File sourceFile) throws IOException { FileInputStream input = new FileInputStream(sourceFile); BufferedInputStream inBuff = new BufferedInputStream(input); FileOutputStream output = new FileOutputStream(targetFile); BufferedOutputStream outBuff = new BufferedOutputStream(output); byte[] b = new byte[1024 * 5]; int len; while ((len = inBuff.read(b)) != -1) { outBuff.write(b, 0, len); } outBuff.flush(); inBuff.close(); outBuff.close(); output.close(); input.close(); } private static void copyDirectiory(String targetDir, String sourceDir) throws IOException { (new File(targetDir)).mkdirs(); File[] file = (new File(sourceDir)).listFiles(); if ( null != file ) { for (int i = 0; i < file.length; i ) { if (file[i].isFile()) { File sourceFile = file[i]; File targetFile = new File(new File(targetDir).getAbsolutePath() File.separator file[i].getName()); copyFile(targetFile, sourceFile); } if (file[i].isDirectory()) { String srcPath = sourceDir "/" file[i].getName(); String targetPath = targetDir "/" file[i].getName(); copyDirectiory(targetPath, srcPath); } } } } @Override public void onSinToken(int[] tokens) { if ( null != tokens ) { StringBuilder sb = new StringBuilder(); for ( int i = 1; i < tokens.length; i ) { sb.append(CODEBOOK.charAt(tokens[i])); } mPlayText = sb.toString(); LogHelper.d(TAG, "onSinToken " mPlayText); mHanlder.sendEmptyMessage(MSG_PLAY_TEXT); } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论