0

I have developed a Music player application that is playing both audio and video songs, it has features like play,pause,progress bar showing song's progress, now when going back from that MusicPlayerActivity, music is running in background , now i want to go to this MusicPlayerActivity from another activity to pause the song, i have used Intent for switching , But i am not getting the value of progress bar i.e how much song is played, although song is playing in background.Please anyone help me, How to store the duration of song and states of UI. Please provide some help or tutorial or link. Thanks in advance.

2 Answers2

0

In my application I have music player implemented as background service. In that service I send broadcast messages every 500ms where I provide info about current position. When player is paused I stop sending messages. And in every activity I register for listening to this messages so I am able to update visible player. For sending messages I use handler which runs on background thread.

private Runnable mUpdateProgressRunnable = new Runnable() {

        @Override
        public void run() {
            if (mMediaPlayer != null && !mSeeking) {
                int duration = mMediaPlayer.getDuration();
                int currentPosition = mMediaPlayer.getCurrentPosition();
                Bundle params = new Bundle();
                params.putInt(Broadcaster.P_PLAYER_DURATION, duration);
                params.putInt(Broadcaster.P_PLAYER_POSITION, currentPosition);
                getRferl().getBroadcaster().send(Broadcaster.E_PLAYER_CURRENT_POSITION, params);
                mUpdateProgressHandler.postDelayed(this, 500);
            }
        }
    };

Rferl is my application,Broadcaster is just wrapper for LocalBroadcastManager

pcu
  • 2,745
  • 4
  • 18
  • 22
  • thanks on answering my question but i am not implemented any service, i have used Handler to update the progress bar continously , how can i get this handler object on recreating the activity, thus i also need to store the MediaPlayer object ? –  Aug 27 '12 at 11:12
  • This is why I implemented service which hold media player and every activity can listen to broadcast and updates status of progressbar. Sorry I can not help you, I did not implemented media player this way. I try to not hold shared objects in activities, so I have little experience have to recreate it correctly. I use application object or background services for this purpose. It is much easier for me :) – pcu Aug 27 '12 at 11:50
  • thanks for your reply @pcu , can you provide any source code for this type of implementation , My email id is - dhiraj.linkites@gmail.com –  Aug 27 '12 at 11:56