CaFc_Br40ck

MainActivity.java MusicPlayer PAB

Jun 21st, 2021
516
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package www.ariwibawa.my.id.musicplayer;
  2.  
  3. import android.content.res.AssetFileDescriptor;
  4. import android.media.MediaMetadataRetriever;
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.media.MediaPlayer;
  8. import android.provider.MediaStore;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.SeekBar;
  12. import android.widget.TextView;
  13. import android.os.Handler;
  14. import java.util.concurrent.TimeUnit;
  15. import androidx.appcompat.app.AppCompatActivity;
  16.  
  17. public class MainActivity extends AppCompatActivity {
  18.     private MediaPlayer mediaPlayer;
  19.     private TextView songName, songDuration;
  20.     private SeekBar seekBar;
  21.     private double timeStart = 0, finalTime = 0;
  22.     private int forwardTime = 20000, backwardTime = 20000;
  23.     private Handler durationHandler = new Handler();
  24.     private boolean swtch = false;
  25.     @Override
  26.     protected void onCreate(Bundle savedInstanceState) {
  27.         super.onCreate(savedInstanceState);
  28.         setContentView(R.layout.activity_main);
  29.         songName = (TextView) findViewById(R.id.songName);
  30.         songDuration = (TextView) findViewById(R.id.songDuration);
  31.         mediaPlayer = MediaPlayer.create(this, R.raw.music);
  32.  
  33.         MediaMetadataRetriever meta = new MediaMetadataRetriever();
  34.  
  35.         final AssetFileDescriptor afd=getResources().openRawResourceFd(R.raw.music);
  36.         meta.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
  37.  
  38.         String music_duration = meta.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
  39.         finalTime = Long.parseLong(music_duration);
  40.  
  41.         Log.v("musicDuration", "Duration : "+music_duration);
  42.  
  43.         seekBar = (SeekBar) findViewById(R.id.seekBar);
  44.         songName.setText("Nokia Ring Tone.mp3");
  45.         seekBar.setMax((int) finalTime);
  46.         seekBar.setOnSeekBarChangeListener(new yourListener() );
  47.         seekBar.setClickable(false);
  48.     }
  49.  
  50.     private class yourListener implements SeekBar.OnSeekBarChangeListener {
  51.         public void onProgressChanged(SeekBar seekBar, int progress,
  52.                                       boolean fromUser){
  53.             if(swtch){
  54.                 mediaPlayer.seekTo(progress);
  55.             }
  56.         }
  57.  
  58.         public void onStartTrackingTouch(SeekBar seekBar){
  59.             swtch = true;
  60.         }
  61.  
  62.         public void onStopTrackingTouch(SeekBar seekBar){
  63.             swtch = false;
  64.         }
  65.     }
  66.  
  67.     private Runnable updateSeekBarTime = new Runnable() {
  68.         @Override
  69.         public void run() {
  70.             timeStart = mediaPlayer.getCurrentPosition();
  71.             seekBar.setProgress((int) timeStart);
  72.             double timeRemaining = finalTime - timeStart;
  73.             songDuration.setText(String.format("%d min, %d sec",
  74.                     TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining),
  75.                     TimeUnit.MILLISECONDS.toSeconds((long) timeRemaining) -
  76.                     TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining))));
  77.             durationHandler.postDelayed(this, 1000);
  78.         }
  79.     };
  80.     public void play(View view){
  81.         mediaPlayer.start();
  82.         timeStart = mediaPlayer.getCurrentPosition();
  83.         seekBar.setProgress((int) timeStart);
  84.         durationHandler.postDelayed(updateSeekBarTime, 100);
  85.     }
  86.     public void pause(View view){
  87.         mediaPlayer.pause();
  88.     }
  89.     public void forward(View view){
  90.         timeStart = timeStart + forwardTime;
  91.         mediaPlayer.seekTo((int) timeStart);
  92.     }
  93.     public void backforward(View view){
  94.         timeStart = timeStart + backwardTime;
  95.         mediaPlayer.seekTo((int) timeStart);
  96.     }
  97. }
RAW Paste Data