一切正常,但搜索栏在每个列表视图中都在更新, 但音频播放正确
public class AudioPLayAdapter extends BaseAdapter implements
OnCompletionListener, SeekBar.OnSeekBarChangeListener {
private Activity activity;
private ArrayList<String> al_fname = new ArrayList<String>();
private static LayoutInflater inflater = null;
private boolean flag_play;
private MediaPlayer mPlayer = null;
Handler seekHandler = new Handler();
public AudioPLayAdapter(Activity a, ArrayList<String> al_fname) {
activity = a;
mPlayer = new MediaPlayer();
this.al_fname = al_fname;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(final int position, final View convertView,
final ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.audi_rec_lv_layout, null);
final Button play_btn = (Button) vi.findViewById(R.id.play_stop_button);
final SeekBar seekBar = (SeekBar) vi.findViewById(R.id.seekbar_play);
play_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!flag_play) {
play_btn.setText("Stop");
startPlaying();
flag_play = true;
} else {
play_btn.setText("Play");
mPlayer.stop();
seekBar.setProgress(0);
flag_play = false;
}
}
private void startPlaying() {
// TODO Auto-generated method stub
try {
mPlayer.reset();
FileInputStream rawFile = new FileInputStream(al_fname
.get(position));
Log.e("filename", al_fname.get(position));
mPlayer.setDataSource(rawFile.getFD());
// mPlayer.setDataSource(mFileName);
mPlayer.prepare();
mPlayer.start();
// set Progress bar values
seekBar.setProgress(0);
seekBar.setMax(100);
// seekHandler.postDelayed(updateSeek, 100);
// Updating progress bar
updateProgressBar();
} catch (IOException e) {
Log.e("preparefailed", "prepare() failed");
}
}
private void updateProgressBar() {
// TODO Auto-generated method stub
seekHandler.postDelayed(mUpdateTimeTask, 100);
}
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
long totalDuration = mPlayer.getDuration();
long currentDuration = mPlayer.getCurrentPosition();
String str1 = new String();
// holder.timer_tv.setText(str1.valueOf(currentDuration));
// Updating progress bar
int progress = (int) (getProgressPercentage(
currentDuration, totalDuration));
// Log.d("Progress", ""+progress);
// if(position==holder.seekBar.getId())
// seekBar.setTag(position);
seekBar.setProgress(progress);
// Running this thread after 100 milliseconds
seekHandler.postDelayed(this, 100);
if (!mPlayer.isPlaying()) {
play_btn.setText("Play");
flag_play = false;
}
if (!flag_play)
seekBar.setProgress(0);
}
private int getProgressPercentage(long currentDuration,
long totalDuration) {
// TODO Auto-generated method stub
Double percentage = (double) 0;
long currentSeconds = (int) (currentDuration / 1000);
long totalSeconds = (int) (totalDuration / 1000);
// calculating percentage
percentage = (((double) currentSeconds) / totalSeconds) * 100;
// return percentage
return percentage.intValue();
}
};
});
return vi;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return al_fname.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
}
}