ソースを参照

完善音频维护

yangchong 3 年 前
コミット
a1401206ca

+ 4 - 4
Demo/src/main/java/com/yc/ycvideoplayer/MainActivity.java

@@ -23,7 +23,7 @@ import androidx.appcompat.app.AppCompatActivity;
 import org.yc.ycvideoplayer.R;
 
 import com.yc.music.model.AudioBean;
-import com.yc.music.service.PlayService;
+import com.yc.music.service.PlayAudioService;
 import com.yc.music.tool.BaseAppHelper;
 import com.yc.videotool.VideoLogUtils;
 import com.yc.videoview.FloatWindow;
@@ -211,7 +211,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
      * 开启服务
      */
     private void startService() {
-        Intent intent = new Intent(this, PlayService.class);
+        Intent intent = new Intent(this, PlayAudioService.class);
         startService(intent);
     }
 
@@ -222,7 +222,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
      */
     private void bindService() {
         Intent intent = new Intent();
-        intent.setClass(this, PlayService.class);
+        intent.setClass(this, PlayAudioService.class);
         mPlayServiceConnection = new PlayServiceConnection();
         bindService(intent, mPlayServiceConnection, Context.BIND_AUTO_CREATE);
     }
@@ -232,7 +232,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
         @Override
         public void onServiceConnected(ComponentName name, IBinder service) {
             VideoLogUtils.e("onServiceConnected"+name);
-            final PlayService playService = ((PlayService.PlayBinder) service).getService();
+            final PlayAudioService playService = ((PlayAudioService.PlayBinder) service).getService();
             BaseAppHelper.get().setPlayService(playService);
             List<AudioBean> musicList = BaseAppHelper.get().getMusicList();
             AudioBean audioBean1 = new AudioBean();

+ 5 - 5
Demo/src/main/java/com/yc/ycvideoplayer/music/PlayMusicFragment.java

@@ -26,9 +26,9 @@ import com.yc.music.config.PlayModeEnum;
 import com.yc.music.inter.OnPlayerEventListener;
 import com.yc.music.model.AudioBean;
 import com.yc.music.tool.BaseAppHelper;
-import com.yc.music.utils.MusicLogUtils;
 import com.yc.music.utils.MusicSpUtils;
 import com.yc.video.tool.PlayerUtils;
+import com.yc.videotool.VideoLogUtils;
 
 import org.yc.ycvideoplayer.R;
 
@@ -81,7 +81,7 @@ public class PlayMusicFragment extends Fragment implements View.OnClickListener,
     @Override
     public void setUserVisibleHint(boolean isVisibleToUser) {
         super.setUserVisibleHint(isVisibleToUser);
-        MusicLogUtils.e(TAG+"setUserVisibleHint"+isVisibleToUser);
+        VideoLogUtils.e(TAG+"setUserVisibleHint"+isVisibleToUser);
     }
 
     /**
@@ -93,7 +93,7 @@ public class PlayMusicFragment extends Fragment implements View.OnClickListener,
     @Override
     public void onHiddenChanged(boolean hidden) {
         super.onHiddenChanged(hidden);
-        MusicLogUtils.e(TAG+"onHiddenChanged"+hidden);
+        VideoLogUtils.e(TAG+"onHiddenChanged"+hidden);
         if (!hidden){
             initData();
         }
@@ -393,7 +393,7 @@ public class PlayMusicFragment extends Fragment implements View.OnClickListener,
         sbProgress.setProgress((int) BaseAppHelper.get().getMusicService().getCurrentPosition());
         sbProgress.setSecondaryProgress(0);
         sbProgress.setMax((int) playingMusic.getDuration());
-        MusicLogUtils.e("-----------------------"+(int) playingMusic.getDuration());
+        VideoLogUtils.e("-----------------------"+(int) playingMusic.getDuration());
         mLastProgress = 0;
         tvCurrentTime.setText("00:00");
         tvTotalTime.setText(PlayerUtils.formatTime(playingMusic.getDuration()));
@@ -445,7 +445,7 @@ public class PlayMusicFragment extends Fragment implements View.OnClickListener,
     @Override
     public void onBufferingUpdate(int percent) {
         if(sbProgress.getMax()>0 && percent>0){
-            MusicLogUtils.e("setOnPlayEventListener---percent---"+ sbProgress.getMax() + "-----" +percent);
+            VideoLogUtils.e("setOnPlayEventListener---percent---"+ sbProgress.getMax() + "-----" +percent);
             sbProgress.setSecondaryProgress(sbProgress.getMax() * 100 / percent);
         }
     }

+ 1 - 0
MusicPlayer/build.gradle

@@ -25,6 +25,7 @@ dependencies {
     implementation fileTree(dir: "libs", include: ["*.jar"])
     implementation project.ext.AppDependencies['appcompat']
     implementation project.ext.AppDependencies['media']
+    implementation project(path: ':VideoTool')
 }
 
 /** 以下开始是将Android Library上传到jcenter的相关配置**/

+ 1 - 1
MusicPlayer/src/main/AndroidManifest.xml

@@ -4,7 +4,7 @@
     <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
 
     <application>
-        <service android:name=".service.PlayService" />
+        <service android:name=".service.PlayAudioService" />
         <receiver android:name=".receiver.NotificationStatusBarReceiver">
             <intent-filter>
                 <action android:name="YC_ACTION_STATUS_BAR" />

+ 3 - 3
MusicPlayer/src/main/java/com/yc/music/manager/AudioFocusManager.java

@@ -4,7 +4,7 @@ import android.media.AudioManager;
 
 import androidx.annotation.NonNull;
 
-import com.yc.music.service.PlayService;
+import com.yc.music.service.PlayAudioService;
 
 import static android.content.Context.AUDIO_SERVICE;
 
@@ -12,7 +12,7 @@ import static android.content.Context.AUDIO_SERVICE;
 public class AudioFocusManager implements AudioManager.OnAudioFocusChangeListener {
 
 
-    private PlayService mPlayService;
+    private PlayAudioService mPlayService;
     private AudioManager mAudioManager;
     /**
      * 是否因聚焦丢失瞬变而暂停
@@ -25,7 +25,7 @@ public class AudioFocusManager implements AudioManager.OnAudioFocusChangeListene
      * 初始化操作
      * @param content           playService对象
      */
-    public AudioFocusManager(@NonNull PlayService content) {
+    public AudioFocusManager(@NonNull PlayAudioService content) {
         mPlayService = content;
         mAudioManager = (AudioManager) content.getSystemService(AUDIO_SERVICE);
     }

+ 2 - 2
MusicPlayer/src/main/java/com/yc/music/manager/AudioSoundManager.java

@@ -6,7 +6,7 @@ import android.os.Build;
 
 import androidx.annotation.NonNull;
 
-import com.yc.music.service.PlayService;
+import com.yc.music.service.PlayAudioService;
 
 import static android.content.Context.AUDIO_SERVICE;
 
@@ -20,7 +20,7 @@ public class AudioSoundManager  {
      * 初始化操作
      * @param content           playService对象
      */
-    public AudioSoundManager(@NonNull PlayService content) {
+    public AudioSoundManager(@NonNull PlayAudioService content) {
         mAudioManager = (AudioManager) content.getSystemService(AUDIO_SERVICE);
     }
 

+ 3 - 3
MusicPlayer/src/main/java/com/yc/music/manager/MediaSessionManager.java

@@ -6,7 +6,7 @@ import android.support.v4.media.session.MediaSessionCompat;
 import android.support.v4.media.session.PlaybackStateCompat;
 
 import com.yc.music.model.AudioBean;
-import com.yc.music.service.PlayService;
+import com.yc.music.service.PlayAudioService;
 import com.yc.music.tool.BaseAppHelper;
 
 
@@ -21,10 +21,10 @@ public class MediaSessionManager {
             | PlaybackStateCompat.ACTION_STOP
             | PlaybackStateCompat.ACTION_SEEK_TO;
 
-    private PlayService mPlayService;
+    private PlayAudioService mPlayService;
     private MediaSessionCompat mMediaSession;
 
-    public MediaSessionManager(PlayService playService) {
+    public MediaSessionManager(PlayAudioService playService) {
         mPlayService = playService;
         setupMediaSession();
     }

+ 8 - 8
MusicPlayer/src/main/java/com/yc/music/receiver/AudioBroadcastReceiver.java

@@ -5,8 +5,8 @@ import android.content.Context;
 import android.content.Intent;
 
 import com.yc.music.config.MusicConstant;
-import com.yc.music.service.PlayService;
-import com.yc.music.utils.MusicLogUtils;
+import com.yc.music.service.PlayAudioService;
+import com.yc.videotool.VideoLogUtils;
 
 
 /**
@@ -22,18 +22,18 @@ public class AudioBroadcastReceiver extends BroadcastReceiver {
             switch (action){
                 //锁屏时处理的逻辑
                 case MusicConstant.LOCK_SCREEN_ACTION:
-                    PlayService.startCommand(context, MusicConstant.LOCK_SCREEN_ACTION);
-                    MusicLogUtils.e("AudioBroadcastReceiver"+"---LOCK_SCREEN");
+                    PlayAudioService.startCommand(context, MusicConstant.LOCK_SCREEN_ACTION);
+                    VideoLogUtils.e("AudioBroadcastReceiver"+"---LOCK_SCREEN");
                     break;
                 //当屏幕灭了
                 case Intent.ACTION_SCREEN_OFF:
-                    PlayService.startCommand(context,Intent.ACTION_SCREEN_OFF);
-                    MusicLogUtils.e("AudioBroadcastReceiver"+"---当屏幕灭了");
+                    PlayAudioService.startCommand(context,Intent.ACTION_SCREEN_OFF);
+                    VideoLogUtils.e("AudioBroadcastReceiver"+"---当屏幕灭了");
                     break;
                 //当屏幕亮了
                 case Intent.ACTION_SCREEN_ON:
-                    PlayService.startCommand(context,Intent.ACTION_SCREEN_ON);
-                    MusicLogUtils.e("AudioBroadcastReceiver"+"---当屏幕亮了");
+                    PlayAudioService.startCommand(context,Intent.ACTION_SCREEN_ON);
+                    VideoLogUtils.e("AudioBroadcastReceiver"+"---当屏幕亮了");
                     break;
                 default:
                     break;

+ 2 - 2
MusicPlayer/src/main/java/com/yc/music/receiver/AudioEarPhoneReceiver.java

@@ -6,7 +6,7 @@ import android.content.Intent;
 import android.media.AudioManager;
 
 import com.yc.music.config.MusicPlayAction;
-import com.yc.music.service.PlayService;
+import com.yc.music.service.PlayAudioService;
 
 
 /**
@@ -23,7 +23,7 @@ public class AudioEarPhoneReceiver extends BroadcastReceiver {
             switch (action){
                 //来电/耳机拔出时暂停播放
                 case AudioManager.ACTION_AUDIO_BECOMING_NOISY:
-                    PlayService.startCommand(context, MusicPlayAction.TYPE_START_PAUSE);
+                    PlayAudioService.startCommand(context, MusicPlayAction.TYPE_START_PAUSE);
                     break;
                 default:
                     break;

+ 4 - 4
MusicPlayer/src/main/java/com/yc/music/receiver/EarphoneControlReceiver.java

@@ -7,7 +7,7 @@ import android.support.v4.media.session.MediaSessionCompat;
 import android.view.KeyEvent;
 
 import com.yc.music.config.MusicPlayAction;
-import com.yc.music.service.PlayService;
+import com.yc.music.service.PlayAudioService;
 
 
 /**
@@ -26,13 +26,13 @@ public class EarphoneControlReceiver extends BroadcastReceiver {
             case KeyEvent.KEYCODE_MEDIA_PAUSE:
             case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
             case KeyEvent.KEYCODE_HEADSETHOOK:
-                PlayService.startCommand(context, MusicPlayAction.TYPE_START_PAUSE);
+                PlayAudioService.startCommand(context, MusicPlayAction.TYPE_START_PAUSE);
                 break;
             case KeyEvent.KEYCODE_MEDIA_NEXT:
-                PlayService.startCommand(context, MusicPlayAction.TYPE_NEXT);
+                PlayAudioService.startCommand(context, MusicPlayAction.TYPE_NEXT);
                 break;
             case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
-                PlayService.startCommand(context, MusicPlayAction.TYPE_PRE);
+                PlayAudioService.startCommand(context, MusicPlayAction.TYPE_PRE);
                 break;
             default:
                 break;

+ 9 - 9
MusicPlayer/src/main/java/com/yc/music/receiver/NotificationStatusBarReceiver.java

@@ -6,9 +6,9 @@ import android.content.Intent;
 import android.text.TextUtils;
 
 import com.yc.music.config.MusicPlayAction;
-import com.yc.music.service.PlayService;
+import com.yc.music.service.PlayAudioService;
 import com.yc.music.tool.BaseAppHelper;
-import com.yc.music.utils.MusicLogUtils;
+import com.yc.videotool.VideoLogUtils;
 
 
 public class NotificationStatusBarReceiver extends BroadcastReceiver {
@@ -24,22 +24,22 @@ public class NotificationStatusBarReceiver extends BroadcastReceiver {
         }
         String extra = intent.getStringExtra(EXTRA);
         if (TextUtils.equals(extra, MusicPlayAction.TYPE_NEXT)) {
-            PlayService.startCommand(context, MusicPlayAction.TYPE_NEXT);
-            MusicLogUtils.e("NotifiyStatusBarReceiver"+"下一首");
+            PlayAudioService.startCommand(context, MusicPlayAction.TYPE_NEXT);
+            VideoLogUtils.e("NotifiyStatusBarReceiver"+"下一首");
         } else if (TextUtils.equals(extra, MusicPlayAction.TYPE_START_PAUSE)) {
             if(BaseAppHelper.get().getPlayService()!=null){
                 boolean playing = BaseAppHelper.get().getPlayService().isPlaying();
                 if(playing){
-                    MusicLogUtils.e("NotifiyStatusBarReceiver"+"暂停");
+                    VideoLogUtils.e("NotifiyStatusBarReceiver"+"暂停");
                 }else {
-                    MusicLogUtils.e("NotifiyStatusBarReceiver"+"播放");
+                    VideoLogUtils.e("NotifiyStatusBarReceiver"+"播放");
                 }
-                PlayService.startCommand(context, MusicPlayAction.TYPE_START_PAUSE);
+                PlayAudioService.startCommand(context, MusicPlayAction.TYPE_START_PAUSE);
             }
 
         }else if(TextUtils.equals(extra, MusicPlayAction.TYPE_PRE)){
-            PlayService.startCommand(context, MusicPlayAction.TYPE_PRE);
-            MusicLogUtils.e("NotifiyStatusBarReceiver"+"上一首");
+            PlayAudioService.startCommand(context, MusicPlayAction.TYPE_PRE);
+            VideoLogUtils.e("NotifiyStatusBarReceiver"+"上一首");
         }
     }
 }

+ 21 - 14
MusicPlayer/src/main/java/com/yc/music/service/PlayService.java → MusicPlayer/src/main/java/com/yc/music/service/PlayAudioService.java

@@ -25,10 +25,10 @@ import com.yc.music.model.AudioBean;
 import com.yc.music.receiver.AudioBroadcastReceiver;
 import com.yc.music.receiver.AudioEarPhoneReceiver;
 import com.yc.music.tool.BaseAppHelper;
-import com.yc.music.utils.MusicLogUtils;
 import com.yc.music.utils.NotificationHelper;
 import com.yc.music.tool.QuitTimerHelper;
 import com.yc.music.utils.MusicSpUtils;
+import com.yc.videotool.VideoLogUtils;
 
 import java.io.IOException;
 import java.util.List;
@@ -36,9 +36,15 @@ import java.util.Random;
 
 
 /**
- * Service就是用来在后台完成一些不需要和用户交互的动作
+ * <pre>
+ *     @author yangchong
+ *     blog  : https://www.jianshu.com/p/514eb6193a06
+ *     time  : 2016/2/10
+ *     desc  : Service就是用来在后台完成一些不需要和用户交互的动作
+ *     revise: 在清科公司写的音频播放器,后期暂停维护
+ * </pre>
  */
-public class PlayService extends Service {
+public class PlayAudioService extends Service {
 
     /**
      * 正在播放的歌曲的序号
@@ -133,15 +139,15 @@ public class PlayService extends Service {
      * @param type          类型
      */
     public static void startCommand(Context context, String type) {
-        Intent intent = new Intent(context, PlayService.class);
+        Intent intent = new Intent(context, PlayAudioService.class);
         intent.setAction(type);
         context.startService(intent);
     }
 
 
     public class PlayBinder extends Binder {
-        public PlayService getService() {
-            return PlayService.this;
+        public PlayAudioService getService() {
+            return PlayAudioService.this;
         }
     }
 
@@ -183,7 +189,7 @@ public class PlayService extends Service {
         //注销广播接收者
         unregisterReceiver(mAudioReceiver);
         //结束notification通知
-        NotificationHelper.get().cancelAll();
+        NotificationHelper.get().onDestroy(true);
         //设置service为null
         BaseAppHelper.get().setPlayService(null);
     }
@@ -219,15 +225,16 @@ public class PlayService extends Service {
                     break;
                 //添加锁屏界面
                 case MusicConstant.LOCK_SCREEN_ACTION:
-                    MusicLogUtils.e("PlayService"+"---LOCK_SCREEN"+mIsLocked);
+                    VideoLogUtils.e("PlayService"+"---LOCK_SCREEN"+mIsLocked);
                     break;
                 //当屏幕灭了,添加锁屏页面
                 case Intent.ACTION_SCREEN_OFF:
                     startLockAudioActivity();
-                    MusicLogUtils.e("PlayService"+"---当屏幕灭了");
+                    VideoLogUtils.e("PlayService"+"---当屏幕灭了");
                     break;
+                //当屏幕亮了
                 case Intent.ACTION_SCREEN_ON:
-                    MusicLogUtils.e("PlayService"+"---当屏幕亮了");
+                    VideoLogUtils.e("PlayService"+"---当屏幕亮了");
                     break;
                 default:
                     break;
@@ -402,7 +409,7 @@ public class PlayService extends Service {
                     // 如果是最后一首,则切换回第一首
                     mPlayingPosition = 0;
                 }
-                MusicLogUtils.e("PlayService"+"----mPlayingPosition----"+ mPlayingPosition);
+                VideoLogUtils.e("PlayService"+"----mPlayingPosition----"+ mPlayingPosition);
                 play(mPlayingPosition);
                 break;
         }
@@ -508,7 +515,7 @@ public class PlayService extends Service {
         mPlayingPosition = position;
         AudioBean music = audioMusics.get(mPlayingPosition);
         String id = music.getId();
-        MusicLogUtils.e("PlayService"+"----id----"+ id);
+        VideoLogUtils.e("PlayService"+"----id----"+ id);
         //保存当前播放的musicId,下次进来可以记录状态
         long musicId = Long.parseLong(id);
         MusicSpUtils.getInstance(MusicConstant.SP_NAME).put(MusicConstant.MUSIC_ID,musicId);
@@ -577,7 +584,7 @@ public class PlayService extends Service {
             int currentPosition =  mPlayer.getCurrentPosition();
             mListener.onUpdateProgress(currentPosition);
         }
-        MusicLogUtils.e("updatePlayProgressShow");
+        VideoLogUtils.e("updatePlayProgressShow");
         // 每30毫秒更新一下显示的内容,注意这里时间不要太短,因为这个是一个循环
         // 经过测试,60毫秒更新一次有点卡,30毫秒最为顺畅
         handler.sendEmptyMessageDelayed(UPDATE_PLAY_PROGRESS_SHOW, 300);
@@ -810,7 +817,7 @@ public class PlayService extends Service {
         }
         for (int i = 0; i < audioMusics.size(); i++) {
             String musicId = audioMusics.get(i).getId();
-            MusicLogUtils.e("PlayService"+"----musicId----"+ musicId);
+            VideoLogUtils.e("PlayService"+"----musicId----"+ musicId);
             if (Long.parseLong(musicId) == id) {
                 position = i;
                 break;

+ 6 - 6
MusicPlayer/src/main/java/com/yc/music/tool/BaseAppHelper.java

@@ -3,7 +3,7 @@ package com.yc.music.tool;
 import android.annotation.SuppressLint;
 
 import com.yc.music.model.AudioBean;
-import com.yc.music.service.PlayService;
+import com.yc.music.service.PlayAudioService;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -22,7 +22,7 @@ public class BaseAppHelper {
     /**
      * 播放音乐service
      */
-    private PlayService mPlayService;
+    private PlayAudioService mPlayService;
     /**
      * 本地歌曲列表
      */
@@ -45,14 +45,14 @@ public class BaseAppHelper {
      * 获取PlayService对象
      * @return              返回PlayService对象
      */
-    public PlayService getPlayService() {
+    public PlayAudioService getPlayService() {
         return mPlayService;
     }
 
     /**
      * 设置PlayService服务
      */
-    public void setPlayService(PlayService service) {
+    public void setPlayService(PlayAudioService service) {
         mPlayService = service;
     }
 
@@ -78,8 +78,8 @@ public class BaseAppHelper {
      * 获取到播放音乐的服务
      * @return              PlayService对象
      */
-    public PlayService getMusicService () {
-        PlayService playService = BaseAppHelper.get().getPlayService();
+    public PlayAudioService getMusicService () {
+        PlayAudioService playService = BaseAppHelper.get().getPlayService();
         if (playService == null) {
             //待解决:当长期处于后台,如何保活?避免service被杀死……
             throw new NullPointerException("play service is null");

+ 3 - 3
MusicPlayer/src/main/java/com/yc/music/tool/QuitTimerHelper.java

@@ -6,7 +6,7 @@ import android.text.format.DateUtils;
 import androidx.annotation.NonNull;
 
 import com.yc.music.inter.EventCallback;
-import com.yc.music.service.PlayService;
+import com.yc.music.service.PlayAudioService;
 
 
 /**
@@ -14,7 +14,7 @@ import com.yc.music.service.PlayService;
  */
 public class QuitTimerHelper {
 
-    private PlayService mPlayService;
+    private PlayAudioService mPlayService;
     private EventCallback<Long> mTimerCallback;
     private Handler mHandler;
     private long mTimerRemain;
@@ -35,7 +35,7 @@ public class QuitTimerHelper {
      * @param handler                   handler
      * @param timerCallback             timerCallback
      */
-    public void init(@NonNull PlayService playService, @NonNull Handler handler,
+    public void init(@NonNull PlayAudioService playService, @NonNull Handler handler,
                      @NonNull EventCallback<Long> timerCallback) {
         mPlayService = playService;
         mHandler = handler;

+ 0 - 71
MusicPlayer/src/main/java/com/yc/music/utils/MusicLogUtils.java

@@ -1,71 +0,0 @@
-/*
-Copyright 2017 yangchong211(github.com/yangchong211)
-
-Licensed under the Apache License, Version 2.0 (the "License");
-you may not use this file except in compliance with the License.
-You may obtain a copy of the License at
-
-http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
-*/
-package com.yc.music.utils;
-
-import android.util.Log;
-
-/**
- * <pre>
- *     @author yangchong
- *     blog  : https://github.com/yangchong211
- *     time  : 2017/10/21
- *     desc  : log工具
- *     revise:
- * </pre>
- */
-public final class MusicLogUtils {
-
-    private static final String TAG = "MusicPlayer";
-    private static boolean isLog = false;
-
-    /**
-     * 设置是否开启日志
-     * @param isLog                 是否开启日志
-     */
-    public static void setIsLog(boolean isLog) {
-        MusicLogUtils.isLog = isLog;
-    }
-
-    public static boolean isIsLog() {
-        return isLog;
-    }
-
-    public static void d(String message) {
-        if(isLog){
-            Log.d(TAG, message);
-        }
-    }
-
-    public static void i(String message) {
-        if(isLog){
-            Log.i(TAG, message);
-        }
-
-    }
-
-    public static void e(String msg) {
-        if (isLog) {
-            Log.e(TAG, msg);
-        }
-    }
-
-    public static void e(String message, Throwable throwable) {
-        if(isLog){
-            Log.e(TAG, message, throwable);
-        }
-    }
-
-}

+ 24 - 8
MusicPlayer/src/main/java/com/yc/music/utils/NotificationHelper.java

@@ -11,13 +11,13 @@ import android.widget.RemoteViews;
 import com.yc.music.R;
 import com.yc.music.model.AudioBean;
 import com.yc.music.receiver.NotificationStatusBarReceiver;
-import com.yc.music.service.PlayService;
+import com.yc.music.service.PlayAudioService;
 
 
 public class NotificationHelper {
 
 
-    private PlayService playService;
+    private PlayAudioService playService;
     private NotificationManager notificationManager;
     private static final int NOTIFICATION_ID = 0x111;
 
@@ -26,7 +26,7 @@ public class NotificationHelper {
     }
 
     private static class SingletonHolder {
-        private static NotificationHelper instance = new NotificationHelper();
+        private static final NotificationHelper instance = new NotificationHelper();
     }
 
     private NotificationHelper() {
@@ -37,9 +37,10 @@ public class NotificationHelper {
      * 1.创建一个NotificationManager的引用
      * @param playService           PlayService对象
      */
-    public void init(PlayService playService) {
+    public void init(PlayAudioService playService) {
         this.playService = playService;
-        notificationManager = (NotificationManager) playService.getSystemService(Context.NOTIFICATION_SERVICE);
+        notificationManager = (NotificationManager)
+                playService.getSystemService(Context.NOTIFICATION_SERVICE);
     }
 
     /**
@@ -50,7 +51,8 @@ public class NotificationHelper {
         if (music == null) {
             return;
         }
-        playService.startForeground(NOTIFICATION_ID, buildNotification(playService, music, true));
+        playService.startForeground(NOTIFICATION_ID,
+                buildNotification(playService, music, true));
         //这个方法是启动Notification到前台
     }
 
@@ -65,15 +67,29 @@ public class NotificationHelper {
             return;
         }
         playService.stopForeground(false);
-        notificationManager.notify(NOTIFICATION_ID, buildNotification(playService, music, false));
+        notificationManager.notify(NOTIFICATION_ID,
+                buildNotification(playService, music, false));
     }
 
+    /**
+     * 销毁的时候调用
+     * @param isRemoveNotification                  是否移除通知栏
+     */
+    public void onDestroy(boolean isRemoveNotification){
+        //如果设置成ture,则会移除通知栏
+        playService.stopForeground(isRemoveNotification);
+        cancelAll();
+
+    }
 
     /**
      * 结束所有的
      */
     public void cancelAll() {
-        notificationManager.cancelAll();
+        if (notificationManager!=null){
+            notificationManager.cancelAll();
+            notificationManager = null;
+        }
     }
 
     private Notification buildNotification(Context context, AudioBean music, boolean isPlaying) {

+ 0 - 11
MusicPlayer/src/main/java/com/yc/music/utils/NotificationUtils.java

@@ -53,17 +53,6 @@ public class NotificationUtils extends ContextWrapper {
         //范围是从 NotificationManager.IMPORTANCE_NONE(0) ~ NotificationManager.IMPORTANCE_HIGH(4)
         NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME,
                 NotificationManager.IMPORTANCE_DEFAULT);
-        channel.canBypassDnd();//是否绕过请勿打扰模式
-        channel.enableLights(true);//闪光灯
-        channel.setLockscreenVisibility(VISIBILITY_SECRET);//锁屏显示通知
-        channel.setLightColor(Color.RED);//闪关灯的灯光颜色
-        channel.canShowBadge();//桌面launcher的消息角标
-        channel.enableVibration(isVibration);//是否允许震动
-        channel.getAudioAttributes();//获取系统通知响铃声音的配置
-        channel.getGroup();//获取通知取到组
-        channel.setBypassDnd(true);//设置可绕过 请勿打扰模式
-        channel.setVibrationPattern(new long[]{100, 100, 200});//设置震动模式
-        channel.shouldShowLights();//是否会有灯光
         getManager().createNotificationChannel(channel);
     }
 

+ 416 - 0
VideoTool/src/main/java/com/yc/videotool/MusicSpUtils.java

@@ -0,0 +1,416 @@
+package com.yc.videotool;
+
+import android.annotation.SuppressLint;
+import android.app.Application;
+import android.content.Context;
+import android.content.SharedPreferences;
+
+import androidx.annotation.NonNull;
+import androidx.collection.SimpleArrayMap;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+
+
+@SuppressLint("ApplySharedPref")
+public final class MusicSpUtils {
+
+    private static SimpleArrayMap<String, MusicSpUtils> SP_UTILS_MAP = new SimpleArrayMap<>();
+    private SharedPreferences sp;
+    private static Context context;
+
+    public static void init(Application application){
+        context = application;
+    }
+
+    /**
+     * 获取 SP 实例
+     *
+     * @return {@link MusicSpUtils}
+     */
+    public static MusicSpUtils getInstance() {
+        return getInstance("");
+    }
+
+    /**
+     * 获取 SP 实例
+     *
+     * @param spName sp 名
+     * @return {@link MusicSpUtils}
+     */
+    public static MusicSpUtils getInstance(String spName) {
+        if (isSpace(spName)) spName = "spUtils";
+        MusicSpUtils spUtils = SP_UTILS_MAP.get(spName);
+        if (spUtils == null) {
+            spUtils = new MusicSpUtils(spName);
+            SP_UTILS_MAP.put(spName, spUtils);
+        }
+        return spUtils;
+    }
+
+    private MusicSpUtils(final String spName) {
+        sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
+    }
+
+    /**
+     * SP 中写入 String
+     *
+     * @param key   键
+     * @param value 值
+     */
+    public void put(@NonNull final String key, @NonNull final String value) {
+        put(key, value, false);
+    }
+
+    /**
+     * SP 中写入 String
+     *
+     * @param key      键
+     * @param value    值
+     * @param isCommit {@code true}: {@link SharedPreferences.Editor#commit()}<br>
+     *                 {@code false}: {@link SharedPreferences.Editor#apply()}
+     */
+    public void put(@NonNull final String key,
+                    @NonNull final String value,
+                    final boolean isCommit) {
+        if (isCommit) {
+            sp.edit().putString(key, value).commit();
+        } else {
+            sp.edit().putString(key, value).apply();
+        }
+    }
+
+    /**
+     * SP 中读取 String
+     *
+     * @param key 键
+     * @return 存在返回对应值,不存在返回默认值{@code ""}
+     */
+    public String getString(@NonNull final String key) {
+        return getString(key, "");
+    }
+
+    /**
+     * SP 中读取 String
+     *
+     * @param key          键
+     * @param defaultValue 默认值
+     * @return 存在返回对应值,不存在返回默认值{@code defaultValue}
+     */
+    public String getString(@NonNull final String key, @NonNull final String defaultValue) {
+        return sp.getString(key, defaultValue);
+    }
+
+    /**
+     * SP 中写入 int
+     *
+     * @param key   键
+     * @param value 值
+     */
+    public void put(@NonNull final String key, final int value) {
+        put(key, value, false);
+    }
+
+    /**
+     * SP 中写入 int
+     *
+     * @param key      键
+     * @param value    值
+     * @param isCommit {@code true}: {@link SharedPreferences.Editor#commit()}<br>
+     *                 {@code false}: {@link SharedPreferences.Editor#apply()}
+     */
+    public void put(@NonNull final String key, final int value, final boolean isCommit) {
+        if (isCommit) {
+            sp.edit().putInt(key, value).commit();
+        } else {
+            sp.edit().putInt(key, value).apply();
+        }
+    }
+
+    /**
+     * SP 中读取 int
+     *
+     * @param key 键
+     * @return 存在返回对应值,不存在返回默认值-1
+     */
+    public int getInt(@NonNull final String key) {
+        return getInt(key, -1);
+    }
+
+    /**
+     * SP 中读取 int
+     *
+     * @param key          键
+     * @param defaultValue 默认值
+     * @return 存在返回对应值,不存在返回默认值{@code defaultValue}
+     */
+    public int getInt(@NonNull final String key, final int defaultValue) {
+        return sp.getInt(key, defaultValue);
+    }
+
+    /**
+     * SP 中写入 long
+     *
+     * @param key   键
+     * @param value 值
+     */
+    public void put(@NonNull final String key, final long value) {
+        put(key, value, false);
+    }
+
+    /**
+     * SP 中写入 long
+     *
+     * @param key      键
+     * @param value    值
+     * @param isCommit {@code true}: {@link SharedPreferences.Editor#commit()}<br>
+     *                 {@code false}: {@link SharedPreferences.Editor#apply()}
+     */
+    public void put(@NonNull final String key, final long value, final boolean isCommit) {
+        if (isCommit) {
+            sp.edit().putLong(key, value).commit();
+        } else {
+            sp.edit().putLong(key, value).apply();
+        }
+    }
+
+    /**
+     * SP 中读取 long
+     *
+     * @param key 键
+     * @return 存在返回对应值,不存在返回默认值-1
+     */
+    public long getLong(@NonNull final String key) {
+        return getLong(key, -1L);
+    }
+
+    /**
+     * SP 中读取 long
+     *
+     * @param key          键
+     * @param defaultValue 默认值
+     * @return 存在返回对应值,不存在返回默认值{@code defaultValue}
+     */
+    public long getLong(@NonNull final String key, final long defaultValue) {
+        return sp.getLong(key, defaultValue);
+    }
+
+    /**
+     * SP 中写入 float
+     *
+     * @param key   键
+     * @param value 值
+     */
+    public void put(@NonNull final String key, final float value) {
+        put(key, value, false);
+    }
+
+    /**
+     * SP 中写入 float
+     *
+     * @param key      键
+     * @param value    值
+     * @param isCommit {@code true}: {@link SharedPreferences.Editor#commit()}<br>
+     *                 {@code false}: {@link SharedPreferences.Editor#apply()}
+     */
+    public void put(@NonNull final String key, final float value, final boolean isCommit) {
+        if (isCommit) {
+            sp.edit().putFloat(key, value).commit();
+        } else {
+            sp.edit().putFloat(key, value).apply();
+        }
+    }
+
+    /**
+     * SP 中读取 float
+     *
+     * @param key 键
+     * @return 存在返回对应值,不存在返回默认值-1
+     */
+    public float getFloat(@NonNull final String key) {
+        return getFloat(key, -1f);
+    }
+
+    /**
+     * SP 中读取 float
+     *
+     * @param key          键
+     * @param defaultValue 默认值
+     * @return 存在返回对应值,不存在返回默认值{@code defaultValue}
+     */
+    public float getFloat(@NonNull final String key, final float defaultValue) {
+        return sp.getFloat(key, defaultValue);
+    }
+
+    /**
+     * SP 中写入 boolean
+     *
+     * @param key   键
+     * @param value 值
+     */
+    public void put(@NonNull final String key, final boolean value) {
+        put(key, value, false);
+    }
+
+    /**
+     * SP 中写入 boolean
+     *
+     * @param key      键
+     * @param value    值
+     * @param isCommit {@code true}: {@link SharedPreferences.Editor#commit()}<br>
+     *                 {@code false}: {@link SharedPreferences.Editor#apply()}
+     */
+    public void put(@NonNull final String key, final boolean value, final boolean isCommit) {
+        if (isCommit) {
+            sp.edit().putBoolean(key, value).commit();
+        } else {
+            sp.edit().putBoolean(key, value).apply();
+        }
+    }
+
+    /**
+     * SP 中读取 boolean
+     *
+     * @param key 键
+     * @return 存在返回对应值,不存在返回默认值{@code false}
+     */
+    public boolean getBoolean(@NonNull final String key) {
+        return getBoolean(key, false);
+    }
+
+    /**
+     * SP 中读取 boolean
+     *
+     * @param key          键
+     * @param defaultValue 默认值
+     * @return 存在返回对应值,不存在返回默认值{@code defaultValue}
+     */
+    public boolean getBoolean(@NonNull final String key, final boolean defaultValue) {
+        return sp.getBoolean(key, defaultValue);
+    }
+
+    /**
+     * SP 中写入 String 集合
+     *
+     * @param key    键
+     * @param values 值
+     */
+    public void put(@NonNull final String key, @NonNull final Set<String> values) {
+        put(key, values, false);
+    }
+
+    /**
+     * SP 中写入 String 集合
+     *
+     * @param key      键
+     * @param values   值
+     * @param isCommit {@code true}: {@link SharedPreferences.Editor#commit()}<br>
+     *                 {@code false}: {@link SharedPreferences.Editor#apply()}
+     */
+    public void put(@NonNull final String key,
+                    @NonNull final Set<String> values,
+                    final boolean isCommit) {
+        if (isCommit) {
+            sp.edit().putStringSet(key, values).commit();
+        } else {
+            sp.edit().putStringSet(key, values).apply();
+        }
+    }
+
+    /**
+     * SP 中读取 StringSet
+     *
+     * @param key 键
+     * @return 存在返回对应值,不存在返回默认值{@code Collections.<String>emptySet()}
+     */
+    public Set<String> getStringSet(@NonNull final String key) {
+        return getStringSet(key, Collections.<String>emptySet());
+    }
+
+    /**
+     * SP 中读取 StringSet
+     *
+     * @param key          键
+     * @param defaultValue 默认值
+     * @return 存在返回对应值,不存在返回默认值{@code defaultValue}
+     */
+    public Set<String> getStringSet(@NonNull final String key,
+                                    @NonNull final Set<String> defaultValue) {
+        return sp.getStringSet(key, defaultValue);
+    }
+
+    /**
+     * SP 中获取所有键值对
+     *
+     * @return Map 对象
+     */
+    public Map<String, ?> getAll() {
+        return sp.getAll();
+    }
+
+    /**
+     * SP 中是否存在该 key
+     *
+     * @param key 键
+     * @return {@code true}: 存在<br>{@code false}: 不存在
+     */
+    public boolean contains(@NonNull final String key) {
+        return sp.contains(key);
+    }
+
+    /**
+     * SP 中移除该 key
+     *
+     * @param key 键
+     */
+    public void remove(@NonNull final String key) {
+        remove(key, false);
+    }
+
+    /**
+     * SP 中移除该 key
+     *
+     * @param key      键
+     * @param isCommit {@code true}: {@link SharedPreferences.Editor#commit()}<br>
+     *                 {@code false}: {@link SharedPreferences.Editor#apply()}
+     */
+    public void remove(@NonNull final String key, final boolean isCommit) {
+        if (isCommit) {
+            sp.edit().remove(key).commit();
+        } else {
+            sp.edit().remove(key).apply();
+        }
+    }
+
+    /**
+     * SP 中清除所有数据
+     */
+    public void clear() {
+        clear(false);
+    }
+
+    /**
+     * SP 中清除所有数据
+     *
+     * @param isCommit {@code true}: {@link SharedPreferences.Editor#commit()}<br>
+     *                 {@code false}: {@link SharedPreferences.Editor#apply()}
+     */
+    public void clear(final boolean isCommit) {
+        if (isCommit) {
+            sp.edit().clear().commit();
+        } else {
+            sp.edit().clear().apply();
+        }
+    }
+
+    private static boolean isSpace(final String s) {
+        if (s == null) return true;
+        for (int i = 0, len = s.length(); i < len; ++i) {
+            if (!Character.isWhitespace(s.charAt(i))) {
+                return false;
+            }
+        }
+        return true;
+    }
+}