在 BUTTON CLICK 上播放自定义列表视图中的歌曲
Play song From Custom Listview On BuTTON CLICK with position
我有一个多头列表,它是 inflated
in custom listview
,带有按钮。我想在单击 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();
}
}
您可以尝试从 activity 的 setOnItemClickListner 启动播放器。
当您启动 activity 时,将为列表视图的所有项目调用 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" 和 "songsList"
Adapter
的职责是填充你的List
、RecyclerView
等,getView
是绑定单品的。如果您的 Activity
或 Fragment
损坏,您必须手动释放 MediaPlayer
资源,这会让您头疼,所以您应该通过在 [=15] 中添加 onListItemClick
来播放您的歌曲=] 或 Fragment
public void onListItemClick(ListView l, View v, int position, long id) {
}
现在您可以播放和暂停各自的歌曲了。玩得开心:)
我有一个多头列表,它是 inflated
in custom listview
,带有按钮。我想在单击 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();
}
}
您可以尝试从 activity 的 setOnItemClickListner 启动播放器。
当您启动 activity 时,将为列表视图的所有项目调用 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" 和 "songsList"
Adapter
的职责是填充你的List
、RecyclerView
等,getView
是绑定单品的。如果您的 Activity
或 Fragment
损坏,您必须手动释放 MediaPlayer
资源,这会让您头疼,所以您应该通过在 [=15] 中添加 onListItemClick
来播放您的歌曲=] 或 Fragment
public void onListItemClick(ListView l, View v, int position, long id) {
}
现在您可以播放和暂停各自的歌曲了。玩得开心:)