Open Doctor32th opened 2 years ago
//I LEAVE MY CODE HERE
public class PlayerActivity extends AppCompatActivity {
Button btnPlay, btnNext, btnPrevious, btnFastForward, btnFastBackward; TextView txtSongName, txtSongStart, txtSongEnd; SeekBar seekMusicBar; BarVisualizer barVisualizer; ImageView imageView;
String songName;
public static final String EXTRA_NAME = "song_name"; static MediaPlayer mediaPlayer;
int position;
ArrayList
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_player);
btnPrevious = findViewById(R.id.btnPrevious);
btnNext = findViewById(R.id.btnNext);
btnPlay = findViewById(R.id.btnPlay);
btnFastForward = findViewById(R.id.btnFastForward);
btnFastBackward = findViewById(R.id.btnFastBackward);
txtSongName = findViewById(R.id.txtSong);
txtSongStart = findViewById(R.id.txtSongStart);
txtSongEnd = findViewById(R.id.txtSongEnd);
seekMusicBar = findViewById(R.id.seekBar);
barVisualizer = findViewById(R.id.green_wave);
imageView = findViewById(R.id.imgView);
if (mediaPlayer != null){
mediaPlayer.start();
mediaPlayer.release();
}
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
mySongs = (ArrayList)bundle.getParcelableArrayList("songs");
String sName = intent.getStringExtra("songname");
position = bundle.getInt("pos", 0);
txtSongName.setSelected(true);
Uri uri = Uri.parse(mySongs.get(position).toString());
songName = mySongs.get(position).getName();
txtSongName.setText(songName);
mediaPlayer = MediaPlayer.create(getApplicationContext(), uri);
mediaPlayer.start();
updateSeekBar = new Thread(){
@Override
public void run() {
int totalDuration = mediaPlayer.getDuration();
int currentPosition = 0;
while(currentPosition < totalDuration) {
try {
sleep(500);
currentPosition = mediaPlayer.getCurrentPosition();
seekMusicBar.setProgress(currentPosition);
} catch (InterruptedException | IllegalStateException e) {
e.printStackTrace();
}
}
}
};
seekMusicBar.setMax(mediaPlayer.getDuration());
updateSeekBar.start();
seekMusicBar.getProgressDrawable().setColorFilter(getResources().getColor(R.color.teal_200), PorterDuff.Mode.MULTIPLY);
seekMusicBar.getThumb().setColorFilter(getResources().getColor(R.color.teal_200), PorterDuff.Mode.SRC_IN);
seekMusicBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mediaPlayer.seekTo(seekBar.getProgress());
}
});
String endTime = createTime(mediaPlayer.getDuration());
txtSongEnd.setText(endTime);
final Handler handler = new Handler();
final int delay = 1000;
handler.postDelayed(new Runnable() {
@Override
public void run() {
String currentTime = createTime(mediaPlayer.getCurrentPosition());
txtSongStart.setText(currentTime);
handler.postDelayed(this, delay);
}
}, delay);
btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mediaPlayer.isPlaying()){
btnPlay.setBackgroundResource(R.drawable.ic_play);
mediaPlayer.pause();
} else {
btnPlay.setBackgroundResource(R.drawable.ic_pause);
mediaPlayer.start();
TranslateAnimation moveAnim = new TranslateAnimation(-25, 25,-25, 25);
moveAnim.setInterpolator(new AccelerateInterpolator());
moveAnim.setDuration(600);
moveAnim.setFillEnabled(true);
moveAnim.setFillAfter(true);
moveAnim.setRepeatMode(Animation.REVERSE);
moveAnim.setRepeatCount(1);
imageView.startAnimation(moveAnim);
}
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
btnNext.performClick();
}
});
int audioSessionId = mediaPlayer.getAudioSessionId();
if (audioSessionId != -1) {
barVisualizer.setAudioSessionId(audioSessionId);
}
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaPlayer.stop();
mediaPlayer.release();
position = ((position + 1)%mySongs.size());
Uri uri = Uri.parse(mySongs.get(position).toString());
mediaPlayer = MediaPlayer.create(getApplicationContext(), uri);
songName = mySongs.get(position).getName();
txtSongName.setText(songName);
mediaPlayer.start();
startAnimation(imageView, 360f);
}
});
btnPrevious.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaPlayer.stop();
mediaPlayer.release();
position = ((position - 1) < 0)?(mySongs.size() -1):position-1;
Uri uri = Uri.parse(mySongs.get(position).toString());
mediaPlayer = MediaPlayer.create(getApplicationContext(), uri);
songName = mySongs.get(position).getName();
txtSongName.setText(songName);
mediaPlayer.start();
startAnimation(imageView, -360f);
}
});
btnFastForward.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mediaPlayer.isPlaying()){
mediaPlayer.seekTo(mediaPlayer.getCurrentPosition() + 10000);
}
}
});
btnFastBackward.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mediaPlayer.isPlaying()){
mediaPlayer.seekTo(mediaPlayer.getCurrentPosition() - 10000);
}
}
});
}
public void startAnimation(View view, Float degree){
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "rotation", 0f, degree);
objectAnimator.setDuration(1000);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(objectAnimator);
animatorSet.start();
}
public String createTime(int duration){ String time = ""; int min = duration/1000/60; int sec = duration/1000%60;
time = time + min + ":";
if (sec < 10) {
time+="0";
}
time+=sec;
return time;
}
//I LEAVE THE MAIN ACTIVITY TOO public class MainActivity extends AppCompatActivity {
ListView listView;
String[] items;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
runtimePermission();
}
public void runtimePermission() {
Dexter.withContext(this).withPermissions(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO)
.withListener(new MultiplePermissionsListener() {
@Override
public void onPermissionsChecked(MultiplePermissionsReport multiplePermissionsReport) {
displaySong();
}
@Override
public void onPermissionRationaleShouldBeShown(List<PermissionRequest> list, PermissionToken permissionToken) {
permissionToken.continuePermissionRequest();
}
}).check();
}
public ArrayList<File> findSong(File file) {
ArrayList<File> arrayList = new ArrayList<>();
File f = file;
File[] files = null;
try {
files = f.listFiles();
if (files.length > 0) {
for (File singleFile : files) {
if (singleFile.isDirectory() && !singleFile.isHidden()) {
arrayList.addAll(findSong(singleFile));
} else {
if (singleFile.getName().endsWith(".mp3") || singleFile.getName().endsWith(".wav")) {
arrayList.add(singleFile);
}
}
}
} else {
Path path = Paths.get("/storage/emulated/0/Music");
File filePath = path.toFile();
arrayList.add(filePath);
}
} catch (NullPointerException e) {
System.out.println("Error:" + e + "" + e.getStackTrace()[0].getLineNumber());
}
return arrayList;
}
public void displaySong() {
final ArrayList<File> mySongs = findSong(Environment.getExternalStorageDirectory());
items = new String[mySongs.size()];
for (int i = 0; i < mySongs.size(); i++) {
items[i] = mySongs.get(i).getName().toString().replace(".mp3", "").replace(".wav", "");
}
customAdapter customAdapter = new customAdapter();
listView.setAdapter(customAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String songName = (String) listView.getItemAtPosition(position);
startActivity(new Intent(getApplicationContext(), PlayerActivity.class)
.putExtra("songs", mySongs)
.putExtra("songname", songName)
.putExtra("pos", position)
);
}
});
}
class customAdapter extends BaseAdapter {
@Override
public int getCount() {
return items.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.list_item, null);
TextView txtSong = view.findViewById(R.id.txtSong);
txtSong.setSelected(true);
txtSong.setText(items[position]);
return view;
}
}
}
I don't think this has anything to do with this project, but to me, it looks like when you click on the song it tries to open an Activity which can not be found
maybe you should be edit your xml layout references of this activity maybe some attributes not found in ur liberary
PLEASE HELP! When I run the program, I´m seeing files called Music. The thing is I don´t know what kind of files they are. When I click on one, I get the next error: