在我的音乐播放器Android应用程序onclick回收器视图行(或)点击页面底部的播放图标,我应该能够为当前正在播放的特定ImageView设置可绘制的动画。任何人都可以帮助我,我如何设置可绘制到基于位置的ImageView当前正在播放? 提前谢谢......
答案 0 :(得分:1)
在Adapter类
中声明如下 int currentPosition = -1;
将标签设置为onBindViewHolder中的图像视图(最终MyViewHolder持有者,int位置) __number可以是任何
holder.albumImage.setTag(1);
为onclick of view
实施以下内容 holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int PlayStopButtonState = (int) holder.albumImage.getTag();
int previousPosition = currentPosition;
if (PlayStopButtonState == 1) {
currentPosition = holder.getAdapterPosition();
holder.albumImage.setImageResource(R.drawable.ic_pause);
holder.albumImage.setTag(2);
} else {
currentPosition = -1;
holder.albumImage.setImageResource(R.drawable.play);
holder.albumImage.setTag(1);
}
if (previousPosition != -1) {
notifyItemChanged(previousPosition);
}
}
});
最后在onBindViewHolder()
if(currentPosition == position ){
holder.albumImage.setImageResource(R.drawable.ic_pause);
} else {
holder.albumImage.setImageResource(R.drawable.ic_play);
}
答案 1 :(得分:0)
在Adapter类中...定义一个接口
public interface MyInterface {
public void onItemClick(int position, Object item);
}
在您的活动中实现此功能,并在onCreate()中调用以下方法来分配活动实例。
public void setListener(MyInterface clickListener) {
this.myItemsClickListener = clickListener;
}
在Adapter类的onBindViewHolder()中... 调用方法
myItemsClickListener.onItemClick(position, item);
注意:此处的位置是recyclerview项目位置,item是具有播放信息的对象。
希望这有帮助。
答案 2 :(得分:0)
深入了解
public class MusicAdapter extends RecyclerView.Adapter<MusicAdapter.MyViewHolder> {
List<MusicPojo> musicPojoList;
private static ColorStateList sColorStatePlaying;
private static ColorStateList sColorStateNotPlaying;
Context context;
String play_pause_state;
public MusicAdapter(Context context, List<MusicPojo> musicPojoList) {
this.context = context;
this.musicPojoList = musicPojoList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.music_list, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
//boolean is to check item clicked for playing
boolean isPlaying = false;
AnimationDrawable animation;
MusicPojo musicPojo = musicPojoList.get(position);
holder.songList.setText(musicPojo.getTrackName());
holder.trackAlbum.setText(musicPojo.getTrackAlbum());
// holder.songList.setSelected(true);
if (sColorStateNotPlaying == null || sColorStatePlaying == null) {
initializeColorStateLists(context);
}
try {
play_pause_state = MainMusicActivity.play_pause_state;
} catch (NullPointerException e) {
e.printStackTrace();
}
if (play_pause_state != null) {
if (play_pause_state.equalsIgnoreCase("play")) {
//set boolean true to apply animation when item clicked to play
isPlaying =true;
AnimationDrawable animation = (AnimationDrawable)
ContextCompat.getDrawable(context, R.drawable.ic_equalizer_white_36dp);
DrawableCompat.setTintList(animation, sColorStatePlaying);
holder.albumImage.setImageDrawable(animation);
holder.albumImage.setVisibility(View.VISIBLE);
animation.start();
} else if (play_pause_state.equalsIgnoreCase("stop")) {
//set boolean false to not apply animation when item clicked to stop music
isPlaying = false;
Drawable playDrawable = ContextCompat.getDrawable(context,
R.drawable.ic_equalizer1_white_36dp);
DrawableCompat.setTintList(playDrawable, sColorStatePlaying);
holder.albumImage.setImageDrawable(playDrawable);
holder.albumImage.setVisibility(View.VISIBLE);
}
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isPlaying){
//when item is clicked to play music then apply animation for image here
holder.albumImage.setBackgroundResource(R.drawable.youranimationxml);
animation = (AnimationDrawable) holder.albumImage.getBackground();
animation.start();
}
}
});
}
static private void initializeColorStateLists(Context ctx) {
sColorStateNotPlaying = ColorStateList.valueOf(ctx.getResources().getColor(
R.color.media_item_icon_not_playing));
sColorStatePlaying = ColorStateList.valueOf(ctx.getResources().getColor(
R.color.media_item_icon_playing));
}
public static void setPos(final int pos, Integer drw) {
MyViewHolder.albumImage.setImageResource(drw);
}
@Override
public int getItemCount() {
return musicPojoList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
public static TextView songList, trackAlbum;
public static ImageView albumImage;
private View itemView;
public MyViewHolder(View itemView) {
super(itemView);
this.itemView = itemView;
songList = (TextView) itemView.findViewById(R.id.musicListId);
trackAlbum = (TextView) itemView.findViewById(R.id.musicTrackAlbumId);
albumImage = (ImageView) itemView.findViewById(R.id.albumImageId);
}
}
}
MainActivity Class:
public class MainMusicActivity extends AppCompatActivity implements View.OnClickListener {
Intent serviceIntent;
private Button play_pause;
private boolean isMusicPlaying = false;
RelativeLayout mediaController;
private ProgressBar progressBar;
RecyclerView recyclerView;
ImageView play_pause_audio, prev_song, next_song;
MusicAdapter musicAdapter;
boolean isNetworkConnected;
private static int lastPosition;
public static String play_pause_state;
private static ColorStateList sColorStatePlaying;
private static ColorStateList sColorStateNotPlaying;
MusicService musicService;
private String streamAudioLink = "http://www.stephaniequinn.com/Music/Commercial%20DEMO%20-%2006.mp3";
boolean bufferedBroadcastIsRegistered;
private ProgressDialog progressDialog = null;
MusicPojo musicPojo;
List<MusicPojo> musicPojoArrayList = new ArrayList<MusicPojo>();
Intent audioStatus;
// seekbar variables
private SeekBar seekBar;
private int seekMax;
private static int songEnded = 0;
boolean seekbarBroadcastRegistered;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initInitialize();
musicAdapter = new MusicAdapter(MainMusicActivity.this, musicPojoArrayList);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(musicAdapter);
prepareAudioLinks();
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new ClickListener() {
@Override
public void onClick(View v, int position) {
MusicPojo moviePoj = musicPojoArrayList.get(position);
lastPosition = position;
// playAudio();
if (moviePoj.getMusicUrlLink() != null) {
serviceIntent.putExtra("setAudioLink", moviePoj.getMusicUrlLink());
try {
startService(serviceIntent);
isMusicPlaying = true;
play_pause_audio.setImageResource(R.drawable.ic_pause_circle_filled);
mediaController.setVisibility(View.VISIBLE);
/*MusicService musicService = new MusicService(mediaController);
musicService.startService(serviceIntent);*/
} catch (Exception e) {
e.printStackTrace();
}
} else {
Utils.getInstance(MainMusicActivity.this).customToast("Not a valid file");
}
}
@Override
public void onLongClick(View v, int position) {
}
}));
try {
serviceIntent = new Intent(MainMusicActivity.this, MusicService.class);
// setListeners();
} catch (Exception e) {
e.printStackTrace();
}
musicButtonClickListeners();
}
public void initInitialize() {
mediaController = (RelativeLayout) findViewById(R.id.mediaControls);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
play_pause_audio = (ImageView) findViewById(R.id.play_pause_mediaCont);
prev_song = (ImageView) findViewById(R.id.mc_prev);
next_song = (ImageView) findViewById(R.id.mc_next);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
seekBar = (SeekBar) findViewById(R.id.seekBarMC);
}
private BroadcastReceiver seekbarBroadcast = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
updateSeekbarUI(intent);
}
};
private void updateSeekbarUI(Intent intent) {
String counter = intent.getStringExtra("COUNTER");
String mediaMax = intent.getStringExtra("MEDIA_MAX");
String songEnd = intent.getStringExtra("SONG_ENDED");
int seekbarProgress = Integer.parseInt(counter);
seekMax = Integer.parseInt(mediaMax);
songEnded = Integer.parseInt(songEnd);
seekBar.setMax(seekMax);
seekBar.setProgress(seekbarProgress);
if (songEnded == 1) {
play_pause_audio.setImageResource(R.drawable.ic_play_circle_filled);
}
}
// Handling progress dialog for buffering...
public void showProgressDialog(Intent bufferIntent) {
if (bufferIntent.getStringExtra("buffering") != null) {
String bufferValue = bufferIntent.getStringExtra("buffering");
int bufferIntValue = Integer.parseInt(bufferValue);
// when broadcast value of buffering if "1" show dialog, dismiss when its "0"
switch (bufferIntValue) {
case 0:
/* if (progressDialog != null) {
progressDialog.dismiss();
}*/
progressBar.setIndeterminate(false);
break;
case 1:
// bufferDialog();
progressBar.setIndeterminate(true);
break;
}
}
}
public void changePlayPauseState(Intent musicStatusIntent) {
if (musicStatusIntent != null) {
if (musicStatusIntent.getStringExtra("PLAY_STATUS") != null) {
play_pause_state = musicStatusIntent.getStringExtra("PLAY_STATUS").toString();
if (play_pause_state.equalsIgnoreCase("play")) {
// play_pause_audio.setImageResource(R.drawable.ic_pause_circle_filled);
// progressBar.setIndeterminate(true);
if (progressDialog != null) {
progressDialog.dismiss();
}
} else if (play_pause_state.equalsIgnoreCase("stop")) {
// play_pause_audio.setImageResource(R.drawable.ic_play_circle_filled);
// progressBar.setIndeterminate(false);
if (progressDialog != null) {
progressDialog.dismiss();
}
} else {
}
} else {
// Utils.getInstance(MainMusicActivity.this).customToast("Null object");
}
}
}
public void musicButtonClickListeners() {
play_pause_audio.setOnClickListener(MainMusicActivity.this);
prev_song.setOnClickListener(MainMusicActivity.this);
next_song.setOnClickListener(MainMusicActivity.this);
}
// progress dialog
private void bufferDialog() {
new Thread(new Runnable() {
@Override
public void run() {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
progressDialog = new ProgressDialog(MainMusicActivity.this);
progressDialog.setMessage("Acquiring song...");
progressDialog.setTitle("Buffering...");
progressDialog.setCancelable(true);
progressDialog.show();
}
});
}
}).start();
}
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
showProgressDialog(intent);
changePlayPauseState(intent);
audioAction(intent);
}
};
public void setListeners() {
play_pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// playStopOnClick();
}
});
}
private void playStopOnClick() {
isNetworkConnected = NetworkInformation.isNetworkConnected(MainMusicActivity.this);
if (isNetworkConnected) {
if (!isMusicPlaying) {
LocalBroadcastManager.getInstance(MainMusicActivity.this).registerReceiver(seekbarBroadcast, new IntentFilter(MusicService.BROADCAST_ACTION));
seekbarBroadcastRegistered = true;
play_pause_audio.setImageResource(R.drawable.ic_pause_circle_filled);
// playAudio();
playSongBasedOnPosition(lastPosition);
isMusicPlaying = true;
} else {
if (isMusicPlaying) {
// play_pause.setBackgroundResource(android.R.drawable.ic_media_play);
play_pause_audio.setImageResource(R.drawable.ic_play_circle_filled);
stopPlaysService();
isMusicPlaying = false;
}
}
} else {
Utils.getInstance(MainMusicActivity.this).networkAlert();
}
}
private void stopPlaysService() {
// un-register seekbar broadcast
if (seekbarBroadcastRegistered) {
try {
LocalBroadcastManager.getInstance(MainMusicActivity.this).unregisterReceiver(seekbarBroadcast);
seekbarBroadcastRegistered = false;
} catch (Exception e) {
e.printStackTrace();
}
}
try {
stopService(serviceIntent);
} catch (Exception e) {
e.printStackTrace();
}
isMusicPlaying = false;
}
private void playAudio() {
serviceIntent.putExtra("setAudioLink", streamAudioLink);
try {
// musicService.setSong(musicPojoArrayList);
startService(serviceIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onPause() {
if (bufferedBroadcastIsRegistered) {
LocalBroadcastManager.getInstance(MainMusicActivity.this).unregisterReceiver(broadcastReceiver);
bufferedBroadcastIsRegistered = false;
}
if (seekbarBroadcastRegistered) {
try {
LocalBroadcastManager.getInstance(MainMusicActivity.this).unregisterReceiver(seekbarBroadcast);
seekbarBroadcastRegistered = false;
} catch (Exception e) {
e.printStackTrace();
}
}
super.onPause();
}
@Override
protected void onResume() {
// audioStatus = new Intent(MusicService.BROADCAST_BUFFER);
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled()) {
NetworkInformation.wifiSpeedTest(MainMusicActivity.this);
}
if (!bufferedBroadcastIsRegistered) {
LocalBroadcastManager.getInstance(MainMusicActivity.this).registerReceiver(broadcastReceiver, new IntentFilter(MusicService.BROADCAST_BUFFER));
bufferedBroadcastIsRegistered = true;
}
if (play_pause_state != null) {
if (play_pause_state.equalsIgnoreCase("play")) {
isMusicPlaying = true;
play_pause_audio.setImageResource(R.drawable.ic_pause_circle_filled);
mediaController.setVisibility(View.VISIBLE);
}
}
if (!seekbarBroadcastRegistered) {
LocalBroadcastManager.getInstance(MainMusicActivity.this).registerReceiver(seekbarBroadcast, new IntentFilter(MusicService.BROADCAST_ACTION));
seekbarBroadcastRegistered = true;
}
super.onResume();
}
public void prepareAudioLinks() {
MusicPojo musicPojo = new MusicPojo(ConstantsClass.STREAM_URL1, 1, "Track - 01", "Album Name");
musicPojoArrayList.add(musicPojo);
musicPojo = new MusicPojo(ConstantsClass.STREAM_URL2, 2, "Track - 02", "Album Name");
musicPojoArrayList.add(musicPojo);
musicPojo = new MusicPojo(ConstantsClass.STREAM_URL3, 3, "Track - 03", "Album Name");
musicPojoArrayList.add(musicPojo);
musicPojo = new MusicPojo(ConstantsClass.STREAM_URL4, 4, "Track - 04", "Album Name");
musicPojoArrayList.add(musicPojo);
musicPojo = new MusicPojo(ConstantsClass.STREAM_URL5, 5, "Track - 05", "Album Name");
musicPojoArrayList.add(musicPojo);
musicPojo = new MusicPojo(ConstantsClass.STREAM_URL6, 6, "Track - 06", "Album Name");
musicPojoArrayList.add(musicPojo);
musicPojo = new MusicPojo(ConstantsClass.STREAM_URL7, 7, "Track - 07", "Album Name");
musicPojoArrayList.add(musicPojo);
musicPojo = new MusicPojo(ConstantsClass.STREAM_URL8, 8, "Track - 08", "Album Name");
musicPojoArrayList.add(musicPojo);
musicAdapter.notifyDataSetChanged();
}
public void audioAction(Intent intent) {
intent.putExtra("AUDIO_ACTION", "STOP_AUDIO");
sendBroadcast(intent);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.play_pause_mediaCont:
if (play_pause_state.equalsIgnoreCase("play")) {
/* play_pause_audio.setImageResource(R.drawable.ic_pause_circle_filled);
// playSongBasedOnPosition(lastPosition);
audioAction(audioStatus);*/
playStopOnClick();
} else if (play_pause_state.equalsIgnoreCase("stop")) {
play_pause_audio.setImageResource(R.drawable.ic_play_circle_filled);
playSongBasedOnPosition(lastPosition);
}
break;
case R.id.mc_prev:
lastPosition = (lastPosition - 1);
if (lastPosition == -1) {
lastPosition = 0;
if (lastPosition < 0) {
Utils.getInstance(MainMusicActivity.this).customToast("No more songs to PLAY");
} else {
playSongBasedOnPosition(lastPosition);
}
}
break;
case R.id.mc_next:
lastPosition = (lastPosition + 1);
if (lastPosition >= musicPojoArrayList.size()) {
Utils.getInstance(MainMusicActivity.this).customToast("No more songs to PLAY");
} else {
playSongBasedOnPosition(lastPosition);
}
break;
}
}
public interface ClickListener {
void onClick(View v, int position);
void onLongClick(View v, int position);
}
public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
GestureDetector gestureDetector;
MainMusicActivity.ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final MainMusicActivity.ClickListener clickListener) {
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
public void playSongBasedOnPosition(int pos) {
if (musicPojoArrayList.get(pos) != null) {
MusicPojo moviePoj = musicPojoArrayList.get(pos);
// Utils.getInstance(MainMusicActivity.this).customToast(String.valueOf(musicPojoArrayList.get(pos).getMusicUrlLink()));
// playAudio();
if (moviePoj.getMusicUrlLink() != null) {
serviceIntent.putExtra("setAudioLink", moviePoj.getMusicUrlLink());
try {
startService(serviceIntent);
mediaController.setVisibility(View.VISIBLE);
/*MusicService musicService = new MusicService(mediaController);
musicService.startService(serviceIntent);*/
} catch (Exception e) {
e.printStackTrace();
}
} else {
Utils.getInstance(MainMusicActivity.this).customToast("Not a valid file");
}
}
}
}