So I have a Song List which plays a song when clicked on text view and displays a gif (audio spectrum) next to the song name. What I want is when I click on another song from Song List the previous clicked song should stop (which it was able to do). But along with this I also want the gif of the previous song to disappear which I am not able to do. Here is my code -
package com.shaikhsakib.sounddemo;
import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.database.DatabaseReference;
import java.util.List;
import pl.droidsonroids.gif.GifImageView;
public class SongList extends ArrayAdapter {
private Activity context;
private List<Song> songList;
DatabaseReference databaseReference;
MediaPlayer mediaPlayer;
View listViewItem;
boolean unclicked = true;
public SongList(@NonNull Activity context, List<Song> songList, DatabaseReference databaseReference){
super(context, R.layout.playlist_textview, songList);
this.context = context;
this.songList = songList;
this.databaseReference = databaseReference;
}
@Override
public View getView(final int pos, View view, ViewGroup viewGroup){
LayoutInflater inflater = context.getLayoutInflater();
listViewItem = inflater.inflate(R.layout.songlist_textview, null, true);
final TextView songListTextView = (TextView) listViewItem.findViewById(R.id.songListTextView);
songListTextView.setSelected(true);
final GifImageView bass = (GifImageView) listViewItem.findViewById(R.id.bass);
final Song list = songList.get(pos);
songListTextView.setText(list.getSongName());
songListTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer != null && mediaPlayer.isPlaying() && null!=bass.getDrawable()) {
mediaPlayer.stop();
//trying to remove gif from last song
bass.setVisibility(View.GONE);
}else {
//trying to put back gif over here on current song
bass.setVisibility(View.VISIBLE);
}
try {
String f = Environment.getExternalStorageDirectory().getAbsolutePath() + "/music/" + list.getSongName().toString() + ".mp3";
Uri uri = Uri.parse(f);
mediaPlayer = MediaPlayer.create(context.getApplicationContext(), uri);
bass.setImageResource(R.drawable.bass);
mediaPlayer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
bass.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mediaPlayer.isPlaying()) {
mediaPlayer.pause();
bass.setImageResource(R.drawable.bass1);
}
else {
bass.setImageResource(R.drawable.bass);
mediaPlayer.start();
}
}
});
return listViewItem;
}
}
Here is video link of demo -
Declare a GifImageView lastBass;
in SongList
class as global variable.
Now modify your songListTextView's onClickListener
code like:
songListTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer != null && mediaPlayer.isPlaying() && null!=bass.getDrawable()) {
mediaPlayer.stop();
//trying to remove gif from last song
if(lastBass!=null)
lastBass.setVisibility(View.GONE);
bass.setVisibility(View.GONE);
}else {
if(lastBass!=null)
lastBass.setVisibility(View.GONE);
//trying to put back gif over here on current song
bass.setVisibility(View.VISIBLE);
}
try {
String f = Environment.getExternalStorageDirectory().getAbsolutePath() + "/music/" + list.getSongName().toString() + ".mp3";
Uri uri = Uri.parse(f);
mediaPlayer = MediaPlayer.create(context.getApplicationContext(), uri);
bass.setImageResource(R.drawable.bass);
bass.setVisibility(View.VISIBLE); // add this line
mediaPlayer.start();
lastBass=bass; // initialize lastBass here
} catch (Exception e) {
e.printStackTrace();
}
}
});
Update
add this: bass.setVisibility(View.VISIBLE);
after bass.setImageResource(R.drawable.bass);
as in above code