Java tutorial
/* * Copyright 2013 Niek Haarman * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.tt.engtrain.showcontent; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import android.animation.Animator; import android.animation.Animator.AnimatorListener; import android.animation.ObjectAnimator; import android.animation.ValueAnimator; import android.animation.ValueAnimator.AnimatorUpdateListener; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.media.AudioFormat; import android.media.AudioManager; import android.media.AudioTrack; import android.os.Bundle; import android.os.Environment; import android.support.v4.app.FragmentActivity; import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.iflytek.cloud.ErrorCode; import com.iflytek.cloud.EvaluatorListener; import com.iflytek.cloud.EvaluatorResult; import com.iflytek.cloud.InitListener; import com.iflytek.cloud.SpeechConstant; import com.iflytek.cloud.SpeechError; import com.iflytek.cloud.SpeechEvaluator; import com.iflytek.cloud.SpeechSynthesizer; import com.iflytek.cloud.SynthesizerListener; import com.iflytek.ise.result.Result; import com.iflytek.ise.result.entity.Sentence; import com.iflytek.ise.result.entity.Word; import com.iflytek.ise.result.util.ResultTranslateUtil; import com.iflytek.speech.util.XmlResultParser; import com.nhaarman.listviewanimations.itemmanipulation.ExpandableListItemAdapter; import com.nhaarman.listviewanimations.swinginadapters.prepared.SwingBottomInAnimationAdapter; import com.pascalwelsch.holocircularprogressbar.HoloCircularProgressBar; import com.tt.engtrain.R; public class ContentListItemActivity extends FragmentActivity { private ListView mListView; private ArrayList<ItemMode> mModelist; private MyExpandableListItemAdapter mExpandableListItemAdapter; private static String TAG = "TtsDemo"; private String pcmpath = Environment.getExternalStorageDirectory() + "/engtrain/"; private static int selectedNumCloud = 0; private static int selectedNumLocal = 0; // ? // private SpeechRecognizer mIat; // ?UI // private RecognizerDialog iatDialog; // ? private EditText mResultText; int ret = 0;// // ?? private SpeechSynthesizer mTts; private AudioTrack audioTrack; // ? public static String voicerCloud = "catherine"; // ? public static String voicerLocal = "xiaoyan"; // ? // ? // // catherine // // // // ? // // henry // // // // ? // // vimary // ? // // // private int mPercentForBuffering = 0; // // // private int mPercentForPlaying = 0; private Toast mToast; private SpeechEvaluator mSpeechEvaluator;// // private SharedPreferences mSharedPreferences; public static void actionToExpandableList(Context context) { Intent intent = new Intent(context, ContentListItemActivity.class); context.startActivity(intent); } public static ArrayList<Integer> getItems() { ArrayList<Integer> items = new ArrayList<Integer>(); for (int i = 0; i < 1000; i++) { items.add(i); } return items; } @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_baselist); mSpeechEvaluator = SpeechEvaluator.createEvaluator(ContentListItemActivity.this, null); mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT); int iMinBufSize = AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT); audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_STEREO, AudioFormat.ENCODING_PCM_16BIT, iMinBufSize, AudioTrack.MODE_STREAM); mTts = SpeechSynthesizer.createSynthesizer(this, mTtsInitListener); initData(); initListView(); // initspker(); } private void initData() { // TODO Auto-generated method stub mModelist = new ArrayList<ItemMode>(); mModelist.add(new ItemMode( "One is always on a strange road, watching strange scenery and listening to strange music. Then one day, you will find that the things you try hard to forget are already gone. ", "?????????", 0, true)); mModelist.add(new ItemMode( "Happiness is not about being immortal nor having food or rights in one's hand. Its about having each tiny wish come true, or having something to eat when you are hungry or having someone's love when you need love. ", "??????????????? ", 0, false)); mModelist.add(new ItemMode( "Love is a lamp, while friendship is the shadow. When the lamp is off,you will find the shadow everywhere. Friend is who can give you strength at last. ", "??????????? ", 0, true)); mModelist.add(new ItemMode("I love you not for who you are, but for who I am before you. ", "??????", 20, false)); mModelist.add(new ItemMode("Love makes man grow up or sink down. ", "???? ", 30, true)); mModelist.add(new ItemMode( ".If you can hold something up and put it down, it is called weight-lifting; if you can hold something up but can never put it down,it's called burden-bearing. Pitifully, most of people are bearing heavy burdens when they are in love.", "???????", 100, true)); mModelist.add( new ItemMode("A fall into the pit, a gain in your wit.", "?", 0, false)); mModelist.add(new ItemMode("A fair face may hide a foul heart.", "??", 0, true)); mModelist.add(new ItemMode("Stay put", "???", 0, false)); mModelist.add(new ItemMode("get the drift", "", 60, true)); mModelist.add(new ItemMode("pistachio", "", 0, false)); mModelist.add(new ItemMode(" I dont think much of the movie ", " ?", 89, true)); } private void initListView() { mListView = (ListView) findViewById(R.id.activity_baselist_listview); mListView.setDivider(null); mExpandableListItemAdapter = new MyExpandableListItemAdapter(this, mModelist); mExpandableListItemAdapter.setLimit(1); SwingBottomInAnimationAdapter swingBottomInAnimationAdapter = new SwingBottomInAnimationAdapter( mExpandableListItemAdapter); swingBottomInAnimationAdapter.setAbsListView(mListView); swingBottomInAnimationAdapter.setInitialDelayMillis(500); mListView.setAdapter(swingBottomInAnimationAdapter); } // private void initspker() { // mIat = SpeechRecognizer.createRecognizer(this, mTtsInitListener); // // ??Dialog,?UI?,SpeechRecognizer // iatDialog = new RecognizerDialog(this, mTtsInitListener); // // // // mSharedPreferences = getSharedPreferences(IatSettings.PREFER_NAME, // // Activity.MODE_PRIVATE); // // mResultText = ((EditText) findViewById(R.id.iat_text)); // // ### ? // mTts = SpeechSynthesizer.createSynthesizer(this, mTtsInitListener); // // } // public void startListening(ItemMode mode) { if (mSpeechEvaluator == null) { return; } setIseParams(mode); mSpeechEvaluator.startEvaluating(mode.getEngcontent(), null, new CustomEvaluatorListener(mode)); // ######## ? ? } // ?? public void startTts(String text, boolean ismale, com.tt.engtrain.showcontent.ContentListItemActivity.MyExpandableListItemAdapter.ContentViewHolder viewHolder) { text += ""; // ? setTtsParam(ismale); int code = mTts.startSpeaking(text, new CustomSynthesizerListener(viewHolder)); if (code != ErrorCode.SUCCESS) { showTip("??,?: " + code); } } private void playSelfRecorde(final String path) {// pcm new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub if (!(audioTrack.getPlayState() == AudioTrack.PLAYSTATE_STOPPED)) { audioTrack.stop(); return; } audioTrack.play(); byte[] data = getBytes(path); audioTrack.write(data, 0, data.length); audioTrack.stop(); } }).start(); } public static byte[] getBytes(String filePath) {// file to bytes[] byte[] buffer = null; try { File file = new File(filePath); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(1000); byte[] b = new byte[1000]; int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } fis.close(); bos.close(); buffer = bos.toByteArray(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buffer; } // adabper private class MyExpandableListItemAdapter extends ExpandableListItemAdapter<Integer> { private final Context mContext; private LayoutInflater inflater; private ArrayList<ItemMode> mModelist; /** * Creates a new ExpandableListItemAdapter with the specified list, or * an empty list if items == null. */ private MyExpandableListItemAdapter(final Context context, List mModelist) { super(context, R.layout.item_expandablelistitem_card, R.id.activity_expandablelistitem_card_title, R.id.activity_expandablelistitem_card_content, mModelist); mContext = context; inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.mModelist = (ArrayList<ItemMode>) mModelist; } @Override public View getTitleView(final int position, View convertView, final ViewGroup parent) { if (convertView == null) { convertView = inflater.inflate(R.layout.item_expandablelistitem_title, parent, false); convertView.setTag(new TitleViewHolder(convertView)); } TitleViewHolder viewHolder = (TitleViewHolder) convertView.getTag(); viewHolder.setData(mModelist.get(position), position); return convertView; } @Override public View getContentView(final int position, View convertView, final ViewGroup parent) { if (convertView == null) { convertView = inflater.inflate(R.layout.item_expandablelistitem_content, parent, false); convertView.setTag(new ContentViewHolder(convertView)); } ContentViewHolder viewHolder = (ContentViewHolder) convertView.getTag(); viewHolder.setData(mModelist.get(position), position); return convertView; } /** * @author qiao ?? * */ class TitleViewHolder { View parent; TextView engcontentTv; TextView chincontentTv; TextView scoreTv; ImageView headImageView; ItemMode mMode; int mPostion; TitleViewHolder(View view) { parent = view; engcontentTv = (TextView) parent.findViewById(R.id.engtitle_tv); chincontentTv = (TextView) parent.findViewById(R.id.chinestitle_tv); headImageView = (ImageView) parent.findViewById(R.id.imageview_head); scoreTv = (TextView) parent.findViewById(R.id.score_tv); } private void setData(ItemMode mode, int postion) { this.mMode = mode; mPostion = postion; mode.setTitleview(parent); engcontentTv.setText(mode.getEngcontentBuilder()); chincontentTv.setText(mode.getChincontent()); if (mode.getScore() == 100) { scoreTv.setBackgroundResource(R.drawable.circle_10_red); } else if (mode.getScore() >= 60.0) { scoreTv.setBackgroundResource(R.drawable.circle_10_green); } else { scoreTv.setBackgroundResource(R.drawable.circle_10_gray); } scoreTv.setText(mode.getScore() + ""); if (mode.isIsfamle()) { headImageView.setImageResource(R.drawable.fmale); chincontentTv.setTextColor(Color.rgb(251, 21, 126)); } else { headImageView.setImageResource(R.drawable.male); chincontentTv.setTextColor(Color.rgb(42, 168, 240)); } } }; class ContentViewHolder {// == View parent; HoloCircularProgressBar recodepBar; // TextView playRecoderButton; Button playSelfRecoderButton; ItemMode mMode; int mPostion; int speekstate = 0; // 0 1 2 ? HoloCircularProgressBar progressBar; ContentViewHolder(View view) { parent = view; recodepBar = (HoloCircularProgressBar) view.findViewById(R.id.lessen_pgb); recodepBar.setThumbEnabled(false); playSelfRecoderButton = (Button) view.findViewById(R.id.recoderself_btn); progressBar = (HoloCircularProgressBar) view.findViewById(R.id.recoder_progress); progressBar.setProgress(0); } private void setData(ItemMode mode, int postion) { this.mMode = mode; mPostion = postion; mode.setContentView(parent); recodepBar.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { if (mTts.isSpeaking()) { progressBar.setBackgroundResource(R.drawable.pause); mTts.resumeSpeaking(); speekstate = 1; } startListening(mMode); } }); progressBar.setBackgroundResource(R.drawable.play); progressBar.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { if (!mTts.isSpeaking() || speekstate == 0) { progressBar.setBackgroundResource(R.drawable.pause); startTts(mMode.getEngcontent(), !mMode.isIsfamle(), ContentViewHolder.this); speekstate = 1; } else if (speekstate == 1) { progressBar.setBackgroundResource(R.drawable.play); mTts.pauseSpeaking(); speekstate = 2; } else if (speekstate == 2) { progressBar.setBackgroundResource(R.drawable.pause); mTts.resumeSpeaking(); speekstate = 1; } } }); playSelfRecoderButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub if (new File(pcmpath + "recode" + mMode.getTitle()).exists()) { playSelfRecorde(pcmpath + "recode" + mMode.getTitle()); } else { showTip(""); } } }); } } } /** * ?? */ private InitListener mTtsInitListener = new InitListener() { @Override public void onInit(int code) { Log.d(TAG, "InitListener init() code = " + code); if (code != ErrorCode.SUCCESS) { showTip("?,?" + code); } } }; private void showTip(final String str) { runOnUiThread(new Runnable() { @Override public void run() { mToast.setText(str + ""); mToast.show(); } }); } /** * ? * * @param param * @return */ private void setTtsParam(boolean ismale) { // ? mTts.setParameter(SpeechConstant.PARAMS, null); // ?? // if (mEngineType.equals(SpeechConstant.TYPE_CLOUD)) { // mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); // ? if (ismale) { mTts.setParameter(SpeechConstant.VOICE_NAME, "henry"); } else { mTts.setParameter(SpeechConstant.VOICE_NAME, "vimary"); } mTts.setParameter(SpeechConstant.SPEED, "50"); // mTts.setParameter(SpeechConstant.PITCH, "50"); // ? mTts.setParameter(SpeechConstant.VOLUME, "90"); // ? mTts.setParameter(SpeechConstant.STREAM_TYPE, "3"); } /** * @param mode * ? */ private void setIseParams(ItemMode mode) { mSpeechEvaluator.setParameter(SpeechConstant.LANGUAGE, "en_us"); mSpeechEvaluator.setParameter(SpeechConstant.ISE_CATEGORY, "read_sentence"); mSpeechEvaluator.setParameter(SpeechConstant.TEXT_ENCODING, "utf-8"); mSpeechEvaluator.setParameter(SpeechConstant.VAD_BOS, "5000"); mSpeechEvaluator.setParameter(SpeechConstant.VAD_EOS, "1800"); mSpeechEvaluator.setParameter(SpeechConstant.KEY_SPEECH_TIMEOUT, "-1"); mSpeechEvaluator.setParameter(SpeechConstant.RESULT_LEVEL, "complete"); // ???pcmsd??WRITE_EXTERNAL_STORAGE?? mSpeechEvaluator.setParameter(SpeechConstant.ISE_AUDIO_PATH, pcmpath + "recode" + mode.getTitle()); } class CustomSynthesizerListener implements SynthesizerListener { com.tt.engtrain.showcontent.ContentListItemActivity.MyExpandableListItemAdapter.ContentViewHolder mViewHolder; public CustomSynthesizerListener( com.tt.engtrain.showcontent.ContentListItemActivity.MyExpandableListItemAdapter.ContentViewHolder viewHolder) { mViewHolder = viewHolder; } @Override public void onSpeakBegin() { // mViewHolder.playRecoderButton.setText("?"); mViewHolder.progressBar.setBackgroundResource(R.drawable.pause); showTip(""); } @Override public void onSpeakPaused() { // mViewHolder.playRecoderButton.setText("");\ mViewHolder.progressBar.setBackgroundResource(R.drawable.play); showTip("?"); } @Override public void onSpeakResumed() { mViewHolder.progressBar.setBackgroundResource(R.drawable.pause); // mViewHolder.playRecoderButton.setText("?"); showTip(""); } @Override public void onBufferProgress(int percent, int beginPos, int endPos, String info) { // mPercentForBuffering = percent; // mToast.setText(String.format(getString(R.string.tts_toast_format), // mPercentForBuffering, mPercentForPlaying)); // mViewHolder.progressBar.setProgress(mPercentForPlaying); // mToast.show(); } @Override public void onSpeakProgress(int percent, int beginPos, int endPos) { // mPercentForPlaying = percent; // showTip(String.format(getString(R.string.tts_toast_format), // mPercentForBuffering, mPercentForPlaying)); if (mProgressBarAnimator != null) { mProgressBarAnimator.cancel(); } animate(mViewHolder.progressBar, null, percent / 100.0f, 50); // mViewHolder.progressBar.setProgress(percent / 100.0f); } @Override public void onCompleted(SpeechError error) { if (error == null) { mViewHolder.progressBar.setBackgroundResource(R.drawable.play); // mViewHolder.playRecoderButton.setText(""); // mViewHolder.progressBar.setProgress(0); if (mProgressBarAnimator != null) { mProgressBarAnimator.cancel(); } animate(mViewHolder.progressBar, null, 0, 1000); } else if (error != null) { showTip(error.getPlainDescription(true)); } } @Override public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) { // TODO Auto-generated method stub } } // ########## class CustomEvaluatorListener implements EvaluatorListener { HoloCircularProgressBar progressBar; ItemMode mMode; long time = 0; int beforvolume; TextView scoretv; CustomEvaluatorListener(ItemMode mode) { mMode = mode; progressBar = (HoloCircularProgressBar) mMode.getContentView().findViewById(R.id.lessen_pgb); scoretv = (TextView) mMode.getTitleview().findViewById(R.id.score_tv); } @Override public void onResult(EvaluatorResult result, boolean isLast) { Log.d(TAG, "evaluator result :" + isLast); if (isLast) { StringBuilder builder = new StringBuilder(); builder.append(result.getResultString()); String lastResult = builder.toString(); // ? if (!TextUtils.isEmpty(lastResult)) { XmlResultParser resultParser = new XmlResultParser(); Result result1 = resultParser.parse(lastResult); if (null != result1) { if (null == result1.sentences) { return; } for (Sentence sentence : result1.sentences) { if ("".equals(ResultTranslateUtil.getContent(sentence.content)) || "?".equals(ResultTranslateUtil.getContent(sentence.content))) { continue; } if (null == sentence.words) { continue; } StringBuffer buffer = new StringBuffer(); for (Word word : sentence.words) { if ("".equals(ResultTranslateUtil.getContent(word.content)) || "?".equals(ResultTranslateUtil.getContent(word.content))) { continue; } buffer.append("\n??[" + ResultTranslateUtil.getContent(word.content) + "] ") .append("" + ResultTranslateUtil.getDpMessageInfo(word.dp_message)) .append(" " + word.total_score); ResultTranslateUtil.getContent(word.content); float f = word.total_score; // if (null == word.sylls) { // buffer.append("\n"); // continue; // } // for (Syll syll : word.sylls) { // buffer.append("\n[" + // ResultTranslateUtil.getContent(syll.getStdSymbol()) // + "] "); // if (null == syll.phones) { // continue; // } // // for (Phone phone : syll.phones) { // buffer.append("\n\t[" + // ResultTranslateUtil.getContent(phone.getStdSymbol()) // + "] ").append(" " + // ResultTranslateUtil.getDpMessageInfo(phone.dp_message)); // } // // } // buffer.append("\n"); } } mMode.setScore((int) (result1.total_score * 20)); scoretv.setText(mMode.getScore() + ""); } else { showTip("?"); } } showTip("?"); } progressBar.setProgress(0); progressBar.setThumbEnabled(false); } @Override public void onError(SpeechError error) { // mIseStartButton.setEnabled(true); // if (error != null) { // showTip("error:" + error.getErrorCode() + "," + // error.getErrorDescription()); // mResultEditText.setText(""); // mResultEditText.setHint("?"); // } else { // Log.d(TAG, "evaluator over"); // } progressBar.setThumbEnabled(false); showTip(""); } @Override public void onBeginOfSpeech() { Log.d(TAG, "evaluator begin"); showTip("evaluator begin"); progressBar.setThumbEnabled(true); } @Override public void onEndOfSpeech() { Log.d(TAG, "evaluator stoped"); showTip("evaluator stoped"); progressBar.setThumbEnabled(false); } @Override public void onVolumeChanged(int volume) { showTip("??" + volume); progressBar.setThumbEnabled(true); if (Calendar.getInstance().getTimeInMillis() - time > 500) { if (mProgressBarAnimator != null) { mProgressBarAnimator.cancel(); } animate(progressBar, null, volume * 3f / 100.0f, 500); time = Calendar.getInstance().getTimeInMillis(); } else if (Math.abs(beforvolume - volume) > 5) { beforvolume = volume; if (mProgressBarAnimator != null) { mProgressBarAnimator.cancel(); } animate(progressBar, null, volume * 3f / 100.0f, 500); time = Calendar.getInstance().getTimeInMillis(); } } @Override public void onEvent(int eventType, int arg1, int arg2, Bundle obj) { // TODO Auto-generated method stub } } // ############# ? private ObjectAnimator mProgressBarAnimator; /** * Animate. * * @param progressBar * the progress bar * @param listener * the listener */ private void animate(final HoloCircularProgressBar progressBar, final AnimatorListener listener, final float progress, final int duration) { mProgressBarAnimator = ObjectAnimator.ofFloat(progressBar, "progress", progress); mProgressBarAnimator.setDuration(duration); mProgressBarAnimator.addListener(new AnimatorListener() { @Override public void onAnimationCancel(final Animator animation) { } @Override public void onAnimationEnd(final Animator animation) { progressBar.setProgress(progress); } @Override public void onAnimationRepeat(final Animator animation) { } @Override public void onAnimationStart(final Animator animation) { } }); if (listener != null) { mProgressBarAnimator.addListener(listener); } mProgressBarAnimator.reverse(); mProgressBarAnimator.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(final ValueAnimator animation) { progressBar.setProgress((Float) animation.getAnimatedValue()); } }); progressBar.setMarkerProgress(progress); mProgressBarAnimator.start(); } @Override protected void onDestroy() { super.onDestroy(); mTts.stopSpeaking(); // mTts.destroy(); // mIat.cancel(); // mIat.destroy(); if (null != mSpeechEvaluator) { mSpeechEvaluator.cancel(false); } } }