我有一个带有自定义列表项的ListView,并且在每个列表项中有一个Button和一个SeekBar。现在,当单击一个按钮时,音频开始播放,我想设置该SeekBar的进度以查看按钮的位置。使用我拥有的代码,正确的SeekBar开始移动,但是当我向上和向下滚动几次时,不同的SeekBar开始移动。我尝试了很多东西,这是我的最后一个:
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
public class MyListAdapter extends ArrayAdapter<Audio> {
ArrayList<Audio> audios;
Context context;
int resource;
private static MediaPlayer mediaPlayer;
public Handler mHandler = new Handler();
TextView totalTime;
TextView playingTime;
private MyListAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull ArrayList<Audio> audios) {
super(context, resource, audios);
Collections.reverse(audios);
this.audios = audios;
this. context = context;
this.resource = resource;
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
final ViewHolder viewHolder;
View row;
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.audio_cell, parent, false);
viewHolder = new ViewHolder();
viewHolder.playButton = (ImageButton) row.findViewById(R.id.playButton);
viewHolder.seekBar = (SeekBar) row.findViewById(R.id.seekBar);
viewHolder.playButton.setTag(position);
viewHolder.seekBar.setTag(position);
row.setTag(viewHolder);
} else {
row = convertView;
viewHolder = (ViewHolder) row.getTag();
}
viewHolder.seekBar.setProgress(0);
//I get the audio url here
final Audio audio = getItem(position);
assert audio != null;
String theURL = audio.getURL();
//Button Click
viewHolder.playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//here I call the method to play the audio and after that I want
//to update the SeekBar
playAudio(theURL);
updateSeekBar(viewHolder.seekBar);
}
});
return row;
}
private void updateSeekBar(final SeekBar sb) {
Runnable mUpdateTimeTask = new Runnable() {
@Override
public void run() {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
int mCurrentPosition = mediaPlayer.getCurrentPosition() / 1000;
mHandler.postDelayed(this, 100);
sb.setProgress(mCurrentPosition);
}
}
};
mHandler.postDelayed(mUpdateTimeTask, 100);
}
}
这是我的ViewHolder类:
public class ViewHolder {
public ImageButton playButton;
public SeekBar seekBar;
}
现在正如我所说,我已经尝试了很多东西,但似乎没有任何工作,我想到我尝试过的事情都是一样的,因为我每次都得到相同的结果。一旦我向上和向下滚动几次,它就会在不同的SeekBar上设置进度。任何帮助表示赞赏!
答案 0 :(得分:1)
列表视图在getView()中回收视图,因为你正在使用getTag()和setTAg()。所以当点击项目时,它会反映在其他一些位置,并且由于重复使用,歌曲也开始在其他位置播放图。
解决这个问题。创建一个全局int值,用于存储所单击项目的位置。
private int mSelectedItem = -1;
每当点击播放按钮时,将该位置添加到mSelectedItem
viewHolder.playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Store the position here
mSelectedItem=position;
//here I call the method to play the audio and after that I want
//to update the SeekBar
playAudio(theURL);
updateSeekBar(viewHolder.seekBar);
}
});
然后像这样更新seekBar
在调用方法
时传递位置updateSeekBar(viewHolder.seekBar,position);
检查位置是否与您单击的位置相同并更新搜索栏
private void updateSeekBar(final SeekBar sb,int position) {
final Runnable mUpdateTimeTask=null;
if(mSelectedItem==position){
mUpdateTimeTask = new Runnable() {
@Override
public void run() {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
int mCurrentPosition = mediaPlayer.getCurrentPosition() / 1000;
mHandler.postDelayed(this, 100);
sb.setProgress(mCurrentPosition);
}
}
};
mHandler.postDelayed(mUpdateTimeTask, 100);
}else{
mHandler.removeCallbacks(mUpdateTimeTask);
sb.setProgress(0);
}
}
答案 1 :(得分:0)
你可以像下面一样写...
声明变量
private MediaPlayer mediaPlayer;
private double startTime = 0;
private double finalTime = 0;
public int oneTimeOnly = 0;
更改viewHolder.playButton
viewHolder.playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Being...
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(theURL);
try {
// Log.e("ddd***",tv_directory.getText().toString());
if(mediaPlayer.isPlaying()){
Log.e("isplaying**",mediaPlayer.isPlaying()+"");
mediaPlayer.pause();
finalTime = mediaPlayer.getDuration();
startTime = mediaPlayer.getCurrentPosition();
if (oneTimeOnly == 0) {
seekBar.setMax((int) finalTime);
oneTimeOnly = 1;
}
}else{
// Log.e("isplaying**",mediaPlayer.isPlaying()+"");
mediaPlayer.prepare();
//mediaPlayer.start();
mediaPlayer.start();
finalTime = mediaPlayer.getDuration();
startTime = mediaPlayer.getCurrentPosition();
if (oneTimeOnly == 0) {
seekBar.setMax((int) finalTime);
oneTimeOnly = 1;
}
}
} catch (Exception e) {
Log.e("err***",e.toString());
}
myHandler.postDelayed(new Runnable(){
public void run() {
startTime = mediaPlayer.getCurrentPosition();
tv_start_time.setText(String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes((long) startTime),
TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
toMinutes((long) startTime)))
);
seekBar.setProgress((int) startTime);
myHandler.postDelayed(this, 100);
}
}, 100);
} catch (Exception e) {
Log.e("err***",e.toString());
}
//End....
}
});
答案 2 :(得分:0)