我使用基础适配器创建了一个列表视图,我的问题是当我在listview上开始第一首歌时所以搜索栏工作正常,但如果我在列表上开始播放第二首歌然后第一个和第二个但是搜索栏开始播放。
public class Adapter1 extends BaseAdapter {
ArrayList<String> audioList;
Context context;
private static LayoutInflater inflater = null;
double timeElapsed = 0, finalTime = 0;
MediaPlayer mediaPlayer;
Handler durationHandler = new Handler();
public Adapter1(MainActivity mainActivity, ArrayList<String> audioList) {
this.context = mainActivity;
this.audioList = audioList;
}
@Override
public int getCount() {
return audioList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View myView = convertView;
final ImageView imgIcon, imgPlay, imgPause;
final String fileName = audioList.get(position);
final String path="/Music/AudioFile/";
final TextView tvTotalTime,duration;
final SeekBar seekbar;
mediaPlayer = new MediaPlayer();
if (myView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
myView = inflater.inflate(R.layout.video_adapter, null);
imgPlay = (ImageView) myView.findViewById(R.id.imgPlay);
imgPause = (ImageView) myView.findViewById(R.id.imgPause);
seekbar =(SeekBar)myView.findViewById(R.id.seekbar);
tvTotalTime =(TextView)myView.findViewById(R.id.tvTotalTime);
duration =(TextView)myView.findViewById(R.id.duration);
final Runnable updateSeekBarTime = new Runnable() {
public void run() {
timeElapsed = mediaPlayer.getCurrentPosition();
seekbar.setProgress((int) timeElapsed);
tvTotalTime.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes((long) finalTime), TimeUnit.MILLISECONDS.toSeconds((long) finalTime)));
double timeRemaining = finalTime - timeElapsed;
duration.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining), TimeUnit.MILLISECONDS.toSeconds((long) timeRemaining) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining))));
durationHandler.postDelayed(this, 100);
}
};
seekbar.setClickable(true);
final int personPosition = position;
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
mediaPlayer.seekTo(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
Log.e("Checking File:", "Checking=======:" + fileName);
imgPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
Toast.makeText(context, "Stop" + position, Toast.LENGTH_SHORT).show();
} else {
mediaPlayer = MediaPlayer.create(context, Uri.parse(Environment.getExternalStorageDirectory().getPath() + path + fileName));
mediaPlayer.start();
Toast.makeText(context, "Playing" + position, Toast.LENGTH_SHORT).show();
imgPlay.setVisibility(View.INVISIBLE);
imgPause.setVisibility(View.VISIBLE);
timeElapsed = mediaPlayer.getCurrentPosition();
seekbar.setProgress((int) timeElapsed);
durationHandler.postDelayed(updateSeekBarTime, 100);
finalTime = mediaPlayer.getDuration();
seekbar.setMax((int) finalTime);
Log.e("Checking File:", "Checking=======:" + fileName);
}
}
});
imgPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer.pause();
Toast.makeText(context, "Paused" + position, Toast.LENGTH_LONG).show();
imgPause.setVisibility(View.INVISIBLE);
imgPlay.setVisibility(View.VISIBLE);
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
imgPause.setVisibility(View.INVISIBLE);
imgPlay.setVisibility(View.VISIBLE);
}
});
}
return myView;
}
}