Android Java: Play & Pause Sound With Volume And Scrub Seek Bar

 

 

Playing a sound file and including controlling options is not easy as playing a video file. Android Oreo doesn’t provide any MediaController instance for the purpose.

How To Shot It!
  1. Create a folder named “raw” (any name) under the resource folder(app->res->raw) and save the sound file. Save all required image files (play and pause image) under the drawable folder
  2. Create a MediaPlayer instance for the sound file
  3. Create an onClick() method for play and pause controls
  4. Include a srub seek bar for progress control
  5. Create an AudioManger instance and seek bar for volume control

 

//MediaPlayer instance
mediaPlayer = MediaPlayer.create(this, R.raw.soundFileName);
Play & Pause
//onClick method
boolean mediaIsPlaying= false;

public  void play(View view){
    ImageView controlButton= findViewById(R.id.controlButtonImageViewId);
    if(!mediaIsPlaying){
        controlButton.setImageResource(R.drawable.pauseImageFileName);
        mediaPlayer.start();
        mediaIsPlaying= false;
    }
    else{
        controlButton.setImageResource(R.drawable.playImageFileName);
        mediaPlayer.pause();
        mediaIsPlaying= true;
    }
}
Progress Bar
//Scrub Seek Bar for progress control
final SeekBar scrubSeekBar  = (SeekBar) findViewById(R.id.scrubSeekBarId);

//set the duration for seekbar to be the duration of sound file playing
scrubSeekBar.setMax(mediaPlayer.getDuration());

//set the actions on scrolling the seek bar
scrubSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){

    @Override
    //Change the sound file progress to match the seek bar progress
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
          mediaPlayer.seekTo(progress);
    }

    @Override
    //Pause the sound while scrolling the seek bar
    public void onStartTrackingTouch(SeekBar seekBar) {
         mediaPlayer.pause();

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        mediaPlayer.start();

    }
});

//set the seek bar progress in accordance to the sound play progress
new Timer().scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
        scrubSeekBar.setProgress(mediaPlayer.getCurrentPosition());
    }
},0,100);
Volume Control
//Volume SeekBar
//Create an AudioManager instance for accessing the system audio
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);

//grab system's max and current volume
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

//assign system volume to seekbar volume
SeekBar volumeControl = (SeekBar) findViewById(R.id.volumeSeekBarId);
volumeControl.setMax(maxVolume);
volumeControl.setProgress(currentVolume);
volumeControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
     @Override
     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,progress,0);

      }

      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {
      }

      @Override
      public void onStopTrackingTouch(SeekBar seekBar) {
      }});

Happy Coding!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s