【发布时间】:2014-11-10 22:17:07
【问题描述】:
我有一个如下设置的列表视图,每一行都有一个“播放”按钮,我需要实现的是让媒体播放器在单击此播放按钮时播放两个音频文件(一个接一个)。
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()) 应该分开播放,第一个已经完成。
【问题讨论】: