在通知的音乐播放器控制播放器、通知、音乐

2023-09-03 20:38:54 作者:现实太现实

如何设置通知与播放/暂停,下一首和previous按钮,在Android的!

how to set notification with play/pause, next and previous button in android.!

我是新与Android和放大器;此外,在堆栈溢出。所以,请多多包涵。

I am new with Android & also at stack overflow. So please bear with me.

我设置的通知时,歌曲开始播放象下面这样:

I set notification when song is start to play like below :

`

@SuppressLint("NewApi")
public void setNotification(String songName){
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager notificationManager = (NotificationManager) getSystemService(ns);


    @SuppressWarnings("deprecation")
    Notification notification = new Notification(R.drawable.god_img, null, System.currentTimeMillis());

    RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.notification_mediacontroller);

    //the intent that is started when the notification is clicked (works)
    Intent notificationIntent = new Intent(this, AudioBookListActivity.class);
    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    notification.contentView = notificationView;
    notification.contentIntent = pendingNotificationIntent;
    notification.flags |= Notification.FLAG_NO_CLEAR;

    //this is the intent that is supposed to be called when the button is clicked
    Intent switchIntent = new Intent(this, AudioPlayerBroadcastReceiver.class);
    PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, switchIntent, 0);

    notificationView.setOnClickPendingIntent(R.id.btn_play_pause_in_notification, pendingSwitchIntent);
    notificationManager.notify(1, notification);        
}

`

我已创建BroadcastReceiver的象下面这样:  `

I have create BroadcastReceiver like below : `

   private class AudioPlayerBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        System.out.println("intent action = " + action);
        long id = intent.getLongExtra("id", -1);

        if(Constant.PLAY_ALBUM.equals(action)) {
            //playAlbum(id);
        } else if(Constant.QUEUE_ALBUM.equals(action)) {
            //queueAlbum(id);
        } else if(Constant.PLAY_TRACK.equals(action)) {
            //playTrack(id);
        } else if(Constant.QUEUE_TRACK.equals(action)) {
            //queueTrack(id);
        } else if(Constant.PLAY_PAUSE_TRACK.equals(action)) {
 //                playPauseTrack();
            System.out.println("press play");
        } else if(Constant.HIDE_PLAYER.equals(action)) {
 //                hideNotification();
            System.out.println("press next");
        }
        else {
        }
    }

}`

现在,我成功设置自定义通知,但我怎么能处理通知按钮和事件,如播放/暂停,previous和明年......等我也尝试使用广播接收器,但不能得到任何回应。

Now, I set custom notification successfully but how can i handle notification buttons and its events like play/pause, previous and next... etc. I also try using broadcast receiver but could not get any response.

寻求专家的解决方案和指导,请帮助我。

Seeking solution and guidance from experts, please help me out.

在此先感谢。

推荐答案

您需要设置一个自定义的意图行动,而不是 AudioPlayerBroadcastReceiver 组件类。

You need to set a custom intent action, not the AudioPlayerBroadcastReceiver component class.

像这样的自定义操作的名称创建一个意图

Create a Intent with custom action name like this

  Intent switchIntent = new Intent("com.example.app.ACTION_PLAY");

然后,注册 PendingIntent 广播接收

  PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 100, switchIntent, 0);

然后,设置一个的onClick 的播放控制,怎么做,如果需要其他控件类似的自定义操作。

Then, set a onClick for the play control , do similar custom action for other controls if required.

  notificationView.setOnClickPendingIntent(R.id.btn_play_pause_in_notification, pendingSwitchIntent);

接下来,注册在 AudioPlayerBroadcastReceiver 自定义操作这样

   <receiver android:name="com.example.app.AudioPlayerBroadcastReceiver" >
        <intent-filter>
            <action android:name="com.example.app.ACTION_PLAY" />
        </intent-filter>
    </receiver>

最后,当比赛被点击通知 RemoteViews 的布局,您将收到玩动作的BroadcastReceiver

Finally, when play is clicked on Notification RemoteViews layout, you will receive the play action by the BroadcastReceiver

public class AudioPlayerBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {

    String action = intent.getAction();

    if(action.equalsIgnoreCase("com.example.app.ACTION_PLAY")){
        // do your stuff to play action;
    }
   }
}

编辑:如何设置意图过滤器在code注册的广播接收器的

您还可以设置自定义操作意图过滤器从code为注册广播接收器像这样

You can also set the Custom Action through Intent filter from code for the registered Broadcast receiver like this

    // instance of custom broadcast receiver
    CustomReceiver broadcastReceiver = new CustomReceiver();

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
    // set the custom action
    intentFilter.addAction("com.example.app.ACTION_PLAY");
    // register the receiver
    registerReceiver(broadcastReceiver, intentFilter);