public class BodyActivity extends AppCompatActivity {
TextView tvBody, tvTitle;
ImageView imageView;
// AudioPlay
ImageView imgPlayerControl;
TextView textCurrentTime, textTotalDuration;
SeekBar seekBar;
MediaPlayer mediaPlayer;
Handler handler = new Handler();
Runnable runnable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_body);
Intent intent = getIntent();
String bid = intent.getStringExtra("bId");
tvBody = findViewById(R.id.tv_body);
tvTitle = findViewById(R.id.tv_title);
imageView = findViewById(R.id.img_body);
retriveData(bid);
//AudioPlay
imgPlayerControl = findViewById(R.id.img_play_control);
textCurrentTime = findViewById(R.id.tv_current_time);
textTotalDuration = findViewById(R.id.tv_balance_time);
seekBar = findViewById(R.id.seekBar);
mediaPlayer = new MediaPlayer();
imgPlayerControl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
imgPlayerControl.setImageResource(R.drawable.ic_play);
}else {
mediaPlayer.start();
imgPlayerControl.setImageResource(R.drawable.ic_pause);
updateSeekbar();
}
}
});
prepareMediaPlayer();
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if(b){
mediaPlayer.seekTo(i);
seekBar.setProgress(i);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
private void prepareMediaPlayer(){
Uri uri = Uri.parse("https://www2.cs.uic.edu/~i101/SoundFiles/BabyElephantWalk60.wav");
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(BodyActivity.this,uri);
}catch (Exception exception){
Toast.makeText(this, exception.getMessage(), Toast.LENGTH_SHORT).show();
}
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
seekBar.setMax(mp.getDuration());
textTotalDuration.setText(milliSecondsToTimer(mp.getDuration()));
mp.setVolume(0.5f, 0.5f);
mp.setLooping(false);
}
});
mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
@Override
public void onBufferingUpdate(MediaPlayer mp, int i) {
double ratio = i / 100.0;
int bufferingLevel = (int) (mp.getDuration() * ratio);
seekBar.setSecondaryProgress(bufferingLevel);
}
});
}
private void updateSeekbar(){
if(mediaPlayer.isPlaying()){
int currPos = mediaPlayer.getCurrentPosition();
seekBar.setProgress(currPos);
runnable = new Runnable() {
@Override
public void run() {
updateSeekbar();
long currentDuration = mediaPlayer.getCurrentPosition();
textCurrentTime.setText(milliSecondsToTimer(currentDuration));
textTotalDuration.setText(milliSecondsToTimer(mediaPlayer.getDuration() - currentDuration));
}
};
handler.postDelayed(runnable,100);
}
}
private String milliSecondsToTimer(long milliSeconds){
String timerString = "";
String secondsString;
int hours = (int) (milliSeconds / (1000 * 60 * 60));
int minutes = (int) (milliSeconds % (1000 * 60 * 60)) / (1000 * 60);
int seconds = (int) ((milliSeconds % (1000 * 60 * 60)) % (1000 * 60) / 1000);
if(hours > 0){
timerString = hours + ":";
}
if(seconds < 10){
secondsString = "0" + seconds;
}else {
secondsString = "" + seconds;
}
timerString = timerString + minutes + ":" + secondsString;
return timerString;
}
private void retriveData(String bid) {
FirebaseDatabase.getInstance().getReference().child("SiddhaMedicine").child("Surgery").child(bid).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
String x = snapshot.child("Body").getValue().toString();
String y = x.replace("n","\r\n\n");
tvTitle.setText(snapshot.child("Title").getValue().toString());
tvBody.setText(y);
Glide.with(BodyActivity.this).load(snapshot.child("ImgUrl").getValue().toString()).placeholder(R.drawable.ic_launcher_background).into(imageView);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
0 Comments