title: Shared Preferences in music player
body: <p>I created a Music stream app that fetch the data from the firebase but i have a one issue in my app when i play a song and go back and come again then seek bar does not shows the current duration . So i think i should use Shared Preferences but i don't known how to use can any one suggest me the code for my music player </p>
  
  <pre><code> public class ViewUploadsActivity extends AppCompatActivity  implements 
        MediaPlayer.OnBufferingUpdateListener,View.OnTouchListener
                ,MediaPlayer.OnCompletionListener{
     //the listview
     ListView listView;
       private SeekBar seekBarProgress;
     private int mediaFileLengthInMilliseconds;
     private final Handler handler = new Handler();
     CustomArrayAdapter adapter;
    //database reference to get uploads data
     DatabaseReference mDatabaseReference;
  
   //list to store uploads data
    List&lt;Upload&gt; uploadList = new ArrayList&lt;&gt;();
    private TextView mSelectedTrackTitle,selected_track_ar;
     private ImageView mSelectedTrackImage;
     private MediaPlayer mMediaPlayer =new MediaPlayer();
    private ImageView mPlayerControl;
    Toolbar toolbar2;
  
  
    TextView length;
  
     @Override
     protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_view_upload);
      listView = (ListView) findViewById(R.id.listView);
       //status = (TextView) findViewById(R.id.status);
  
       seekBarProgress = (SeekBar)findViewById(R.id.length);
  
      listView.setStackFromBottom(true);
              mMediaPlayer = new MediaPlayer();
      mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  
     mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
          @Override
          public void onPrepared(MediaPlayer mp) {
              togglePlayPause();
          }
      });
    mMediaPlayer.setOnCompletionListener(new 
        MediaPlayer.OnCompletionListener() {
          @Override
          public void onCompletion(MediaPlayer mp) {
              mPlayerControl.setImageResource(R.drawable.ic_play);
          }
      });
      mSelectedTrackTitle = (TextView)findViewById(R.id.selected_track_title);
      mSelectedTrackImage = 
       (ImageView)findViewById(R.id.selected_track_image);
       mSelectedTrackTitle.setSelected(true);
       mPlayerControl = (ImageView)findViewById(R.id.player_control);
       selected_track_ar=(TextView)findViewById(R.id.selected_track_ar);
  
           mPlayerControl.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              togglePlayPause();
          }
      });
           get();
              //getting the database reference
           mDatabaseReference = FirebaseDatabase.getInstance().getReference
        (Constants.DATABASE_PATH_UPLOADS);
  
      //retrieving upload data from firebase database
      mDatabaseReference.addValueEventListener(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
              for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                  Upload upload = postSnapshot.getValue(Upload.class);
                  uploadList.add(upload);
              }
  
                 adapter = new CustomArrayAdapter(getApplicationContext(), 
              (ArrayList&lt;Upload&gt;) uploadList);
              listView.setAdapter(adapter);
  
           }
  
            @Override
           public void onCancelled(DatabaseError databaseError) {
  
          }
       });
  
  
       }
      @Override
       protected void onDestroy() {
      super.onDestroy();
  
     Intent svc=new Intent(this, MySongServices.class);
     startService(svc);
              }
     private void togglePlayPause() {
       if (mMediaPlayer.isPlaying()) {
          mMediaPlayer.pause();
           mPlayerControl.setImageResource(R.drawable.ic_play);
       } else if(!mMediaPlayer.isPlaying()){
           mMediaPlayer.start();
           mPlayerControl.setImageResource(R.drawable.ic_pause);
           mediaFileLengthInMilliseconds = mMediaPlayer.getDuration();
            primarySeekBarProgressUpdater();
                }
       }
  
        public void get()
       {
       seekBarProgress.setMax(99); // It means 100% .0-99
       seekBarProgress.setOnTouchListener(this);
       mMediaPlayer.setOnBufferingUpdateListener(this);
       mMediaPlayer.setOnCompletionListener(this);
       listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int 
      i, long l) {
              //getting the upload
              Upload upload = uploadList.get(i);
              mSelectedTrackTitle.setText(upload.getName());
              selected_track_ar.setText(upload.getAr());
              if (mMediaPlayer.isPlaying()) {
                  mMediaPlayer.stop();
                  mMediaPlayer.reset();
                  seekBarProgress.setProgress(0);
                                }
  
              try {
                   mMediaPlayer.setDataSource(upload.getUrl());
                  mMediaPlayer.prepare();
                  show();
  
              } catch (IOException e) {
                  e.printStackTrace();
              }
  
                          }
            });
  
  
  
        }
  
        private void primarySeekBarProgressUpdater() {
     try {
         seekBarProgress.setProgress((int) (((float) 
               mMediaPlayer.getCurrentPosition() 
            / mediaFileLengthInMilliseconds) * 100));
         if (mMediaPlayer.isPlaying()) {
             Runnable notification = new Runnable() {
                 public void run() {
                     primarySeekBarProgressUpdater();
                 }
             };
             handler.postDelayed(notification, 1000);
         }
      }
      catch (NullPointerException e)
      {
          e.printStackTrace();
       }
        }
  
  
  
  
     @Override
       public boolean onTouch(View v, MotionEvent event) {
        if(v.getId() == R.id.length){
            /** Seekbar onTouch event handler. Method which seeks MediaPlayer  
              to 
       seekBar primary progress position*/
         if(mMediaPlayer.isPlaying()){
              SeekBar sb = (SeekBar)v;
              int playPositionInMillisecconds = (mediaFileLengthInMilliseconds 
         / 100) * sb.getProgress();
             mMediaPlayer.seekTo(playPositionInMillisecconds);
          }
      }
      return false;
  
  
  }
  
  }
  </code></pre>