TextView文字同步讯飞语音滚动解决方案

来源:互联网 发布:钢楼梯计算软件 编辑:程序博客网 时间:2024/05/10 21:48

讯飞语音同步滚动TextView文字播放的问题

SpeechSynthesizer mTts = new SpeechSynthesizer(context, mTtsInitListener);

1.最简单方式

 private SynthesizerListener mTtsListener = new SynthesizerListener.Stub() {        。。。。省略        @Override        public void onSpeakProgress(int progress) throws RemoteException {//height 文字内容总高度 float height=mContent.getLineCount()*mContent.getLineHeight(); //内容高度大于一屏才滑动,mContent.getMeasuredHeight()控件高度            if(height>mContent.getMeasuredHeight()) {//              //置顶滑动,有点快,需要减速                int y= (int) (height* (progress) / 100);                mContent.setScrollY(y);            }        }    };

2.优化,减速2行,滚动速度比较适中了,基本播放在中间了

private SynthesizerListener mTtsListener = new SynthesizerListener.Stub() {        。。。。省略        @Override        public void onSpeakProgress(int progress) throws RemoteException {//height 文字内容总高度float height=mContent.getLineCount()*mContent.getLineHeight();            if(height>mContent.getMeasuredHeight()) {//内容高度大于一屏才滚动                int y= (int) (height* progress / 100);                //2*mContent.getLineHeight()滚动慢2行,一般来说一屏不至于才2行,比较合适,y必须大于0                y = y - 2 * mContent.getLineHeight() ;                if(y>0)mContent.setScrollY(y);            }        }    };

但是以上方法对于 文字总高度>100*控件高度 的又不试用了,这是因为讯飞的回调progress最小值是1,所以讯飞回调progress=1之前,语音就已经读到看不见的地方了,影响用户体验。
目前的解决方式是,将文字分屏播放,分屏总数=文字总高度/100*控件高度,然后将文字是用substring拆分成几段(分屏总数)播放。

0 0
原创粉丝点击