我有一个列表视图设置如下,每一行都有一个“播放”按钮,我需要实现的是让媒体播放器在点击此播放按钮时播放两个音频文件(一个接一个)。 / p>
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())应该在第一个完成时播放。
答案 0 :(得分:0)
获取&#34; Word&#34;的新数组列表对象,并在每个项目上单击,通过遍历ArrayList中添加的对象,将单词对象(wrt索引)添加到ArrayList(类似于PLayList)。播放音乐(使用playfile()方法)。(可以清除列表一次你去下一个活动。)