0
Nun, ich habe Recyclerview mit Audiodateien, Audio-Wiedergabe funktioniert gut, aber Seekbar wird nicht aktualisiert, wenn ich auf ein Element klicke! Das letzte Positionselement seekbar wird aktualisiert und abgespielt. Ich habe die genaue Lösung nicht gefunden. Hier ist mein Code.Seekbar in RecyclerView gibt die letzte Position zurück
RecyclerAdapter
public class AudiorecyclerAdapter extends RecyclerView.Adapter<AudiorecyclerAdapter.MyViewHolder> {
private ProgressBar pb;
private List<GettingAudio> datalist;
MediaPlayer mp=new MediaPlayer();
private Context context;
Activity act;
private Dialog dialog;
private String BASE_URL = "http://zuman.e3.amazon/";
private String medialink,titlestring;
private int downloadedSize = 0 ;
TextView cur_val;
SeekBar seek;
private int totalSize = 0;
public AudiorecyclerAdapter(Context context,Activity act,List<GettingAudio> list){
this.datalist=list;
this.context=context;
this.act=act;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.listreff,parent,false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position)
{
holder.title.setText(datalist.get(position).getTitle());
holder.postedby.setText(datalist.get(position).getPostedby());
holder.postedon.setText(datalist.get(position).getDatetime());
holder.likes.setText(datalist.get(position).getLikes());
final String media=datalist.get(position).getMedia();
final String tit=holder.title.getText().toString();
Log.d("Harry Coooooovaaa", datalist.toString());
seek=holder.seekBar;
holder.play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
medialink=media;
titlestring=tit;
showProgress();
Toast.makeText(context,medialink,Toast.LENGTH_LONG).show();
new Thread(new Runnable() {
@Override
public void run() {
downloadFile();
}
}).start();
}
});
}
@Override
public int getItemCount() {
return datalist.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView title,postedby,likes,postedon;
Button play;
SeekBar seekBar;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.audiotitle);
title=(TextView)itemView.findViewById(R.id.audiotitle);
postedby=(TextView)itemView.findViewById(R.id.postedby);
postedon=(TextView)itemView.findViewById(R.id.date);
likes=(TextView)itemView.findViewById(R.id.likes);
play=(Button)itemView.findViewById(R.id.butplay);
seekBar=(SeekBar)itemView.findViewById(R.id.seekBar);
}
}
private void showProgress(){
dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.myprogressdialog);
dialog.setTitle("Download Progress");
cur_val = (TextView) dialog.findViewById(R.id.cur_pg_tv);
cur_val.setText("Starting download...");
dialog.show();
pb = (ProgressBar)dialog.findViewById(R.id.progress_bar);
pb.setProgress(0);
pb.setProgressDrawable(context.getResources().getDrawable(R.drawable.green_progress));
}
private void downloadFile(){
try {
URL url = new URL(BASE_URL+medialink);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
//connect
urlConnection.connect();
final String folder_main = "Apna";
//set the path where we want to save the file
/* File SDCardRoot =new File(Environment.getExternalStorageDirectory(),folder_main);
if (!SDCardRoot.exists()) {
SDCardRoot.mkdirs();
}
//create a new file, to save the downloaded file
File file = new File(SDCardRoot,title);*/
File wallpaperDirectory = new File(Environment.getExternalStorageDirectory(),folder_main);
//have the object build the directory structure, if needed.
wallpaperDirectory.mkdirs();
//create a File object for the output file
final String perfection=titlestring.replaceAll("\"","");
File outputFile = new File(wallpaperDirectory, perfection+".mp3");
//now attach the OutputStream to the file object, instead of a String representation
FileOutputStream fileOutput = new FileOutputStream(outputFile);
//Stream used for reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
//this is the total size of the file which we are downloading
totalSize = urlConnection.getContentLength();
act.runOnUiThread(new Runnable() {
public void run() {
pb.setMax(totalSize);
}
});
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
// update the progressbar //
act.runOnUiThread(new Runnable() {
public void run() {
pb.setProgress(downloadedSize);
float per = ((float)downloadedSize/totalSize) * 100;
cur_val.setText("Downloaded " + downloadedSize + "KB/" + totalSize + "KB (" + (int)per + "%)");
}
});
}
//close the output stream when complete //
fileOutput.close();
act.runOnUiThread(new Runnable() {
public void run() {
dialog.dismiss();
try{
mp.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/Apna/"+perfection+".mp3");//Write your location here
mp.prepareAsync();
// mp.start();
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
mRunnable.run();
}
});
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
} catch (final MalformedURLException e) {
showError("Error : MalformedURLException " + e);
Log.d("dfdsfsd", e.toString());
e.printStackTrace();
} catch (final IOException e) {
Log.d("dfdsfsd", e.toString());
showError("Error : IOException " + e);
e.printStackTrace();
}
catch (final Exception e) {
Log.d("dfdsfsd", e.toString());
showError("Error : Please check your internet connection " + e);
}
}
private void showError(final String err){
act.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(context, err, Toast.LENGTH_LONG).show();
Log.d("dfdsfsd", err);
}
});
}
private Handler mHandler = new Handler();
private Runnable mRunnable = new Runnable() {
@Override
public void run() {
if(mp != null) {
//set max value
int mDuration = mp.getDuration();
seek.setMax(mDuration);
//update total time text view
//set progress to current position
int mCurrentPosition = mp.getCurrentPosition();
seek.setProgress((int) mp.getCurrentPosition());
// holder.seekBar.setProgress(mCurrentPosition);
//update current time text view
//handle drag on seekbar
seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(mp != null && fromUser){
mp.seekTo(progress);
}
}
});
}
//repeat above code every second
mHandler.postDelayed(this, 10);
}
};
}
MyActivity
public class Recyclerviewaudio extends AppCompatActivity {
RecyclerView recyclerView;
List<List<GettingAudio>> arrylist;
List<GettingAudio> mlist;
AudiorecyclerAdapter audiorecyclerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recyclerviewaudio);
recyclerView = (RecyclerView) findViewById(R.id.audiorecycler);
init();
}
public void init() {
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://zuman.ec3.amazonaws.com/")
.addConverterFactory(GsonConverterFactory.create()).build();
Apicall api = retrofit.create(Apicall.class);
Call<List<List<GettingAudio>>> call = api.GetAudio("Audio", "1", "0");
call.enqueue(new Callback<List<List<GettingAudio>>>() {
@Override
public void onResponse(Call<List<List<GettingAudio>>> call, Response<List<List<GettingAudio>>> response) {
arrylist = response.body();
mlist = arrylist.get(0);
Log.d("Boooooodyyy", arrylist.toString());
// Log.d("response code", new Gson().toJson(response.body()));
// Log.d("response body", response.body().toString());
audiorecyclerAdapter = new AudiorecyclerAdapter(Recyclerviewaudio.this, Recyclerviewaudio.this, mlist);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
recyclerView.setAdapter(audiorecyclerAdapter);
}
@Override
public void onFailure(Call<List<List<GettingAudio>>> call, Throwable t) {
Log.d("Throwable", t.getMessage());
}
});
}
}