安卓的MediaController播放/暂停按钮和搜索栏不刷新 [英] Android MediaController Play/Pause button and Seekbar doesn't Refresh
问题描述
我使用MediaController.MediaPlayerControl为了显示的MediaController在我的自定义视图的底部,但我不能让它正常工作。当我播放音乐的第一次那么就应该暂停按钮可见,但取而代之的是戏,当我preSS的按钮,然后在音乐的正确暂停,状态保持不变,之后它的正常工作。此外,当我玩下一首歌曲,老的MediaController部件被重叠的新的。有时进度/搜索,而音乐播放栏不刷新。它只是更新自己的东西时,对的MediaController是pressed(播放/暂停,前进,等等)。
我发现这些问题,类似于我的,但我不认为他们有将解决我的问题的答案。
- <一个href="http://stackoverflow.com/questions/11682680/android-mediacontroller-play-pause-controls-not-refresh-properly">Android的MediaController播放暂停控制不正确刷新
- <一个href="http://stackoverflow.com/questions/12011631/android-mediacontroller-seekbar-not-refreshing">Android的MediaController搜索栏没有刷新
- <一个href="http://stackoverflow.com/questions/15582818/android-videoview-playback-controls-show-play-initially-instead-of-pause-eve">Android VideoView播放控件显示&QUOT;播放&QUOT;最初,而不是&QUOT;暂停&QUOT;即使文件已在播放
这是我如何初始化控制器:
私人无效setController()
{
控制器=新MusicController(本);
controller.set prevNextListeners(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
playNext();
}
},新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
玩preV();
}
});
controller.setMediaPlayer(本);
controller.setAnchorView(findViewById(R.id.song_list));
controller.setEnabled(真正的);
}
这是我怎么显示控制器:
公共无效playMusic()
{
musicSrv.playSong(); //播放歌曲的服务
setController();
controller.show(0);
controller.requestFocus();
}
我有完全相同这个问题。不知道你是否还需要帮助,但我想我反正发布。对于后人,你下面的<一个href="http://$c$c.tutsplus.com/tutorials/create-a-music-player-on-android-project-setup--mobile-22764">this教程?
一,简单的问题:你得到你的控制,因为重复调用的多个实例 setController()
。你的函数的第一行更改为:
如果(控制== NULL)控制器=新MusicController(本);
至于播放按钮失灵,我的认为的,那是因为你展示它之前的音乐播放器一直是prepared(免责声明:我是一个新手到Android自己,以下是我已经发现有工作的事情)。
-
设置广播从你的音乐播放服务,通知你的音乐控制活动时musicplayer已经prepared。追加以下功能的音乐播放服务,广播意图:
@覆盖 公共无效于prepared(MusicPlayer播放器){ //做一些其他的东西... //广播意图的活动,让它知道媒体播放器已经prepared 热衷于preparedIntent =新的意向书(MEDIA_PLAYER_ prePARED); LocalBroadcastManager.getInstance(本).sendBroadcast(上preparedIntent); }
-
设置的广播接收器在你的音乐控制活动由服务接收意向广播。添加下面的类到你的活动:
//广播接收器,以确定何时音乐播放器一直是prepared 在prepareReceiver =新的BroadcastReceiver私人的BroadcastReceiver(){ @覆盖 公共无效的onReceive(上下文C,意图我){ //当音乐播放器一直是prepared,显示控制器 controller.show(0); } };
-
填写您的接收器在您的活动的
onResume()
方法://设置接收器上的$ P $媒体播放器ppared广播 LocalBroadcastManager.getInstance(本).registerReceiver(上prepareReceiver, 新的IntentFilter(MEDIA_PLAYER_ prePARED));
-
根据您的code中的结构,你就必须做一些常规整理起来。在我的code,我只叫
setController()
两次:从的onCreate()
和onResume()
方法在我的活动。我也只有叫controller.show(0)
从我的广播接收器,和我的onResume()
方法。
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();
}
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 theonCreate()
andonResume()
methods in my activity. I also only callcontroller.show(0)
from my broadcast receiver, and myonResume()
method.
这篇关于安卓的MediaController播放/暂停按钮和搜索栏不刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!