Android Listview在单击按钮时播放两个音频文件

Android Listview在单击按钮时播放两个音频文件,android,listview,audio,Android,Listview,Audio,我有一个列表视图设置如下,每一行都有一个“播放”按钮,我需要实现的是让mediaplayer在点击这个播放按钮后一个接一个地播放两个音频文件 public class WordsActivity extends SherlockActivity{ Category currentCat; ArrayList<Word> words; String catID; MediaPlayer m; protected void onCreate(Bu

我有一个列表视图设置如下,每一行都有一个“播放”按钮,我需要实现的是让mediaplayer在点击这个播放按钮后一个接一个地播放两个音频文件

public class WordsActivity extends SherlockActivity{
    Category currentCat;
    ArrayList<Word> words;
    String catID;
    MediaPlayer m;

    protected void onCreate(Bundle savedInstanceState) {
        ...
        words = myDbHelper.getAllWordsforCat(catID);
        wordListAdapter adapter = new wordListAdapter(this, words);
        ListView listView = (ListView) findViewById(R.id.words_list);
        listView.setAdapter(adapter);
    }

    ...

    public class wordListAdapter extends ArrayAdapter<Word>{

        private final Context context;
        private final ArrayList<Word> itemsArrayList;

        public wordListAdapter(Context context, ArrayList<Word> itemsArrayList) {

            super(context, R.layout.words_row, itemsArrayList);

            this.context = context;
            this.itemsArrayList = itemsArrayList;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final Word word = itemsArrayList.get(position);
            LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View rowView = inflater.inflate(R.layout.words_row, parent, false);

            ...
            ImageView playbtn = (ImageView) rowView.findViewById(R.id.word_play_btn);
            playbtn.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    playfile(word.getengSound());
                }
            });

            ...

            return rowView;
        }

        public void playfile(String filename) {
            try {
                if (m.isPlaying()) {
                    m.stop();
                    m.release();
                    m = new MediaPlayer();
                }
                m = new MediaPlayer();
                AssetFileDescriptor descriptor = getAssets().openFd(filename);
                m.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
                descriptor.close();

                m.prepare();
                m.setVolume(1f, 1f);
                m.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


}


}

正如您可以看到的,音频文件是资产包的一部分,上面的代码播放第一个音频文件,第二个word.getFreSound应该在第一个音频文件完成后播放。

获取word对象的新ArrayList,然后在每个项上单击,将单词objectwrt索引添加到类似于播放列表的ArrayList中。通过遍历ArrayList中添加的对象来播放音乐使用playfile方法。转到下一个活动后,可以清除该列表