从自定义列表视图中播放歌曲,请按一下位置单击



我有一个longs的列表,即带有按钮的inflated中的CC_1。我想在单个button上播放单曲。我获得了按钮点击的位置,还通过播放歌曲方法传递了歌曲。但是它eventually它没有播放任何歌曲。如果我设置了playSong(1);,那么第一首歌正在播放。但是,如果我设置了playSong(position);,那么它将不会播放任何歌曲。

 public abstract class CustomAdapter extends BaseAdapter implements Filterable , SeekBar.OnSeekBarChangeListener {
    Context context;
    ArrayList<String> countryList;
ArrayList<String> mStringFilterList;
    LayoutInflater inflter;
    public ImageView img2;
    Handler mHandler = new Handler();
    private SeekBar songProgressBar;
    public CustomAdapter(Context applicationContext, ArrayList<String> countryList) {
        this.context = applicationContext;
        this.countryList = countryList;
        mStringFilterList =  countryList;
        inflter = (LayoutInflater.from(applicationContext));
    }
    @Override
    public int getCount() {
        return countryList.size();
    }
    public void updateData(ArrayList<String> countryList) {
        this.countryList = countryList;
        notifyDataSetChanged();
    }
    @Override
    public Object getItem(int i) {
        return null;
    }
    @Override
    public long getItemId(int i) {
        return 0;
    }
    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        view = inflter.inflate(R.layout.list_itemss, null);
        String hello = countryList.get(position);
        Log.d("Hello",hello);
            playSong(1);
        String henno1 = String.valueOf(hello.length());
        Log.d("hellya",henno1);
        songProgressBar = (SeekBar) view.findViewById(R.id.songProgressBar);
        TextView country = (TextView) view.findViewById(R.id.textView);
        country.setText(hello);
        songProgressBar.setOnSeekBarChangeListener(this);
        songCurrentDurationLabel = (TextView) findViewById(R.id.songCurrentDurationLabel);
        songTotalDurationLabel = (TextView) findViewById(R.id.songTotalDurationLabel);

        img2 = (ImageView)view.findViewById(R.id.button3);
        img2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(Main.this,"Position is --"+position,Toast.LENGTH_LONG).show();
                int songIndex = position;
                playSong(songIndex);
                Log.d("song_index", String.valueOf(songIndex));
    /*I GET THE INDEX IN LOG*/
                if(mp.isPlaying()){
                    if(mp!=null){
                        mp.pause();
                        // Changing button image to play button
                        img2.setImageResource(R.drawable.playbutton);
                    }
                }else{
                    // Resume song
                    if(mp!=null){
                        mp.start();
                        // Changing button image to pause button
                        img2.setImageResource(R.drawable.pausebutton);
                    }
                }
                Toast.makeText(Main.this,"Play Song",Toast.LENGTH_LONG).show();
            }
        });
        ImageView img =(ImageView)view.findViewById(R.id.button);
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(Main.this,"Added to Playlist",Toast.LENGTH_LONG).show();
                file = new File(path.get(position));
               customAdapter.notifyDataSetChanged();
            }
        });
        return view;
    }
    public void  playSong(int songIndex){
        // Play song
        Toast.makeText(Main.this,"Song is Playing",Toast.LENGTH_LONG).show();
        int m =songIndex;
//i ALSO GET THE POSITION IN LOG ON BUTTON CLICK BUT SONG IS NOT PLAYING.
        Log.d("Printsssss", String.valueOf(m));
        try {
            mp.start();
            mp.reset();
            mp.setDataSource(songsList.get(songIndex).get("songPath"));
            mp.prepare();
            mp.start();
            updateProgressBar();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

您可以尝试从您的活动的setOnitemClicklistner启动播放器。

启动活动时,getView()方法将用于列表视图的所有项目。

  listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
              //You have to put here the code to get your song from list of songs
        }
  });

我无能为力,我看不到变量的声明" MP"one_answers"歌曲列表"

Adapter的责任是填充您的ListRecyclerView等,getView与单个项目结合。如果您的ActivityFragment破坏了您必须手动发布MediaPlayer资源,这将为您准备,因此您应该简单地通过在ActivityFragment中添加onListItemClick来播放歌曲。

public void onListItemClick(ListView l, View v, int position, long id) {
}

现在,您将可以播放和暂停各自的歌曲。玩得开心:)

最新更新