可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am using MediaController.MediaPlayerControl in order to display a MediaController at the bottom of my Custom View but I can't get it to work properly. When I play music for first time then there should be pause button visible but instead there is play and when I press that button then the music is paused correctly and state remains the same and after that its working properly. Also when I play next song, the old MediaController widget gets overlapped with the new one. And sometimes the progress/seek bar doesn't refresh while the music is playing. It just updates itself when something on the MediaController is pressed (Play/Pause, forward, etc).
I found these questions similar to mine but I don't think the answers they got will solve my problem.
- Android mediacontroller Play Pause controls not refresh properly
- Android MediaController seekbar not refreshing
- Android VideoView Playback Controls Show "Play" Initially Instead of "Pause" Even Though File is Already Playing
This is how I initialize the 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 is how I show controller:
public void playMusic() { musicSrv.playSong(); //Play song in a service setController(); controller.show(0); controller.requestFocus(); }
回答1:
I had exactly this problem. Don't know if you still need help, but I thought I'd post anyway. For posterity, are you following this tutorial?
First, the easy problem: you're getting multiple instances of your controls because of repeated calls to setController()
. Change the first line of your function to:
if (controller == null) controller = new MusicController(this);
With regards to the play button malfunctioning, I believe it's because you're showing it before the music player has been prepared (disclaimer: I'm a newbie to Android myself, and the following are the things I've found to have worked).
Set up a broadcast from your music-playing service to notify your music-controlling activity when the musicplayer has been prepared. Append the following function in your music-playing service to broadcast intent:
@Override public void onPrepared(MusicPlayer player) { // Do some other stuff... // Broadcast intent to activity to let it know the media player has been prepared Intent onPreparedIntent = new Intent("MEDIA_PLAYER_PREPARED"); LocalBroadcastManager.getInstance(this).sendBroadcast(onPreparedIntent); }
Set up a broadcast receiver in your music-controlling activity to receive the intent broadcast by your service. Add the following class to your activity:
// Broadcast receiver to 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 your receiver in your activity's onResume()
method:
// Set up receiver for media player onPrepared broadcast LocalBroadcastManager.getInstance(this).registerReceiver(onPrepareReceiver, new IntentFilter("MEDIA_PLAYER_PREPARED"));
Depending on the structure of your code, you'll have to do some general tidying up. In my code, I've only called setController()
twice: from the onCreate()
and onResume()
methods in my activity. I also only call controller.show(0)
from my broadcast receiver, and my onResume()
method.