我想制作一个简单的音乐播放器应用程序,在选定的播放列表中播放歌曲。我成功地检索到了该设备歌曲的详细信息。我有一个多首歌的播放列表。我一次只想放一首歌。但当我点击其他已经播放的歌曲时,歌曲并没有停止或暂停。请给出一个有理由的完整解决方案。
ArrayList<SongModel> list;
SongModel模型类
public class SongModel {
String songImg,songName,songPath;
Boolean isClick;
public SongModel(String songImg, String songName, String songPath, Boolean isClick) {
this.songImg = songImg;
this.songName = songName;
this.songPath = songPath;
this.isClick = isClick;
}
public Boolean getClick() {
return isClick;
}
public void setClick(Boolean click) {
isClick = click;
}
public String getSongPath() {
return songPath;
}
public void setSongPath(String songPath) {
this.songPath = songPath;
}
public String getSongImg() {
return songImg;
}
public void setSongImg(String songImg) {
this.songImg = songImg;
}
public String getSongName() {
return songName;
}
public void setSongName(String songName) {
this.songName = songName;
}
}
适配器中使用的MediaPlayer
MediaPlayer mediaPlayer = MediaPlayer.create(context, Uri.parse(list.get(position).getSongPath()));
if(list.get(position).getClick()){
holder.imgv_pause.setVisibility(View.VISIBLE);
holder.imgv_play.setVisibility(View.GONE);
try {
mediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
} else {
list.get(position).setClick(false);
holder.imgv_pause.setVisibility(View.GONE);
holder.imgv_play.setVisibility(View.VISIBLE);
try {
mediaPlayer.pause();
} catch (Exception e) {
e.printStackTrace();
}
}
holder.imgv_play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i=0;i<list.size();i++){
list.get(i).setClick(false);
}
list.get(position).setClick(true);
notifyDataSetChanged();
}
});
holder.imgv_pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
list.get(position).setClick(false);
notifyDataSetChanged();
}
});
}
当用户在onViewRecycled
中滚动时,您需要停止上一首歌曲。当用户滚动回收器视图时,会调用此函数。
@Override
public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
super.onViewRecycled(holder);
//stop song by its holder here
}
在Android Studio中一次只能播放一首歌曲使用这个静态媒体播放器媒体播放器不要使用Privet MediaPlayer MediaPlayer
公共类PlayerActivity<mgr,result,audioSession,getApplicationContext,phoneStateListener>扩展AppCompatActivity实现MediaPlayer.OnCompletionListener、AudioManager.OnAudioFocusChangeListener{
TextView songname, artistname, playduration, totalduration;
SeekBar seekBar;
ImageView backbutton, menu, playprevious, playnext, shufful;
ImageView play, repeat;
ImageView playalbum;
private Thread playThreads, previousThread, nextThread;
static ArrayList<Song> songlists = new ArrayList<>();
static MediaPlayer mediaPlayer;