我的应用程序中有40个textView的大约40个声音,其中一些确实可以播放,但有些人不会。我已经查到了其他类似的问题,但认为我的不同,textview突出显示白色onpress,并在未按下时变黑,同时播放匹配单词的声音。 2个数组的长度相同 - 每个文本视图单词对应一个声音。
words = new ArrayList<>() ;//this is the array of textviews
ArrayList<MediaPlayer>word_sounds = new ArrayList<>();
w1= MediaPlayer.create(Page12.this, R.raw.sentence10_36);
w2= MediaPlayer.create(Page12.this, R.raw.sentence10_37);
w3= MediaPlayer.create(Page12.this, R.raw.sentence10_38);
word_sounds.add(w1);
word_sounds.add(w2);
word_sounds.add(w3);
for( int i = 0 ; i < words.size();i++){
final TextView element = words.get(i);
final MediaPlayer current_sound = word_sounds.get(i);
try{
current_sound.prepare();}
catch (Exception e){}
current_sound.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
current_sound.start();
}
});
element.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
element.setTextColor(Color.WHITE);
current_sound.start();
break ;