我正在实现一个字典。有活动(WordMeaningActivity)显示一个单词的含义。它由fragment和viewPager(滑动视图)组成。使用fragmentStatePagerAdapter填充片段。 在每个片段中都有一个扬声器按钮,用于实现文本到语音。当tts关闭时,按钮的图标是扬声器,否则当它说图标变为静音时。我提供的图像链接 - image with speaker icon(tts doesn't speak)
image with mute icon(tts speaks)
我有两个问题 - 1)当我滑动并更改片段时(当tts讲话时)它停止讲话但是当我向后滑动到该片段时,图标不会改变(保持静音),应该是扬声器。 onPause,onStop不起作用。我使用hashmap保留了以前的位置和片段本身,并且在onPageSelected方法中,我加入了片段并更改了图标但是在几次滑动(左右)之后它抛出了NullPointerException。
2)当tts完成它的演讲时,它不会改变图标。 setOnUtteranceProgressListener中的onDone不会更改图标。 这是代码 -
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_word_meaning);
initViews();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setTitle(title);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setPageTransformer(true, new ViewPager.PageTransformer() {
@Override
public void transformPage(View page, float position) {
if (position < -1) {
// [-00,-1): the page is way off-screen to the left.
right = true;
} else if (position <= 1) {
// [-1,1]: the page is "centered"
} else {
// (1,+00]: the page is way off-screen to the right.
left = true;
}
}
});
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
posOffset = positionOffset;
}
@Override
public void onPageSelected(int position) {
String word = list.get(position);
insertToRecent(word);
PlaceholderFragment frag;
if (right) {
frag = fragMap.get(position - 1);
frag.btnspeak.setBackgroundResource(R.drawable.speaker);
right = false;
} else if (left) {
frag = fragMap.get(position + 1);
frag.btnspeak.setBackgroundResource(R.drawable.speaker);
left = false;
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
mViewPager.setCurrentItem(position);
}
我片段的代码 -
public static class PlaceholderFragment extends Fragment{
TextView txtMeaning,txtHeading;
String word,meaning;
boolean on,speaker;
Button btnspeak,btnfav,btnshare;
static TextToSpeech tts;
int prevFragPos;
public PlaceholderFragment() {
}
public PlaceholderFragment newInstance(String w,String m, boolean bool) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putString("word", w);
args.putString("meaning",m);
args.putBoolean("fabState",bool);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
word=getArguments().getString("word");
on=getArguments().getBoolean("fabState");
meaning=getArguments().getString("meaning");
View rootView = inflater.inflate(R.layout.fragment_word_meaning, container, false);
txtMeaning = (TextView) rootView.findViewById(R.id.txtMeaning);
txtHeading = (TextView)rootView.findViewById(R.id.textHeading);
btnspeak = (Button) rootView.findViewById(R.id.speaker);
btnfav = (Button)rootView.findViewById(R.id.fav);
btnshare = (Button)rootView.findViewById(R.id.share);
txtHeading.setText(word);
txtMeaning.setText(meaning);
btnspeak.setBackgroundResource(R.drawable.speaker);
speaker=false;
Bundle bun = this.getArguments();
prevFragPos = bun.getInt("fragPos") - 1;
posSend = bun.getBoolean("bool");
final HashMap<String, String> map = new HashMap<String, String>();
map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"messageID");
tts=new TextToSpeech(getActivity(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status!=TextToSpeech.ERROR){
tts.setLanguage(Locale.ENGLISH);
}
}
});
if(on){
btnfav.setBackgroundResource(R.drawable.star_on);
}
else{
btnfav.setBackgroundResource(R.drawable.star_border);
}
btnspeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (speaker == false) {
speaker = true;
btnspeak.setBackgroundResource(R.drawable.mute);
tts.speak(meaning, TextToSpeech.QUEUE_FLUSH, map);
} else {
speaker = false;
btnspeak.setBackgroundResource(R.drawable.speaker);
tts.stop();
}
}
});
tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String utteranceId) {
}
@Override
public void onDone(String utteranceId) {
btnspeak.setBackgroundResource(R.drawable.speaker); //not working
}
@Override
public void onError(String utteranceId) {
}
});
btnfav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onClickFavourites(v, on, word)) {
btnfav.setBackgroundResource(R.drawable.star_on);
on = true;
} else {
btnfav.setBackgroundResource(R.drawable.star_border);
on = false;
}
}
});
btnshare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
waIntent.putExtra(Intent.EXTRA_TEXT, word + " - \n" + meaning);
startActivity(Intent.createChooser(waIntent, "Share with"));
}
});
return rootView;
}
@Override
public void onDestroyView() {
if(tts!=null){
tts.stop();
tts.shutdown();
}
super.onDestroyView();
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
片段适配器的代码 -
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
String word=list.get(position);
String m=getMeaning(word);
boolean on=checkIfFav(word);
PlaceholderFragment fragment = new PlaceholderFragment().newInstance(word, m, on);
fragMap.put(position, fragment);
return fragment;
}
@Override
public int getCount() {
return list.size();
}
}
提前感谢您的帮助!