【Android】音乐播放器边播边缓存(二)AndroidVideoCache的后台播放

上一篇:Android音乐播放器边播边缓存(一)AndroidVideoCache的使用方法

第二篇记录一下,后台播放音乐的方式,没看过上一篇的朋友圈可以先移步 ↑ 链接。

下一篇:【Android】音乐播放器边播边缓存(三)AndroidVideoCache的先下载再播放


【Service】

后台播放,那我们很容易的想到就是在Service里面进行。主要介绍一下实现思路。


1.Activity与Service间的通信

启动服务有两种方法,startService和bindService

使用startService()方法启用服务,调用者与服务之间没有关连,即使调用者退出了,服务仍然运行;

使用bindService()方法启用服务,调用者与服务绑定在了一起,调用者一旦退出,服务也就终止。

更具体的区别,大家可以自行搜索,这里不再赘述。


在我们这里面的需求,需要保证用户退出这个界面(activity),service不能停,因此我们需要使用startService方法

此外,我们还需要获得从Service到Activity的通信方式(如再次进入activity时,要从service获取当前正在播放的歌曲名字等)

因此我们还需要bindService方法获取service中的参数等。


2.bindService的使用

我们通过重写Service中的onBind方法 及自定义IBinder

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }
public class MusicPlayBinder extends Binder {
        public ExoPlayer getPlayer() {
            return mPlayer;
        }

        public int getMusicType() {
            return musicType;
        }

        public int getMusicPlayingType() {
            return isPlayingMusicType;
        }

        public String getMusicName() {
            return musicName;
        }

        public String getMusicImageUrl() {
            return imageUrl;
        }

        //... ... 其他方法

    }

在Activity中,获取通信

bindService(intent, connection, BIND_AUTO_CREATE);
private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            musicPlayBinder = (MusicPlayService.MusicPlayBinder) iBinder;//获取到了service中的IBinder
            if (musicPlayBinder.getPlayer() == null || targetUrl == null) {
                Log.d(TAG, "player暂未初始化" + targetUrl);
            } else {
                Log.d(TAG, "player已经初始化" + targetUrl);
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

3.开启前台服务

            Notification.Builder builder = new Notification.Builder(context);
            Intent targetIntent = new Intent(context, MusicPlayActivity.class);

            Bundle targetBundle = new Bundle();
            targetBundle.putInt("musicType", isPlayingMusicType);
            targetIntent.putExtra("bundle", targetBundle);
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0,//点击前台通知时的intent
                    targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            builder.setContentIntent(contentIntent);
            builder.setSmallIcon(R.mipmap.logo_launcher);
            Notification notification = builder.build();
            RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_music_fore);//自定义前台通知的样式
            remoteViews.setTextViewText(R.id.notify_name, musicName);
            remoteViews.setTextViewText(R.id.notify_desc, musicDesc);
            if (isPlayingMusicType == Constant.MUSIC_TYPE_RELAX) {
                remoteViews.setTextViewText(R.id.notify_type, getResources().getString(R.string.home_relax));
            } else if (isPlayingMusicType == Constant.MUSIC_TYPE_SLEEP) {
                remoteViews.setTextViewText(R.id.notify_type, getResources().getString(R.string.home_sleep));
            }
            notification.contentView = remoteViews;
            startForeground(1, notification);//启动前台通知

样式如下



点击这个通知就可以再次进入Activity了,然后通过connection获取Service中正在播放的音乐信息,渲染到Activity上。



猜你喜欢

转载自blog.csdn.net/crab0314/article/details/80137742