I'm creating media player application that will run on service and all
its action (all intent actions stated in the manifest file of course)
What I did is, I created Runnable in my service that will
sendBroadcast whenever I do handler.postDelayed(myRunnable, 1000);
private Runnable updateActivity = new Runnable(){
@Override
public void run() {
if(mediaPlayer!=null)
{
Intent toActivity = new
Intent("com.share.play.SharePlayService");
toActivity.putExtra("position", position);
//32bit
toActivity.putExtra("id",
mediaPlayer.getAudioSessionId()); //
32bit
toActivity.putExtra("progress",mediaPlayer.getCurrentPosition()); //
32bit
toActivity.putExtra("duration",
mediaPlayer.getDuration()); //
32bit
toActivity.putExtra("bands", totalbands); // 5
x 32bit
toActivity.putExtra("loop", mpLoop); // 1bit
toActivity.putExtra("shuffle", mpShuffle);
//1bit
MyPlayService.this.sendBroadcast(toActivity);
handler.postDelayed(updateActivity, 1000); //
post again and
again every 1 sec
}
}};
And in my Activity I have receiver that will take the duration, and of
course the currentPosition() of the playing.
BroadcastReceiver ServiceReciever = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
Bundle b = arg1.getExtras();
if(b!=null)
{
seekbar.setMax(b.getInt("duration"));
seekbar.setProgress(b.getInt("progress"));
position=b.getInt("position");
sessionId = b.getInt("id");
if(b.getInt("position")>(visual.size()-1))
//Changing album
art whenever the current playing position is different from the
previous
{
position-=1;
}
else{
if(position > -1 && visual.size()>0 &&
!visual.isEmpty())
{
playingArtist.setText(visual.get(b.getInt("position")));
Drawable drawable = null;
try {
drawable = getArtwork();
//drawable =
drawable.getCurrent().
} catch (FileNotFoundException
e) {
// TODO Auto-generated
catch block
e.printStackTrace();
} catch
(IllegalArgumentException e) {
// TODO Auto-generated
catch block
e.printStackTrace();
}
art.setImageDrawable(drawable);
}
}
userLoop = b.getBoolean("loop");
userShuffle = b.getBoolean("shuffle");
int Minutes = (b.getInt("progress") % (1000*60*60)) /
(1000*60);
int Seconds = ((b.getInt("progress") % (1000*60*60)) %
(1000*60)) / 1000;
int Minutes2= (b.getInt("duration") % (1000*60*60)) /
(1000*60);
int seconds2 = ((b.getInt("duration") % (1000*60*60)) %
(1000*60)) / 1000;
timeline.setText(Integer.toString(Minutes) + ":"+
Seconds
+"/"+ Minutes2 +":"+ seconds2); //updates the time line.
}
}
};
More over, I unregister and register the receiver regularly whenever
the activity is visible to the user or not.
1. Is there are any side-effect of this approach?
2. Is this reasonable? Does the message queue get starved?
3. Is there any rather better approach than this one? Point me
some...
If you need some more explanation please post...
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en