安卓音乐播放器通知

6
如何从通知栏中的按钮控制音乐播放器,并如何监听通知栏中按钮的操作。

我对此一无所知,但我需要使用远程视图。请提供任何关于此的示例代码。 - Babin
请点击以下链接,您将得到正确的答案。完整代码 - Roadies
1个回答

10

您应该使用RemoteViews的setOnClickPendingIntent()方法来监听通知中按钮的操作。

setOnClickPendingIntent()方法将绑定一个PendingIntent来响应按钮点击事件。

示例代码如下:

private void showControllerInNotification() {       
    PendingIntent pendingIntent = null;
    Intent intent = null;


    //Inflate a remote view with a layout which you want to display in the notification bar.
    if (mRemoteViews == null) {
        mRemoteViews = new RemoteViews(getPackageName(),
                R.layout.notification_control_bar);
    }   

    //Define what you want to do after clicked the button in notification.
    //Here we launcher a service by an action named "ACTION_STOP" which will stop the music play.
    intent = new Intent(ACTION_STOP);       
    pendingIntent = PendingIntent.getService(getApplicationContext(),
            REQUEST_CODE_STOP, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    //In R.layout.notification_control_bar,there is a button view identified by bar_btn_stop
    //We bind a pendingIntent with this button view so when user click the button,it will excute the intent action.
    mRemoteViews.setOnClickPendingIntent(R.id.bar_btn_stop,
            pendingIntent);

    //Create the notification instance.
    mNotification = new NotificationCompat.Builder(getApplicationContext())
            .setSmallIcon(R.drawable.ic_launcher).setOngoing(true)
            .setWhen(System.currentTimeMillis())                
            .setContent(mRemoteViews)
            .build();

    //Show the notification in the notification bar.
    mNotifiManager.notify(NOTIFICATION_ID, mNotification);      
}   

更新 1:

负责响应此操作的服务如下所示:

public class MediaPlayerService extends Service {

    public static final String ACTION_STOP="xxx.yyy.zzz.ACTION_STOP";
    public static final String ACTION_PLAY="xxx.yyy.zzz.ACTION_PLAY";
    public static final String ACTION_PAUSE="xxx.yyy.zzz.ACTION_PAUSE";

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null) {
        String action = intent.getAction();         
        if (!TextUtils.isEmpty(action)) {
            if (action.equals(ACTION_PLAY)) {
                startPlay();
            }else if(action.equals(ACTION_PAUSE)) {
                pausePlay();
            }else if(action.equals(ACTION_STOP)) {
                stopPlay();
            }
        }
    }
    return super.onStartCommand(intent, flags, startId);
}
private void stopPlay(){
    // do the play work here
}
private void stopPause(){
    // do the pause work here
}
private void stopPlay(){
    // do the stop work here
}

}

在清单文件中注册服务:

    <service
        android:name="xxx.yyy.zzz.MusicPlayService"
        android:exported="false" >
        <intent-filter>
            <action android:name="xxx.yyy.zzz.ACTION_PLAY" />
            <action android:name="xxx.yyy.zzz.ACTION_PAUSE" />
            <action android:name="xxx.yyy.zzz.ACTION_STOP" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </service>

关于音乐播放控制,你可以从这里复制一些有用的代码片段。


@AvatarQing 给了我一个建议。我使用 RemoteView 构建了一个通知大视图来控制播放/暂停,就像这个链接(stackoverflow.com/questions/14508369/…)中的示例一样。所有的都没问题,但是当我点击设备的返回按钮并从应用程序退出后,点击事件(播放/暂停/前进/关闭)按钮就不起作用了。请帮帮我。 - Helal Khan
你是在Activity中实现了播放/暂停控制,而不是在Service中吗? - Riki
请问您能否解释一下服务中的意图过滤器是什么? - juztcode
1
@juztcode,那些是你自定义的操作,你可以定义它们。 - Riki
@AvatarQing,这些是可选的还是必需的?我按照教程做了,但没有提到这些。 - juztcode
@juztcode,该操作对应于您传递给PendingIntent.getService()的意图,是否必要取决于您的需求。 - Riki

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接