android音乐播放器开发

如何快速开发一款音乐播放器?

文末提供体验demo,实现了播放控制,进度更新,全网音乐免费下载,欢迎技术交流!

音乐播放器无非就是对mediaplayer对象的操作了,当然需要开启服务,而服务为了保活,只能使用前台服务。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
   
            startForegroundService(new Intent(mContext, MusicService.class));
   } else {
   	
            startService(new Intent(mContext, MusicService.class));
   }

启动前台服务后,你有5秒的时间发送服务通知,否则服务会被杀死并抛出异常

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

        // 服务通知
        mNotification = createForegroundNotification();
        //将服务置于启动状态 ,NOTIFICATION_ID指的是创建的通知的ID
        startForeground(mNotificationId, mNotification);

创建服务通知

/**
     * 创建服务通知
     */
    private Notification createForegroundNotification() {
   

        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // 唯一的通知通道的id.
        String notificationChannelId = "notification_channel_music";

			//  Android8.0以上的系统,新建消息通道
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
   
            //用户可见的通道名称
            String channelName = "音乐播放";
            //通道的重要程度
            int importance = NotificationManager.IMPORTANCE_NONE;
            NotificationChannel notificationChannel = new NotificationChannel(notificationChannelId, channelName, importance);
            notificationChannel.setDescription("音乐播放控制器");
            //LED灯
            notificationChannel.enableLights(false);
					// notificationChannel.setLightColor(Color.RED);
            //震动
            notificationChannel.setVibrationPattern(new long[]{
   0});
            notificationChannel.enableVibration(false);
            //声音
            notificationChannel.setSound(null,null);
            mNotificationManager.createNotificationChannel(notificationChannel);
        }

        mRemoteView = new RemoteViews(getPackageName(), R.layout.notification);

        initRemoteView(mRemoteView);


        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, notificationChannelId)
                .
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一鱼浅游

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值