title: How to get duration of Audio Stream and continue audio streaming from any point
body: <p><strong>Description:</strong></p>
  
  <p>I have following code for an Audio player. I can continue Audio playback from any duration by clicking on Progress Bar(between 0-to-mediaplayer.getDuration()). It is working perfectly for Audio playback.</p>
  
  <p><strong>Problem in Audio Streaming:</strong></p>
  
  <ul>
  <li>When I stream an Audio file from an internet server (say s3-bucket)
  it starts streaming correctly.</li>
  <li>But mediaPlayer.getDuration() and mediaPlayer.getCurrentPosition()
  return wrong values. At the beginning of streaming
  mediaPlayer.getCurrentPosition() returns 5 hours.</li>
  <li>Due to this I am not able to continue Audio streaming from a
  specified duration of Stream (0-to-Stream Duration).</li>
  </ul>
  
  <p><strong>Questions:</strong></p>
  
  <ol>
  <li>How can I get the duration of an Audio stream</li>
  <li>How can I continue Audio Streaming from a specified duration. For
  example for a file of 10-minute duration, I want to start streaming from 6th
  minute.</li>
  </ol>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>public class MyAudioPlayer extends Activity 
  implements OnClickListener{
  
  
      MediaPlayer mediaPlayer = null;
      private boolean isPaused=false;
      private boolean isStop = true;
  
      String filePath = null;
      String productName = null;
  
      ImageButton btnPlay = null;
      ImageButton btnPause = null;
      ImageButton btnReset = null;
      ImageButton btnStop = null;
  
      AudioManager audioManager = null;
      SeekBar volControl = null;
      SeekBar progressControl = null;
      TextView progressText = null;
      long durationInMillis = -1;
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.ltd_audio_player);
  
          volControl = (SeekBar)findViewById(R.id.player_volume);
          progressControl = (SeekBar)findViewById(R.id.player_seekbar);
          progressText = (TextView) findViewById(R.id.player_progress_text);
  
          btnPlay = (ImageButton) findViewById(R.id.ic_player_play); 
  
          btnPause = (ImageButton) findViewById(R.id.ic_player_pause);  
  
          btnReset = (ImageButton) findViewById(R.id.ic_player_reset); 
  
          btnStop = (ImageButton) findViewById(R.id.ic_player_stop);   
  
          btnPlay.setOnClickListener(this);
          btnPause.setOnClickListener(this);
          btnReset.setOnClickListener(this);
          btnStop.setOnClickListener(this);
  
          filePath = getIntent().getExtras().getString("localPath");
  
          this.setPlayer();
          this.resetAndStartPlayer();
  
  
      }
  
      @Override
      protected void onResume() {
          super.onResume();   
          isPaused=false;
          progressText.postDelayed(onEverySecond, 1000);
      }
  
      @Override
      protected void onPause() {
          super.onPause();
  
          isPaused=true;
      }
      private void setProgressControl() {
          int maxVolume = mediaPlayer.getDuration();
          int curVolume = mediaPlayer.getCurrentPosition();
  
          progressControl.setMax(maxVolume);
          progressControl.setProgress(curVolume);
          progressControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  
              @Override
              public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) {
                  mediaPlayer.seekTo(progress);
              }
  
              @Override
              public void onStartTrackingTouch(SeekBar seekBar) {
                  // TODO Auto-generated method stub
  
              }
  
              @Override
              public void onStopTrackingTouch(SeekBar seekBar) {
                  // TODO Auto-generated method stub
  
              }
          });     
      }
      @Override
      public void onClick(View v) {
          switch(v.getId()){
          case R.id.ic_player_play:
              if(isStop==true){
                  try {
                      mediaPlayer.prepareAsync();
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }else{
                  mediaPlayer.start();
                  isStop = true;
              }
              break;
          case R.id.ic_player_pause:
              mediaPlayer.pause();
              break;
          case R.id.ic_player_reset:
              mediaPlayer.seekTo(0);
              break;
          case R.id.ic_player_stop:
              isStop = true;
              progressControl.setProgress(0);
              mediaPlayer.stop();
              break;
          }
  
      }
      private void resetAndStartPlayer(){
          try {
              if(filePath!=null){
                  mediaPlayer.setDataSource(filePath);
                  mediaPlayer.prepareAsync();
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
      private void setPlayer(){
  
          getWindow().setFormat(PixelFormat.UNKNOWN);
          mediaPlayer = new MediaPlayer();    
  
          mediaPlayer.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
  
              @Override
              public void onBufferingUpdate(MediaPlayer mp, int percent) {
                  progressControl.setSecondaryProgress((progressControl.getMax()/100)*percent);
  
              }
          });
          mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
  
              @Override
              public void onPrepared(MediaPlayer mp) {
                  mediaPlayer.start();
                  isStop=false;
                  durationInMillis = mediaPlayer.getDuration();
                  MyAudioPlayer.this.setProgressControl();
              }
          });
          mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
      }
      @Override
      protected void onDestroy() {
          // TODO Auto-generated method stub
          mediaPlayer.release();
          super.onDestroy();
      }
      protected void setProgressText() {
          durationInMillis = mediaPlayer.getDuration();
          int curVolume = mediaPlayer.getCurrentPosition();
          long HOUR = 60*60*1000;
          if(progressText!=null){
              if(durationInMillis&gt;HOUR){
                  progressText.setText(String.format("%1$tH:%1$tM:%1$tS", new Date(curVolume))
                          +" / "+String.format("%1$tH:%1$tM:%1$tS", new Date(durationInMillis)));
              }else{
                  progressText.setText(String.format("%1$tM:%1$tS", new Date(curVolume))
                          +" / "+String.format("%1$tM:%1$tS", new Date(durationInMillis)));
              }
          }       
      }
      private Runnable onEverySecond=new Runnable() {
          public void run() {
  
              if (mediaPlayer!=null) {
                  progressControl.setProgress(mediaPlayer.getCurrentPosition());
  
                  MyAudioPlayer.this.setProgressText();
              }
  
              if (!isPaused) {
                  progressText.postDelayed(onEverySecond, 1000);
              }
          }
      };
  }
  </code></pre>
  
  <p>Time is displayed above the progress bar.</p>
  
  <p><strong>Time: 'Current Duration'/'Total Duration'</strong></p>
  
  <p><img src="https://i.stack.imgur.com/iGvf4.png" alt="enter image description here"></p>