Commit 7d996612 authored by Ravishani W.M.S's avatar Ravishani W.M.S

Upload New File

parent e0532784
package com.example.smartpillow;
import androidx.appcompat.app.AppCompatActivity;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import java.io.File;
import java.time.OffsetDateTime;
import java.util.ArrayList;
public class MusicTherapyS extends AppCompatActivity {
Button btnplay, btnnext, btnpre, btnff, btnfr;
TextView txtsname, txtsstart, txtsstop;
SeekBar seekmusic;
ImageView imagevView;
String sname;
public static final String EXTRA_NAME = "song_name";
static MediaPlayer mediaPlayer;
int position;
ArrayList<File> mySongs;
Thread updateseekbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music_therapy_s);
btnplay = findViewById(R.id.btnPlay);
btnnext = findViewById(R.id.btnNext);
btnpre = findViewById(R.id.btnPre);
btnff = findViewById(R.id.btnFf);
btnfr = findViewById(R.id.btnFr);
txtsstart = findViewById(R.id.txtStart);
txtsstop = findViewById(R.id.txtStop);
txtsname = findViewById(R.id.txtSn);
seekmusic = findViewById(R.id.seekbar);
imagevView = findViewById(R.id.imageview);
if (mediaPlayer != null)
{
mediaPlayer.stop();
mediaPlayer.release();
}
Intent i = getIntent();
Bundle bundle = i.getExtras();
mySongs = (ArrayList) bundle.getParcelableArrayList("songs");
String songName = i.getStringExtra("songname");
position = bundle.getInt("pos", 0);
txtsname.setSelected(true);
Uri uri = Uri.parse(mySongs.get(position).toString());
sname = mySongs.get(position).getName();
txtsname.setText(sname);
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();
seekmusic.setProgress(currentposition);
}
catch (InterruptedException | IllegalStateException e)
{
e.printStackTrace();
}
}
}
};
seekmusic.setMax(mediaPlayer.getDuration());
updateseekbar.start();
seekmusic.getProgressDrawable().setColorFilter(getResources().getColor(R.color.purple_700), PorterDuff.Mode.MULTIPLY);
seekmusic.getThumb().setColorFilter(getResources().getColor(R.color.purple_700), PorterDuff.Mode.SRC_IN);
seekmusic.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mediaPlayer.seekTo(seekBar.getProgress());
}
});
String endTime = createTime(mediaPlayer.getDuration());
txtsstop.setText(endTime);
final Handler handler = new Handler();
final int delay = 1000;
handler.postDelayed(new Runnable() {
@Override
public void run() {
String currentTime = createTime(mediaPlayer.getCurrentPosition());
txtsstart.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();
}
}
});
//next listener
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
btnnext.performClick();
}
});
btnnext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mediaPlayer.isPlaying())
{
mediaPlayer.stop();
mediaPlayer.release();
}
/*position = ((position+1)%mySongs.size());*/
position = (((position+1)==mySongs.size())?0:position+1);
Uri u = Uri.parse(mySongs.get(position).toString());
mediaPlayer = MediaPlayer.create(getApplicationContext(), u);
sname = mySongs.get(position).getName();
txtsname.setText(sname);
mediaPlayer.start();
btnplay.setBackgroundResource(R.drawable.ic_pause);
startAnimation(imagevView);
}
});
btnpre.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mediaPlayer.isPlaying())
{
mediaPlayer.stop();
mediaPlayer.release();
}
/*position = ((position-1)<0)?(mySongs.size()-1):(position-1);*/
position=((position-1)<0)?mySongs.size()-1:(position-1);
Uri ur = Uri.parse(mySongs.get(position).toString());
mediaPlayer = MediaPlayer.create(getApplicationContext(), ur);
sname = mySongs.get(position).getName();
txtsname.setText(sname);
mediaPlayer.start();
btnplay.setBackgroundResource(R.drawable.ic_pause);
startAnimation(imagevView);
}
});
btnff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mediaPlayer.isPlaying())
{
mediaPlayer.seekTo(mediaPlayer.getCurrentPosition()+10000);
}
}
});
btnfr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mediaPlayer.isPlaying())
{
mediaPlayer.seekTo(mediaPlayer.getCurrentPosition()-10000);
}
}
});
}
public void startAnimation(View view)
{
ObjectAnimator animator = ObjectAnimator.ofFloat(imagevView,"rotation", 0f,360f);
animator.setDuration(1000);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(animator);
animatorSet.start();
}
public String createTime(int duration)
{
String time = "";
int min = duration/1000/60;
int sec = duration/1000%60;
time+=min+":";
if (sec<10)
{
time+="0";
}
time+=sec;
return time;
}
@Override
public void onBackPressed() {
mediaPlayer.stop();
Intent intent = new Intent(MusicTherapyS.this, MusicTherapyF.class);
startActivity(intent);
finish();
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment