几次单击后,音频文件是否停止在列表视图上播放?

时间:2018-11-28 15:11:01

标签: android android-studio listview audio media-player

我创建了一个ListView,每个ListView项都有2个TextView,一个图像和一个音频文件。一切正常,并且播放了音频文件,但是当我多次单击项目并上下滚动时,不会播放音频文件。那部分有什么问题?

这是我的Java代码:

public class NumbersActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_numbers);

    ArrayList<Word> words = new ArrayList<>();

    words.add(new Word("one","un",R.drawable.number_one,R.raw.one));
    words.add(new Word("two","deux",R.drawable.number_two,R.raw.two));
     .......

    ListView listView = (ListView) findViewById(R.id.numberViewlist);
    WordAdapter itemsAdapter = new WordAdapter(this, 
    words,R.color.category_numbers,listView);
    listView.setAdapter(itemsAdapter);
  }
}

这是WordAdaper的代码:

 lView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
     MediaPlayer mp = MediaPlayer.create(getContext(),currentWord.getsound());
     mp.start();
   }
 });

1 个答案:

答案 0 :(得分:0)

使用此代码:

if (mp != null) {
  if (mp.isPlaying()) {
    mp.stop();
  }
  mp.release();
  mp = null;
}

请参阅此Doc