Android MediaController Play/Pause button and Seekbar doesn't Refresh -
i using mediacontroller.mediaplayercontrol in order display mediacontroller @ bottom of custom view can't work properly. when play music first time there should pause button visible instead there play , when press button music paused correctly , state remains same , after working properly. when play next song, old mediacontroller widget gets overlapped new one. , progress/seek bar doesn't refresh while music playing. updates when on mediacontroller pressed (play/pause, forward, etc).
i found these questions similar mine don't think answers got solve problem.
- android mediacontroller play pause controls not refresh properly
- android mediacontroller seekbar not refreshing
- android videoview playback controls show "play" instead of "pause" though file playing
this how initialize controller:
private void setcontroller() { controller = new musiccontroller(this); controller.setprevnextlisteners(new view.onclicklistener() { @override public void onclick(view v) { playnext(); } }, new view.onclicklistener() { @override public void onclick(view v) { playprev(); } }); controller.setmediaplayer(this); controller.setanchorview(findviewbyid(r.id.song_list)); controller.setenabled(true); }
this how show controller:
public void playmusic() { musicsrv.playsong(); //play song in service setcontroller(); controller.show(0); controller.requestfocus(); }
i had problem. don't know if still need help, thought i'd post anyway. posterity, following this tutorial?
first, easy problem: you're getting multiple instances of controls because of repeated calls setcontroller()
. change first line of function to:
if (controller == null) controller = new musiccontroller(this);
with regards play button malfunctioning, believe it's because you're showing before music player has been prepared (disclaimer: i'm newbie android myself, , following things i've found have worked).
set broadcast music-playing service notify music-controlling activity when musicplayer has been prepared. append following function in music-playing service broadcast intent:
@override public void onprepared(musicplayer player) { // other stuff... // broadcast intent activity let know media player has been prepared intent onpreparedintent = new intent("media_player_prepared"); localbroadcastmanager.getinstance(this).sendbroadcast(onpreparedintent); }
set broadcast receiver in music-controlling activity receive intent broadcast service. add following class activity:
// broadcast receiver determine when music player has been prepared private broadcastreceiver onpreparereceiver = new broadcastreceiver() { @override public void onreceive(context c, intent i) { // when music player has been prepared, show controller controller.show(0); } };
register receiver in activity's
onresume()
method:// set receiver media player onprepared broadcast localbroadcastmanager.getinstance(this).registerreceiver(onpreparereceiver, new intentfilter("media_player_prepared"));
depending on structure of code, you'll have general tidying up. in code, i've called
setcontroller()
twice:oncreate()
,onresume()
methods in activity. callcontroller.show(0)
broadcast receiver, ,onresume()
method.
Comments
Post a Comment