【问题标题】:Listview play two audio files on button clickListview 在按钮单击时播放两个音频文件
【发布时间】: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()) 应该分开播放,第一个已经完成。

【问题讨论】:

    标签: android listview audio


    【解决方案1】:

    获取“Word”对象的新ArrayList,并在每个项目上单击,将单词对象(wrt index)添加到ArrayList(如PLayList)。通过遍历添加的对象播放音乐(使用playfile()方法)在 ArrayList 中。(您可以在进入下一个 Activity 后清除列表)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多