nafiefn_10

Untitled

Jun 20th, 2021
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.49 KB | None | 0 0
  1. package nofieanggraeni.blogspot.com.videoplayer;
  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.util.Log;
  9. import android.view.View;
  10. import android.widget.SeekBar;
  11. import android.widget.TextView;
  12. import android.os.Handler;
  13. import android.widget.VideoView;
  14.  
  15. import java.util.concurrent.TimeUnit;
  16. import androidx.appcompat.app.AppCompatActivity;
  17.  
  18. public class MainActivity extends AppCompatActivity {
  19.     private MediaPlayer mediaPlayer;
  20.     private TextView songName, songDuration;
  21.     private VideoView videoview;
  22.     private SeekBar seekBar;
  23.     private double timeStart = 0, finalTime = 0;
  24.     private int forwardTime = 20000,backwardTime = 20000;
  25.     private Handler durationHandler = new Handler();
  26.     private boolean swtch = false;
  27.     @Override
  28.     protected void onCreate(Bundle savedInstanceState) {
  29.         super.onCreate(savedInstanceState);
  30.         setContentView(R.layout.activity_main);
  31.         songName = (TextView) findViewById(R.id.songName);
  32.         songDuration = (TextView) findViewById(R.id.songDuration);
  33.         mediaPlayer = MediaPlayer.create(this, R.raw.shortmovie_annisa);
  34.         MediaMetadataRetriever meta = new MediaMetadataRetriever();
  35.         videoview = (VideoView) findViewById(R.id.videoplay);
  36.         String path = "android.resource://" + getPackageName() + "/" + R.raw.shortmovie_annisa;
  37.         videoview.setVideoURI(Uri.parse(path));
  38.         seekBar = (SeekBar) findViewById(R.id.seekBar);
  39.         songName.setText("Short Movie - Annisa.mp4");
  40.         seekBar.setOnSeekBarChangeListener(new yourListener());
  41.         seekBar.setClickable(false);
  42.  
  43.     }
  44.     private class yourListener implements SeekBar.OnSeekBarChangeListener {
  45.         public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  46.             if(swtch) {
  47.                 videoview.seekTo(progress);
  48.             }
  49.         }
  50.         public void onStartTrackingTouch(SeekBar seekBar) {
  51.             swtch = true;
  52.         }
  53.         public void onStopTrackingTouch(SeekBar seekBar) {
  54.             swtch = false;
  55.         }
  56.     }
  57.  
  58.     private Runnable updateSeekBarTime = new Runnable() {
  59.         public void run() {
  60.             timeStart = videoview.getCurrentPosition();
  61.             seekBar.setProgress((int) timeStart);
  62.             double timeRemaining = finalTime - timeStart;
  63.             songDuration.setText(String.format("%d min, %d sec",
  64.                     TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining),
  65.                     TimeUnit.MILLISECONDS.toSeconds((long) timeRemaining) -
  66.             TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) timeRemaining))));
  67.             durationHandler.postDelayed(this, 1000);
  68.         }
  69.     };
  70.     public void play(View view){
  71.       videoview.start();
  72.         finalTime = videoview.getDuration();
  73.         seekBar.setMax((int) videoview.getDuration());
  74.         timeStart = videoview.getCurrentPosition();
  75.         seekBar.setProgress((int) timeStart);
  76.         durationHandler.postDelayed(updateSeekBarTime, 1000);
  77.     }
  78.     public  void pause(View view){
  79.         videoview.pause();
  80.     }
  81.     public void forward(View view){
  82.         timeStart = timeStart + forwardTime;
  83.         videoview.seekTo((int) timeStart);
  84.     }
  85.     public void backforward(View view) {
  86.         timeStart = timeStart - backwardTime;
  87.         videoview.seekTo((int) timeStart);
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment