package com.qzzx.taximeter.activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RelativeLayout;
import com.baidu.tts.auth.AuthInfo;
import com.baidu.tts.client.SpeechError;
import com.baidu.tts.client.SpeechSynthesizer;
import com.baidu.tts.client.SpeechSynthesizerListener;
import com.baidu.tts.client.TtsMode;
import com.qzzx.taximeter.R;
import com.qzzx.taximeter.adapter.CustomFragmentPagerAdapter;
import com.qzzx.taximeter.bean.SeatInfo;
import com.qzzx.taximeter.bean.dbo.DatabaseHelper;
import com.qzzx.taximeter.fragment.SeatManagerFragment;
import com.qzzx.taximeter.presenter.FragmentSaveDataPresenter;
import com.qzzx.taximeter.utils.TMApplication;
import com.qzzx.taximeter.view.IFragmentCallBackView;
import com.qzzx.taximeter.view.ISaveSeatDataView;
import com.qzzx.taximeter.widget.SlideShowView;
import java.util.ArrayList;
import java.util.List;
/**
* 主页面.
*/
public class IndexActivity extends BeasActivity implements SpeechSynthesizerListener, View.OnClickListener, IFragmentCallBackView {
protected final static String TAG = IndexActivity.class.getName();
private RelativeLayout fristSeatRL, secondSeatRL, thridSeatRL;
private ViewPager setDatas;
private List layoutList = new ArrayList();
private ArrayList fragmentList = new ArrayList();
private CustomFragmentPagerAdapter customFragmentPagerAdapter;
private int currentSelectIndex = 0;
private List seats = new ArrayList();
private TMApplication tma;
private SlideShowView advertismentBanner;
private DatabaseHelper databaseHelper; //本地广告 数据库存储
public SpeechSynthesizer mSpeechSynthesizer;//百度语音合成客户端
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
initialTts();
presenter.detachView();
initView();
getData();
showView();
}
@Override
protected FragmentSaveDataPresenter creatMyPresenter() {
return new FragmentSaveDataPresenter();
}
//数据绑定页面
private void showView() {
fragmentList = SeatManagerFragment.newInstance(seats);
customFragmentPagerAdapter = new CustomFragmentPagerAdapter(this.getSupportFragmentManager(), fragmentList);
setDatas.setAdapter(customFragmentPagerAdapter);
setDatas.setCurrentItem(currentSelectIndex);
setDatas.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageSelected(int home) {
currentSelectIndex = home;
setIndicatorStatus();
}
});
}
//初始化主界面控件
private void initView() {
tma = (TMApplication) this.getApplicationContext();
fristSeatRL = (RelativeLayout) findViewById(R.id.frist_seat_rl);
layoutList.add(fristSeatRL);
fristSeatRL.setOnClickListener(this);
secondSeatRL = (RelativeLayout) findViewById(R.id.second_seat_rl);
secondSeatRL.setOnClickListener(this);
layoutList.add(secondSeatRL);
thridSeatRL = (RelativeLayout) findViewById(R.id.thrid_seat_rl);
thridSeatRL.setOnClickListener(this);
layoutList.add(thridSeatRL);
setDatas = (ViewPager) findViewById(R.id.set_datas_vp);
advertismentBanner = (SlideShowView) findViewById(R.id.slideshow_view_id);
databaseHelper = new DatabaseHelper(IndexActivity.this,
"advertisement_db", 3);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.frist_seat_rl:
currentSelectIndex = 0;
setIndicatorStatus();
setDatas.setCurrentItem(currentSelectIndex);
mSpeechSynthesizer.speak("一号座位");
break;
case R.id.second_seat_rl:
currentSelectIndex = 1;
setIndicatorStatus();
setDatas.setCurrentItem(currentSelectIndex);
mSpeechSynthesizer.speak("三号座位");
break;
case R.id.thrid_seat_rl:
currentSelectIndex = 2;
setIndicatorStatus();
setDatas.setCurrentItem(currentSelectIndex);
mSpeechSynthesizer.speak("号座位");
break;
default:
break;
}
}
//点击或滑动改变座位title状态
private void setIndicatorStatus() {
int i = 0;
if (currentSelectIndex == 0) {
fristSeatRL.setBackgroundResource(R.mipmap.icon_set_down_btn);
secondSeatRL.setBackgroundResource(R.mipmap.icon_set_up_btn);
thridSeatRL.setBackgroundResource(R.mipmap.icon_set_up_btn);
} else if (currentSelectIndex == 1) {
secondSeatRL.setBackgroundResource(R.mipmap.icon_set_down_btn);
fristSeatRL.setBackgroundResource(R.mipmap.icon_set_up_btn);
thridSeatRL.setBackgroundResource(R.mipmap.icon_set_up_btn);
} else if (currentSelectIndex == 2) {
thridSeatRL.setBackgroundResource(R.mipmap.icon_set_down_btn);
secondSeatRL.setBackgroundResource(R.mipmap.icon_set_up_btn);
fristSeatRL.setBackgroundResource(R.mipmap.icon_set_up_btn);
}
while (i < layoutList.size()) {
RelativeLayout layout = layoutList.get(i);
View indicator = layout.getChildAt(2);
int textColor, indicatorColor;
if (i == currentSelectIndex) {
indicatorColor = R.color.grey_E5E5E5;
} else {
indicatorColor = R.color.black_505156;
}
indicator.setBackgroundColor(getResources().getColor(indicatorColor));
i++;
}
}
//模拟数据
public void getData() {
String[] urls = {"http://pic41.nipic.com/20140515/18726228_180908348114_2.jpg",
"http://img1.imgtn.bdimg.com/it/u=2775239910,914689810&fm=214&gp=0.jpg",
"http://pic35.nipic.com/20131119/11677495_103538269330_2.jpg",
"http://pic36.nipic.com/20131217/14741255_165436598177_2.jpg"};
advertismentBanner.initUI(this, urls);
SeatInfo s1 = new SeatInfo();
s1.status = 0;
s1.addr = 0;
tma.setSeatInfo(s1);
seats.add(s1);
SeatInfo s2 = new SeatInfo();
s2.status = 0;
s2.addr = 1;
tma.setSeatInfo(s2);
seats.add(s2);
SeatInfo s3 = new SeatInfo();
s3.status = 0;
s3.addr = 2;
tma.setSeatInfo(s3);
seats.add(s3);
setDatas.setOffscreenPageLimit(2);
}
@Override
public void refreshUI() {
seats = tma.getSeatInfo();
//通知座位一保存数据
presenter.attachView((ISaveSeatDataView) fragmentList.get(0));
presenter.satveData();
presenter.detachView();
//通知座位二保存数据
presenter.attachView((ISaveSeatDataView) fragmentList.get(1));
presenter.satveData();
presenter.detachView();
//通知座位三保存数据
presenter.attachView((ISaveSeatDataView) fragmentList.get(2));
presenter.satveData();
presenter.detachView();
//获取最新数据
seats = tma.getSeatInfo();
//将最新数据绑定到FM上
fragmentList = SeatManagerFragment.newInstance(seats);
//更新FM
customFragmentPagerAdapter.setFragments(fragmentList);
}
//************************************
/**
* 初始化TTS
*/
private void initialTts() {
// 获取tts实例
this.mSpeechSynthesizer = SpeechSynthesizer.getInstance();
// 设置app上下文(必需参数)
this.mSpeechSynthesizer.setContext(this);
// 设置tts监听器
this.mSpeechSynthesizer.setSpeechSynthesizerListener(this);
// 请替换为语音开发者平台注册应用得到的apikey和secretkey (在线授权)
this.mSpeechSynthesizer.setApiKey("TQwCxWZG10XwXOrmTPtjdN7G5KFfBqVd", "GxC9Cju3yM0i7duicxZjPeTQvbBFxo8Q");
// 初始化tts各参数
initTTSParam(this.mSpeechSynthesizer);
// 授权检测接口(可以不使用,只是验证授权是否成功)
AuthInfo authInfo = this.mSpeechSynthesizer.auth(TtsMode.MIX);
if (authInfo.isSuccess()) {
android.util.Log.d(TAG, "auth success");
} else {
String errorMsg = authInfo.getTtsError().getDetailMessage();
android.util.Log.d(TAG, "auth failed errorMsg=" + errorMsg);
}
/**
* 初始化 tts引擎,可以指定使用online在线,或者 mix离在线混合引擎 .
* mix离在线混合引擎会在online在线不能用的情况下自动使在线offline离线引擎 具体参数清查看文档
*/
mSpeechSynthesizer.initTts(TtsMode.MIX);
}
/**
* 在调用 在调用 speak 方法开始合成 并朗读之前,可以对参数进行配置(未设置的参数将使用默认值)
*/
private void initTTSParam(SpeechSynthesizer speechSynthesizer) {
// 发音人(在线引擎),可用参数为0,1,2,3。。。(服务器端会动态增加,各值含义参考文档,以文档说明为准。0--普通女声,1--普通男声,2--特别男声,3--情感男声。。。)默认0
speechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEAKER, "0");
// 调整音量 ,范围 [0-9],默认为5
speechSynthesizer.setParam(SpeechSynthesizer.PARAM_VOLUME, "5");
// 调整语速 ,范围 [0-9],默认为5
speechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEED, "5");
// 调整语调,范围 [0-9],默认为5
speechSynthesizer.setParam(SpeechSynthesizer.PARAM_PITCH, "5");
// 设置Mix模式的合成策略,默认MIX_MODE_DEFAULT, 其它参数请参考文档
speechSynthesizer.setParam(SpeechSynthesizer.PARAM_MIX_MODE, SpeechSynthesizer.MIX_MODE_HIGH_SPEED_NETWORK);
// 设置音频格式,默认AUDIO_ENCODE_AMR
speechSynthesizer.setParam(SpeechSynthesizer.PARAM_AUDIO_ENCODE, SpeechSynthesizer.AUDIO_ENCODE_AMR);
// 设置比特率,默认AUDIO_BITRATE_AMR_15K
speechSynthesizer.setParam(SpeechSynthesizer.PARAM_AUDIO_RATE, SpeechSynthesizer.AUDIO_BITRATE_AMR_12K65);
}
/**
* 开始合成
*/
@Override
public void onSynthesizeStart(String utteranceId) {
android.util.Log.d(TAG + "开始合成", "onSynthesizeStart utteranceId=" + utteranceId);
}
@Override
public void onSynthesizeDataArrived(String utteranceId, byte[] data, int progress) {
android.util.Log.d(TAG + "得到语音合成文件", "onSynthesizeDataArrived");
}
/**
* 完成合成
*/
@Override
public void onSynthesizeFinish(String utteranceId) {
android.util.Log.d(TAG + "完成合成", "onSynthesizeFinish utteranceId=" + utteranceId);
}
/**
* 播放开始
*/
@Override
public void onSpeechStart(String utteranceId) {
android.util.Log.d(TAG + "播放开始", "onSpeechStart utteranceId=" + utteranceId);
}
/*
* @param arg0
*
* @param arg1
*/
@Override
public void onSpeechProgressChanged(String utteranceId, int progress) {
android.util.Log.d(TAG + "状态改变", "onSpeechProgressChanged");
}
/**
* 播放完成
*/
@Override
public void onSpeechFinish(String utteranceId) {
android.util.Log.d(TAG + "播放完成", "onSpeechFinish utteranceId=" + utteranceId);
}
/**
* 合成出错
*/
@Override
public void onError(String utteranceId, SpeechError error) {
android.util.Log.d(TAG + "合成出错", "onError error=" + "(" + error.code + ")" + error.description + "--utteranceId=" + utteranceId);
}
}
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RelativeLayout;
import com.baidu.tts.auth.AuthInfo;
import com.baidu.tts.client.SpeechError;
import com.baidu.tts.client.SpeechSynthesizer;
import com.baidu.tts.client.SpeechSynthesizerListener;
import com.baidu.tts.client.TtsMode;
import com.qzzx.taximeter.R;
import com.qzzx.taximeter.adapter.CustomFragmentPagerAdapter;
import com.qzzx.taximeter.bean.SeatInfo;
import com.qzzx.taximeter.bean.dbo.DatabaseHelper;
import com.qzzx.taximeter.fragment.SeatManagerFragment;
import com.qzzx.taximeter.presenter.FragmentSaveDataPresenter;
import com.qzzx.taximeter.utils.TMApplication;
import com.qzzx.taximeter.view.IFragmentCallBackView;
import com.qzzx.taximeter.view.ISaveSeatDataView;
import com.qzzx.taximeter.widget.SlideShowView;
import java.util.ArrayList;
import java.util.List;
/**
* 主页面.
*/
public class IndexActivity extends BeasActivity implements SpeechSynthesizerListener, View.OnClickListener, IFragmentCallBackView {
protected final static String TAG = IndexActivity.class.getName();
private RelativeLayout fristSeatRL, secondSeatRL, thridSeatRL;
private ViewPager setDatas;
private List layoutList = new ArrayList();
private ArrayList fragmentList = new ArrayList();
private CustomFragmentPagerAdapter customFragmentPagerAdapter;
private int currentSelectIndex = 0;
private List seats = new ArrayList();
private TMApplication tma;
private SlideShowView advertismentBanner;
private DatabaseHelper databaseHelper; //本地广告 数据库存储
public SpeechSynthesizer mSpeechSynthesizer;//百度语音合成客户端
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
initialTts();
presenter.detachView();
initView();
getData();
showView();
}
@Override
protected FragmentSaveDataPresenter creatMyPresenter() {
return new FragmentSaveDataPresenter();
}
//数据绑定页面
private void showView() {
fragmentList = SeatManagerFragment.newInstance(seats);
customFragmentPagerAdapter = new CustomFragmentPagerAdapter(this.getSupportFragmentManager(), fragmentList);
setDatas.setAdapter(customFragmentPagerAdapter);
setDatas.setCurrentItem(currentSelectIndex);
setDatas.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageSelected(int home) {
currentSelectIndex = home;
setIndicatorStatus();
}
});
}
//初始化主界面控件
private void initView() {
tma = (TMApplication) this.getApplicationContext();
fristSeatRL = (RelativeLayout) findViewById(R.id.frist_seat_rl);
layoutList.add(fristSeatRL);
fristSeatRL.setOnClickListener(this);
secondSeatRL = (RelativeLayout) findViewById(R.id.second_seat_rl);
secondSeatRL.setOnClickListener(this);
layoutList.add(secondSeatRL);
thridSeatRL = (RelativeLayout) findViewById(R.id.thrid_seat_rl);
thridSeatRL.setOnClickListener(this);
layoutList.add(thridSeatRL);
setDatas = (ViewPager) findViewById(R.id.set_datas_vp);
advertismentBanner = (SlideShowView) findViewById(R.id.slideshow_view_id);
databaseHelper = new DatabaseHelper(IndexActivity.this,
"advertisement_db", 3);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.frist_seat_rl:
currentSelectIndex = 0;
setIndicatorStatus();
setDatas.setCurrentItem(currentSelectIndex);
mSpeechSynthesizer.speak("一号座位");
break;
case R.id.second_seat_rl:
currentSelectIndex = 1;
setIndicatorStatus();
setDatas.setCurrentItem(currentSelectIndex);
mSpeechSynthesizer.speak("三号座位");
break;
case R.id.thrid_seat_rl:
currentSelectIndex = 2;
setIndicatorStatus();
setDatas.setCurrentItem(currentSelectIndex);
mSpeechSynthesizer.speak("号座位");
break;
default:
break;
}
}
//点击或滑动改变座位title状态
private void setIndicatorStatus() {
int i = 0;
if (currentSelectIndex == 0) {
fristSeatRL.setBackgroundResource(R.mipmap.icon_set_down_btn);
secondSeatRL.setBackgroundResource(R.mipmap.icon_set_up_btn);
thridSeatRL.setBackgroundResource(R.mipmap.icon_set_up_btn);
} else if (currentSelectIndex == 1) {
secondSeatRL.setBackgroundResource(R.mipmap.icon_set_down_btn);
fristSeatRL.setBackgroundResource(R.mipmap.icon_set_up_btn);
thridSeatRL.setBackgroundResource(R.mipmap.icon_set_up_btn);
} else if (currentSelectIndex == 2) {
thridSeatRL.setBackgroundResource(R.mipmap.icon_set_down_btn);
secondSeatRL.setBackgroundResource(R.mipmap.icon_set_up_btn);
fristSeatRL.setBackgroundResource(R.mipmap.icon_set_up_btn);
}
while (i < layoutList.size()) {
RelativeLayout layout = layoutList.get(i);
View indicator = layout.getChildAt(2);
int textColor, indicatorColor;
if (i == currentSelectIndex) {
indicatorColor = R.color.grey_E5E5E5;
} else {
indicatorColor = R.color.black_505156;
}
indicator.setBackgroundColor(getResources().getColor(indicatorColor));
i++;
}
}
//模拟数据
public void getData() {
String[] urls = {"http://pic41.nipic.com/20140515/18726228_180908348114_2.jpg",
"http://img1.imgtn.bdimg.com/it/u=2775239910,914689810&fm=214&gp=0.jpg",
"http://pic35.nipic.com/20131119/11677495_103538269330_2.jpg",
"http://pic36.nipic.com/20131217/14741255_165436598177_2.jpg"};
advertismentBanner.initUI(this, urls);
SeatInfo s1 = new SeatInfo();
s1.status = 0;
s1.addr = 0;
tma.setSeatInfo(s1);
seats.add(s1);
SeatInfo s2 = new SeatInfo();
s2.status = 0;
s2.addr = 1;
tma.setSeatInfo(s2);
seats.add(s2);
SeatInfo s3 = new SeatInfo();
s3.status = 0;
s3.addr = 2;
tma.setSeatInfo(s3);
seats.add(s3);
setDatas.setOffscreenPageLimit(2);
}
@Override
public void refreshUI() {
seats = tma.getSeatInfo();
//通知座位一保存数据
presenter.attachView((ISaveSeatDataView) fragmentList.get(0));
presenter.satveData();
presenter.detachView();
//通知座位二保存数据
presenter.attachView((ISaveSeatDataView) fragmentList.get(1));
presenter.satveData();
presenter.detachView();
//通知座位三保存数据
presenter.attachView((ISaveSeatDataView) fragmentList.get(2));
presenter.satveData();
presenter.detachView();
//获取最新数据
seats = tma.getSeatInfo();
//将最新数据绑定到FM上
fragmentList = SeatManagerFragment.newInstance(seats);
//更新FM
customFragmentPagerAdapter.setFragments(fragmentList);
}
//************************************
/**
* 初始化TTS
*/
private void initialTts() {
// 获取tts实例
this.mSpeechSynthesizer = SpeechSynthesizer.getInstance();
// 设置app上下文(必需参数)
this.mSpeechSynthesizer.setContext(this);
// 设置tts监听器
this.mSpeechSynthesizer.setSpeechSynthesizerListener(this);
// 请替换为语音开发者平台注册应用得到的apikey和secretkey (在线授权)
this.mSpeechSynthesizer.setApiKey("TQwCxWZG10XwXOrmTPtjdN7G5KFfBqVd", "GxC9Cju3yM0i7duicxZjPeTQvbBFxo8Q");
// 初始化tts各参数
initTTSParam(this.mSpeechSynthesizer);
// 授权检测接口(可以不使用,只是验证授权是否成功)
AuthInfo authInfo = this.mSpeechSynthesizer.auth(TtsMode.MIX);
if (authInfo.isSuccess()) {
android.util.Log.d(TAG, "auth success");
} else {
String errorMsg = authInfo.getTtsError().getDetailMessage();
android.util.Log.d(TAG, "auth failed errorMsg=" + errorMsg);
}
/**
* 初始化 tts引擎,可以指定使用online在线,或者 mix离在线混合引擎 .
* mix离在线混合引擎会在online在线不能用的情况下自动使在线offline离线引擎 具体参数清查看文档
*/
mSpeechSynthesizer.initTts(TtsMode.MIX);
}
/**
* 在调用 在调用 speak 方法开始合成 并朗读之前,可以对参数进行配置(未设置的参数将使用默认值)
*/
private void initTTSParam(SpeechSynthesizer speechSynthesizer) {
// 发音人(在线引擎),可用参数为0,1,2,3。。。(服务器端会动态增加,各值含义参考文档,以文档说明为准。0--普通女声,1--普通男声,2--特别男声,3--情感男声。。。)默认0
speechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEAKER, "0");
// 调整音量 ,范围 [0-9],默认为5
speechSynthesizer.setParam(SpeechSynthesizer.PARAM_VOLUME, "5");
// 调整语速 ,范围 [0-9],默认为5
speechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEED, "5");
// 调整语调,范围 [0-9],默认为5
speechSynthesizer.setParam(SpeechSynthesizer.PARAM_PITCH, "5");
// 设置Mix模式的合成策略,默认MIX_MODE_DEFAULT, 其它参数请参考文档
speechSynthesizer.setParam(SpeechSynthesizer.PARAM_MIX_MODE, SpeechSynthesizer.MIX_MODE_HIGH_SPEED_NETWORK);
// 设置音频格式,默认AUDIO_ENCODE_AMR
speechSynthesizer.setParam(SpeechSynthesizer.PARAM_AUDIO_ENCODE, SpeechSynthesizer.AUDIO_ENCODE_AMR);
// 设置比特率,默认AUDIO_BITRATE_AMR_15K
speechSynthesizer.setParam(SpeechSynthesizer.PARAM_AUDIO_RATE, SpeechSynthesizer.AUDIO_BITRATE_AMR_12K65);
}
/**
* 开始合成
*/
@Override
public void onSynthesizeStart(String utteranceId) {
android.util.Log.d(TAG + "开始合成", "onSynthesizeStart utteranceId=" + utteranceId);
}
@Override
public void onSynthesizeDataArrived(String utteranceId, byte[] data, int progress) {
android.util.Log.d(TAG + "得到语音合成文件", "onSynthesizeDataArrived");
}
/**
* 完成合成
*/
@Override
public void onSynthesizeFinish(String utteranceId) {
android.util.Log.d(TAG + "完成合成", "onSynthesizeFinish utteranceId=" + utteranceId);
}
/**
* 播放开始
*/
@Override
public void onSpeechStart(String utteranceId) {
android.util.Log.d(TAG + "播放开始", "onSpeechStart utteranceId=" + utteranceId);
}
/*
* @param arg0
*
* @param arg1
*/
@Override
public void onSpeechProgressChanged(String utteranceId, int progress) {
android.util.Log.d(TAG + "状态改变", "onSpeechProgressChanged");
}
/**
* 播放完成
*/
@Override
public void onSpeechFinish(String utteranceId) {
android.util.Log.d(TAG + "播放完成", "onSpeechFinish utteranceId=" + utteranceId);
}
/**
* 合成出错
*/
@Override
public void onError(String utteranceId, SpeechError error) {
android.util.Log.d(TAG + "合成出错", "onError error=" + "(" + error.code + ")" + error.description + "--utteranceId=" + utteranceId);
}
}
收藏
点赞
0
个赞
请登录后评论
TOP
切换版块
可以查看一下onError回调中的错误码定位一下错误,或者查看一下tts SDK内部打印的一些log分析一下。应该是合成没有成功导致的,有可能是jar包或者so库没有导入正确导致的。