通知中的音乐播放器控制

 欣欣大妮 发布于 2023-01-17 10:30

如何在android中设置播放/暂停,下一个和上一个按钮的通知.

我是Android的新手,也是堆栈溢出的新手.所以请耐心等待.

在此输入图像描述

我开始播放歌曲时设置通知,如下所示:

`

@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,如下所示:`

   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 {
        }
    }

}`

现在,我成功设置了自定义通知,但我如何处理通知按钮及其事件,如播放/暂停,上一个和下一个......等等.我也尝试使用广播接收器,但无法得到任何响应.

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

提前致谢.

1 个回答
  • 您需要设置a custom intent action,而不是AudioPlayerBroadcastReceiver组件类.

    使用这样的自定义操作名称创建一个Intent

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

    然后,注册PendingIntent Broadcast接收器

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

    然后,onClick为播放控件设置a ,如果需要,为其他控件执行类似的自定义操作.

      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>
    

    最后,点击播放时Notification RemoteViews的布局,您将收到play actionBroadcastReceiver

    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;
        }
       }
    }
    

    编辑:如何设置代码中注册的广播接收器的intent过滤器

    您还可以设置Custom Action通过Intent filter 从代码注册Broadcast receiver 这样

        // 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); 
    

    2023-01-17 10:32 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有