diff --git a/app/src/main/java/com/service/rare/recorder/CustomAdapter.java b/app/src/main/java/com/service/rare/recorder/CustomAdapter.java index 770348c..1c5ac60 100644 --- a/app/src/main/java/com/service/rare/recorder/CustomAdapter.java +++ b/app/src/main/java/com/service/rare/recorder/CustomAdapter.java @@ -16,10 +16,10 @@ public class CustomAdapter extends RecyclerView.Adapter { - private String[] localDataSet; - private String[] localDataSet2; + private final String[] localDataSet; + private final String[] localDataSet2; - private String[] localDataSet3; + private final String[] localDataSet3; public static Integer LastAdapterPos = -1; public static ImageView prev_image; @@ -32,27 +32,27 @@ public static class ViewHolder extends RecyclerView.ViewHolder { private final TextView textView; private final TextView textView2; private final TextView title; - final ImageView play_image; private final CardView cardview; + private final ImageView play_image; public ViewHolder(View view) { super(view); // Define click listener for the ViewHolder's View - textView = (TextView) view.findViewById(R.id.textView); - textView2 = (TextView) view.findViewById(R.id.textView2); - title = (TextView) view.findViewById(R.id.title); - play_image = (ImageView) view.findViewById(R.id.imageView); + textView = view.findViewById(R.id.textView); + textView2 = view.findViewById(R.id.textView2); + title = view.findViewById(R.id.title); + play_image = view.findViewById(R.id.imageView); play_image.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("TAG", "onClick: "+getAdapterPosition()); - String filepath = MainActivity2.path.toString()+"/"+MainActivity2.files[getAdapterPosition()]+".mp3" ; + String filepath = MainActivity2.path +"/"+MainActivity2.files[getAdapterPosition()]+".mp3" ; try { - boolean isPlaying=MainActivity2.playRecording(filepath,v,getAdapterPosition(),prev_image); + boolean isPlaying = MainActivity2.playRecording(filepath, v, getAdapterPosition(), prev_image, play_image); if(isPlaying){ play_image.setImageResource(R.drawable.baseline_stop_24); @@ -74,7 +74,7 @@ public void onClick(View v) { } }); - cardview = (CardView) view.findViewById(R.id.cardView); + cardview = view.findViewById(R.id.cardView); cardview.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { diff --git a/app/src/main/java/com/service/rare/recorder/MainActivity2.java b/app/src/main/java/com/service/rare/recorder/MainActivity2.java index 4724e9f..1365957 100644 --- a/app/src/main/java/com/service/rare/recorder/MainActivity2.java +++ b/app/src/main/java/com/service/rare/recorder/MainActivity2.java @@ -1,7 +1,6 @@ package com.service.rare.recorder; import static com.service.rare.recorder.CustomAdapter.LastAdapterPos; - import static java.lang.Integer.valueOf; import android.content.Intent; @@ -23,7 +22,6 @@ import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; - import java.io.File; import java.io.IOException; import java.util.Date; @@ -193,7 +191,8 @@ protected void onResume() { Toast.makeText(this, "Resumed", Toast.LENGTH_SHORT).show(); super.onResume(); } - public static boolean playRecording(String filePath, View v, int adapterPosition, ImageView prev_image) throws IOException { + + public static boolean playRecording(String filePath, View v, int adapterPosition, ImageView prev_image, ImageView play_image) throws IOException { //----- ToDo: Convert to a switch/case scenario or faster processing---- // Create MediaPlayer Instance if (!hasMPInstance) { @@ -209,8 +208,14 @@ public static boolean playRecording(String filePath, View v, int adapterPosition ); mediaPlayer.setDataSource(v.getContext(), myUri); mediaPlayer.prepare(); + mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { + public void onCompletion(MediaPlayer mp) { + play_image.setImageResource(R.drawable.baseline_play_arrow_24); + } + }); mediaPlayer.start(); - hasMPInstance=true; + + hasMPInstance = true; return true; } else { if(mediaPlayer.isPlaying()) { @@ -241,8 +246,14 @@ public static boolean playRecording(String filePath, View v, int adapterPosition ); mediaPlayer.setDataSource(v.getContext(), myUri); mediaPlayer.prepare(); + mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { + public void onCompletion(MediaPlayer mp) { + play_image.setImageResource(R.drawable.baseline_play_arrow_24); + } + }); mediaPlayer.start(); - hasMPInstance=true; + + hasMPInstance = true; return true; } }else { @@ -254,23 +265,30 @@ public static boolean playRecording(String filePath, View v, int adapterPosition else{ if(prev_image!=null){ prev_image.setImageResource(R.drawable.baseline_play_arrow_24); - Toast.makeText(v.getContext(), "NotNull ",Toast.LENGTH_SHORT).show(); +// Toast.makeText(v.getContext(), "NotNull ",Toast.LENGTH_SHORT).show(); } mediaPlayer.release(); // Toast.makeText(v.getContext(), "Situation 3 - "+ LastAdapterPos+"/"+adapterPosition,Toast.LENGTH_SHORT).show(); - mediaPlayer = new MediaPlayer(); - Uri myUri = Uri.parse(filePath); // initialize Uri here - mediaPlayer.setAudioAttributes( - new AudioAttributes.Builder() - .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) - .setUsage(AudioAttributes.USAGE_MEDIA) - .build() - ); - mediaPlayer.setDataSource(v.getContext(), myUri); - mediaPlayer.prepare(); - mediaPlayer.start(); - hasMPInstance=true; - return true;} + mediaPlayer = new MediaPlayer(); + Uri myUri = Uri.parse(filePath); // initialize Uri here + mediaPlayer.setAudioAttributes( + new AudioAttributes.Builder() + .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) + .setUsage(AudioAttributes.USAGE_MEDIA) + .build() + ); + mediaPlayer.setDataSource(v.getContext(), myUri); + mediaPlayer.prepare(); + mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { + public void onCompletion(MediaPlayer mp) { + play_image.setImageResource(R.drawable.baseline_play_arrow_24); + } + }); + mediaPlayer.start(); + + hasMPInstance = true; + return true; + } }