Giter Site home page Giter Site logo

Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent about audio_manager HOT 4 OPEN

nicks258 avatar nicks258 commented on July 17, 2024
Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent

from audio_manager.

Comments (4)

h2210316651 avatar h2210316651 commented on July 17, 2024 1

I manually overrided the plugin by replacing the contents of .pub-cache/hosted/pub.dartlang.org/audio_manager-0.8.2/android/src/main/java/cc/dync/audio_manager/MediaPlayerService.java

package cc.dync.audio_manager;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import java.util.Objects;

public class MediaPlayerService extends Service {
    private static final String ACTION_NEXT = "MediaPlayerService_next";
    private static final String ACTION_PREVIOUS = "MediaPlayerService_previous";
    private static final String ACTION_PLAY_OR_PAUSE = "MediaPlayerService_playOrPause";
    private static final String ACTION_STOP = "MediaPlayerService_stop";
    private static final String NOTIFICATION_CHANNEL_ID = "MediaPlayerService_1100";

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return serviceBinder;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // 取消Notification
        if (notificationManager != null)
            notificationManager.cancel(NOTIFICATION_PENDING_ID);
        stopForeground(true);
        // 停止服务
        stopSelf();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        setupNotification();
    }

    // 定义Binder类-当然也可以写成外部类
    private ServiceBinder serviceBinder = new ServiceBinder();

    public class ServiceBinder extends Binder {
        Service getService() {
            return MediaPlayerService.this;
        }
    }


    public enum Events {
        next, previous, playOrPause, stop, binder
    }

    public interface ServiceEvents {
        void onEvents(Events events, Object... args);
    }

    private static ServiceEvents serviceEvents;
    private static MediaPlayerService bindService;
    private static boolean isBindService = false;
    private static Context context;

    // 绑定服务 必须先调用 registerReceiver
    public static void bindService(ServiceEvents serviceEvents) {
        MediaPlayerService.serviceEvents = serviceEvents;

        if (!MediaPlayerService.isBindService) {
            Intent intent = new Intent(context, MediaPlayerService.class);
            /*
             * Service:Service的桥梁
             * ServiceConnection:处理链接状态
             * flags:BIND_AUTO_CREATE, BIND_DEBUG_UNBIND, BIND_NOT_FOREGROUND, BIND_ABOVE_CLIENT, BIND_ALLOW_OOM_MANAGEMENT, or BIND_WAIVE_PRIORITY.
             */
            context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
        } else {
            if (serviceEvents != null) serviceEvents.onEvents(Events.binder, bindService);
        }

    }

    /// 通知事件处理,只能加载一次,否则会重复
    public static void registerReceiver(Context context) {
        MediaPlayerService.context = context;
        // 注册广播
        BroadcastReceiver playerReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.d("action", intent.getAction());
                switch (Objects.requireNonNull(intent.getAction())) {
                    case ACTION_NEXT:
                        serviceEvents.onEvents(Events.next);
                        break;
                    case ACTION_PREVIOUS:
                        serviceEvents.onEvents(Events.previous);
                        break;
                    case ACTION_PLAY_OR_PAUSE:// 暂停/播放
                        serviceEvents.onEvents(Events.playOrPause);
                        break;
                    case ACTION_STOP:
                        serviceEvents.onEvents(Events.stop);
                        break;
                }
            }
        };
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTION_NEXT);
        intentFilter.addAction(ACTION_PREVIOUS);
        intentFilter.addAction(ACTION_PLAY_OR_PAUSE);
        intentFilter.addAction(ACTION_STOP);
        context.registerReceiver(playerReceiver, intentFilter);
    }

    // 解除绑定
    public static void unBind(Context context) {
        if (isBindService) {
            bindService.onDestroy();
            context.unbindService(serviceConnection);
            isBindService = false;
        }
    }

    /**
     * serviceConnection是一个ServiceConnection类型的对象,它是一个接口,用于监听所绑定服务的状态
     */
    private static ServiceConnection serviceConnection = new ServiceConnection() {
        /**
         * 该方法用于处理与服务已连接时的情况。
         */
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            ServiceBinder binder = (ServiceBinder) service;
            bindService = (MediaPlayerService) binder.getService();
            isBindService = true;
            if (serviceEvents != null) serviceEvents.onEvents(Events.binder, bindService);
        }

        /**
         * 该方法用于处理与服务断开连接时的情况。
         */
        @Override
        public void onServiceDisconnected(ComponentName name) {
            bindService = null;
        }

    };

    //    private static final int DELETE_PENDING_REQUESTS = 1022;
    private static final int CONTENT_PENDING_REQUESTS = 1023;
    private static final int NEXT_PENDING_REQUESTS = 1024;
    private static final int PLAY_PENDING_REQUESTS = 1025;
    private static final int STOP_PENDING_REQUESTS = 1026;
    private static final int NOTIFICATION_PENDING_ID = 1;

    private NotificationManager notificationManager;
    private NotificationCompat.Builder builder;
    private RemoteViews views;

    private void setupNotification() {
        // 设置点击通知结果
//        Intent intent = new Intent("android.flutter.audio_manager.activity");
        PendingIntent cpi;
        Intent intent = new Intent(this, AudioManagerPlugin.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        PendingIntent contentPendingIntent = PendingIntent.getActivity(this, 1023, intent, PendingIntent.FLAG_UPDATE_CURRENT | 67108864);
        views = new RemoteViews(getPackageName(), R.layout.layout_mediaplayer);
        cpi=contentPendingIntent;
        }else{
        PendingIntent contentPendingIntent = PendingIntent.getActivity(this, 1023, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        views = new RemoteViews(getPackageName(), R.layout.layout_mediaplayer);
        cpi=contentPendingIntent;

        }
        // 自定义布局
        // 下一首
        Intent intentNext = new Intent(ACTION_NEXT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        PendingIntent nextPendingIntent = PendingIntent.getBroadcast(this, 1024, intentNext, PendingIntent.FLAG_CANCEL_CURRENT | 33554432);
        views.setOnClickPendingIntent(R.id.iv_next, nextPendingIntent);
        
        }else{
        PendingIntent nextPendingIntent = PendingIntent.getBroadcast(this, 1024, intentNext, PendingIntent.FLAG_CANCEL_CURRENT);
        views.setOnClickPendingIntent(R.id.iv_next, nextPendingIntent);

        }
        

        // 暂停/播放
        Intent intentPlay = new Intent(ACTION_PLAY_OR_PAUSE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        PendingIntent playPendingIntent = PendingIntent.getBroadcast(this, 1025, intentPlay, PendingIntent.FLAG_CANCEL_CURRENT | 33554432);
        views.setOnClickPendingIntent(R.id.iv_pause, playPendingIntent);

        }else{
        PendingIntent playPendingIntent = PendingIntent.getBroadcast(this, 1025, intentPlay, PendingIntent.FLAG_CANCEL_CURRENT);
        views.setOnClickPendingIntent(R.id.iv_pause, playPendingIntent);

        }
        

        // 停止
        Intent intentStop = new Intent(ACTION_STOP);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        PendingIntent stopPendingIntent = PendingIntent.getBroadcast(this, 1026, intentStop, PendingIntent.FLAG_CANCEL_CURRENT | 33554432);
        views.setOnClickPendingIntent(R.id.iv_cancel, stopPendingIntent);

            }else{
        PendingIntent stopPendingIntent = PendingIntent.getBroadcast(this, 1026, intentStop, PendingIntent.FLAG_CANCEL_CURRENT);
        views.setOnClickPendingIntent(R.id.iv_cancel, stopPendingIntent);

        }
        

        builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                // 设置状态栏小图标
                .setSmallIcon(R.drawable.ic_launcher)
                // 设置标题
                .setContentTitle("")
                // 设置内容
                .setContentText("")
                // 点击通知后自动清除
                .setAutoCancel(false)
                // 设置点击通知效果
                .setContentIntent(cpi)
                // 设置删除时候出发的动作
//                .setDeleteIntent(delPendingIntent)
                // 自定义视图
                .setContent(views);

        // 获取NotificationManager实例
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel;
            notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
                    "Notification display", NotificationManager.IMPORTANCE_LOW);
            notificationManager.createNotificationChannel(notificationChannel);
        }

        // 前台服务
        startForeground(NOTIFICATION_PENDING_ID, builder.build());
    }

    void updateCover(Bitmap bitmap) {
        views.setImageViewBitmap(R.id.image, bitmap);
        notificationManager.notify(NOTIFICATION_PENDING_ID, builder.build());
    }

    void updateCover(int srcId) {
        views.setImageViewResource(R.id.image, srcId);
    }

    // 更新Notification
    void updateNotification(boolean isPlaying, String title, String desc) {
        if (views != null) {
            views.setTextViewText(R.id.tv_name, title);
            if (desc != null) views.setTextViewText(R.id.tv_author, desc);
            if (isPlaying) {
                views.setImageViewResource(R.id.iv_pause, android.R.drawable.ic_media_pause);
            } else {
                views.setImageViewResource(R.id.iv_pause, android.R.drawable.ic_media_play);
            }
        }

        // 刷新notification
        notificationManager.notify(NOTIFICATION_PENDING_ID, builder.build());
    }
}

I don't suggest you guys do the same but my primary device runs on Android 12 so maybe it's a temporary solution until they resolve the issue.

from audio_manager.

nicks258 avatar nicks258 commented on July 17, 2024

Any updates?

from audio_manager.

nicks258 avatar nicks258 commented on July 17, 2024

@jeromexiong

from audio_manager.

MrVipinVijayan avatar MrVipinVijayan commented on July 17, 2024

Hi, Could you please fix this.
Thanks.

from audio_manager.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.