BenTibnam

Rough Audio Player Android

Nov 23rd, 2020 (edited)
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.08 KB | None | 0 0
  1. package com.example.userinterface;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.media.AudioManager;
  6. import android.media.MediaPlayer;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.SeekBar;
  11. import android.widget.TextView;
  12.  
  13. import org.w3c.dom.Text;
  14.  
  15. import java.util.Timer;
  16. import java.util.TimerTask;
  17.  
  18. public class AudioControl extends AppCompatActivity {
  19.     private MediaPlayer mediaPlayer;
  20.     private Button pauseButton;
  21.     private Button playButton;
  22.     private SeekBar seekBarPosition;
  23.     private SeekBar seekBarVolume;
  24.     private TextView currentPositionText;
  25.     private TextView totalTimeText;
  26.     private TextView totalVolume;
  27.     private AudioManager audioManager;
  28.  
  29.     private String msToSeconds(int i){
  30.         int seconds = i / 1000;
  31.         double minutes = 0;
  32.         if(seconds > 60)
  33.             minutes = seconds / 60;
  34.         seconds -= (minutes * 60);
  35.         return Integer.toString((int) Math.round(minutes)) + ":" + ((seconds < 10) ? "0" + Integer.toString(seconds) : Integer.toString(seconds));
  36.     }
  37.  
  38.     @Override
  39.     protected void onCreate(Bundle savedInstanceState) {
  40.         super.onCreate(savedInstanceState);
  41.         setContentView(R.layout.activity_audio_control);
  42.  
  43.         initializeWidgets();
  44.         audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
  45.         seekBarVolume.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
  46.         int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
  47.  
  48.         setSeekbars(maxVolume);
  49.  
  50.     }
  51.  
  52.     private void setSeekbars(int maxVolume) {
  53.  
  54.         seekBarPosition.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  55.             @Override
  56.             public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
  57.                 currentPositionText.setText(msToSeconds(i));
  58.             }
  59.  
  60.             @Override
  61.             public void onStartTrackingTouch(SeekBar seekBar) {
  62.  
  63.             }
  64.  
  65.             @Override
  66.             public void onStopTrackingTouch(SeekBar seekBar) {
  67.                 mediaPlayer.seekTo(seekBar.getProgress());
  68.             }
  69.         });
  70.  
  71.         seekBarVolume.setMax(maxVolume);
  72.  
  73.         seekBarVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  74.             @Override
  75.             public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
  76.                 audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, i, 0);
  77.             }
  78.  
  79.             @Override
  80.             public void onStartTrackingTouch(SeekBar seekBar) {
  81.  
  82.             }
  83.  
  84.             @Override
  85.             public void onStopTrackingTouch(SeekBar seekBar) {
  86.  
  87.             }
  88.         });
  89.  
  90.     }
  91.  
  92.     private void initializeWidgets() {
  93.         mediaPlayer = MediaPlayer.create(this, R.raw.thescotts);
  94.         pauseButton = (Button) findViewById(R.id.pauseBtn);
  95.         playButton = (Button) findViewById(R.id.playBtn);
  96.         seekBarPosition = (SeekBar) findViewById(R.id.seekBarTime);
  97.         seekBarVolume = (SeekBar) findViewById(R.id.seekBarVolume);
  98.         currentPositionText = (TextView) findViewById(R.id.positionText);
  99.         totalTimeText = (TextView) findViewById(R.id.totalTimeText);
  100.         totalVolume = (TextView)findViewById(R.id.volumeText);
  101.         seekBarPosition.setMin(0);
  102.         seekBarPosition.setMax(mediaPlayer.getDuration());
  103.         seekBarVolume.setMin(0);
  104.         seekBarVolume.setMax(100);
  105.         currentPositionText.setText(Integer.toString(mediaPlayer.getCurrentPosition()));
  106.         totalTimeText.setText(msToSeconds(mediaPlayer.getDuration()));
  107.  
  108.         new Timer().scheduleAtFixedRate(new TimerTask() {
  109.             @Override
  110.             public void run() {
  111.                 seekBarPosition.setProgress(mediaPlayer.getCurrentPosition());
  112.             }
  113.         }, 0, 300);
  114.     }
  115.  
  116.     public void play(View view){
  117.         if(!mediaPlayer.isPlaying()) mediaPlayer.start();
  118.     }
  119.  
  120.     public void pause(View view){
  121.         if(mediaPlayer.isPlaying()) mediaPlayer.pause();
  122.     }
  123.  
  124.  
  125. }
Add Comment
Please, Sign In to add comment