MusicNotification.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package com.jld.vod.audio;
  2. import android.annotation.SuppressLint;
  3. import android.app.Notification;
  4. import android.app.NotificationChannel;
  5. import android.app.NotificationManager;
  6. import android.app.PendingIntent;
  7. import android.content.BroadcastReceiver;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.content.IntentFilter;
  11. import android.os.Build;
  12. import android.widget.ImageView;
  13. import android.widget.RemoteViews;
  14. import com.jld.vod.R;
  15. import com.jld.vod.view.MainActivity;
  16. /**
  17. * Created by ding on 2016/12/3.
  18. * 状态栏的音频播放控制器
  19. */
  20. public class MusicNotification {
  21. /**
  22. * 音频播放控制 的 Notification
  23. * 动态的显示后台的AudioplayService的前台展示
  24. */
  25. private static MusicNotification notifyInstance = null;
  26. // 通知id
  27. private final int FLAG = PendingIntent.FLAG_UPDATE_CURRENT;
  28. private final int NOTIFICATION_ID = 0x1213;
  29. // 通知
  30. private Notification musicNotifi = null;
  31. // 管理通知
  32. private NotificationManager manager = null;
  33. // 界面实现
  34. private Notification.Builder builder = null;
  35. // 上下文
  36. private Context context;
  37. MusicController musicController;
  38. // 布局
  39. private RemoteViews remoteViews;
  40. private Intent playsIntent = null;
  41. /**
  42. * 上一首 按钮点击 ID
  43. */
  44. private final static int BUTTON_PREV_ID = 1;
  45. /**
  46. * 播放/暂停 按钮点击 ID
  47. */
  48. private final static int BUTTON_PALY_ID = 2;
  49. /**
  50. * 下一首 按钮点击 ID
  51. */
  52. private final static int BUTTON_NEXT_ID = 3;
  53. /**
  54. * 关闭 按钮点击 ID
  55. */
  56. private final static int BUTTON_COSE_ID = 4;
  57. /**
  58. * 跳转播放界面 按钮点击 ID
  59. */
  60. private final static int BUTTON_JUMP_ID = 5;
  61. private final static String ACTION_BUTTON = "xinkunic.aifatushu.customviews.MusicNotification.ButtonClick";
  62. private final static String INTENT_BUTTONID_TAG = "ButtonId";
  63. private ImageView imageView;
  64. private MusicNotification() {
  65. }
  66. private MusicNotification(Context context, MusicController musicController) {
  67. this.context = context;
  68. this.musicController = musicController;
  69. // 初始化操作
  70. remoteViews = new RemoteViews(context.getPackageName(), R.layout.music_view_custom_button);
  71. builder = new Notification.Builder(context);
  72. // 初始化控制的Intent
  73. playsIntent = new Intent();
  74. playsIntent.setAction(ACTION_BUTTON);
  75. imageView = new ImageView(context);
  76. onCreateMusicNotifi(context);
  77. }
  78. /**
  79. * 恶汉式实现 通知
  80. *
  81. * @return
  82. */
  83. public static MusicNotification getMusicNotification(Context context, MusicController musicController) {
  84. if (notifyInstance == null) {
  85. synchronized (MusicNotification.class) {
  86. if (notifyInstance == null) {
  87. notifyInstance = new MusicNotification(context, musicController);
  88. }
  89. }
  90. }
  91. return notifyInstance;
  92. }
  93. /**
  94. * 创建通知
  95. * 初始化通知
  96. */
  97. @SuppressLint("NewApi")
  98. public void onCreateMusicNotifi(Context context) {
  99. // 设置点击事件
  100. manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  101. // manager = NotificationManagerCompat.from(context);
  102. registerClick();
  103. Intent intent1 = new Intent(context, MainActivity.class);
  104. intent1.putExtra("isHead", true);
  105. PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
  106. //
  107. builder.setContent(remoteViews).setWhen(System.currentTimeMillis())
  108. .setContentIntent(pendingIntent)
  109. .setOngoing(true)
  110. .setSmallIcon(R.mipmap.ic_launcher);
  111. //Android 8.0以后
  112. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  113. NotificationChannel channel = new NotificationChannel(context.getPackageName(), "有章音频", NotificationManager.IMPORTANCE_HIGH);
  114. channel.enableLights(false);
  115. channel.enableVibration(false);
  116. channel.setVibrationPattern(new long[]{0});
  117. channel.setSound(null, null);
  118. manager.createNotificationChannel(channel);
  119. builder.setChannelId(context.getPackageName());
  120. } else {
  121. builder.setVibrate(new long[]{0});
  122. builder.setSound(null);
  123. }
  124. musicNotifi = builder.build();
  125. initButtonReceiver(context);
  126. }
  127. private void registerClick() {
  128. // 1.注册播放或暂停点击事件
  129. playsIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_PALY_ID);
  130. PendingIntent pplay = PendingIntent.getBroadcast(context, 1, playsIntent, FLAG);
  131. remoteViews.setOnClickPendingIntent(R.id.btn_custom_play, pplay);
  132. // 2.注册上一首点击事件
  133. playsIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_PREV_ID);
  134. PendingIntent playPre = PendingIntent.getBroadcast(context, 3, playsIntent, FLAG);
  135. remoteViews.setOnClickPendingIntent(R.id.btn_custom_prev, playPre);
  136. // 3.注册下一首点击事件
  137. playsIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_NEXT_ID);
  138. PendingIntent pnext = PendingIntent.getBroadcast(context, 2, playsIntent, FLAG);
  139. remoteViews.setOnClickPendingIntent(R.id.btn_custom_next, pnext);
  140. // 4.注册关闭点击事件
  141. playsIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_COSE_ID);
  142. PendingIntent pclose = PendingIntent.getBroadcast(context, 4, playsIntent, FLAG);
  143. remoteViews.setOnClickPendingIntent(R.id.btn_custom_close, pclose);
  144. }
  145. private void initButtonReceiver(Context context) {
  146. ButtonBroadcastReceiver bReceiver = new ButtonBroadcastReceiver();
  147. IntentFilter intentFilter = new IntentFilter();
  148. intentFilter.addAction(ACTION_BUTTON);
  149. context.registerReceiver(bReceiver, intentFilter);
  150. }
  151. /**
  152. * 取消通知栏
  153. */
  154. public void onCancelMusicNotifi() {
  155. if (manager == null) return;
  156. manager.cancel(NOTIFICATION_ID);
  157. isShowing = false;
  158. }
  159. private String TAG = "---MusicNotification--";
  160. private boolean isPlaying = false;
  161. /**
  162. * 更新通知
  163. */
  164. public void upDataNotifacation(boolean needRefrush, final String name, String faceUrl, boolean isPlaying) {
  165. this.isPlaying = isPlaying;
  166. if (remoteViews == null) {
  167. return;
  168. }
  169. if (needRefrush) {
  170. remoteViews.setTextViewText(R.id.tv_custom_song_singer, name);
  171. }
  172. if (isPlaying) {
  173. remoteViews.setImageViewResource(R.id.btn_custom_play, android.R.drawable.ic_media_pause);
  174. } else {
  175. remoteViews.setImageViewResource(R.id.btn_custom_play, android.R.drawable.ic_media_play);
  176. }
  177. show();
  178. }
  179. boolean isShowing = false;
  180. private void show() {
  181. // if (isShowing) {
  182. // return;
  183. // }
  184. manager.notify(NOTIFICATION_ID, musicNotifi);
  185. isShowing = true;
  186. }
  187. public class ButtonBroadcastReceiver extends BroadcastReceiver {
  188. @Override
  189. public void onReceive(Context context, Intent intent) {
  190. // TODO Auto-generated method stub
  191. String action = intent.getAction();
  192. if (action.equals(ACTION_BUTTON)) {
  193. int buttonId = intent.getIntExtra(INTENT_BUTTONID_TAG, 0);
  194. switch (buttonId) {
  195. case BUTTON_PREV_ID://上一首
  196. if (musicController != null) {
  197. musicController.pre();
  198. }
  199. break;
  200. case BUTTON_PALY_ID://播放或暂停
  201. if (musicController != null) {
  202. if (musicController.isPlaying()) {
  203. musicController.pause();
  204. } else {
  205. musicController.play();
  206. }
  207. }
  208. break;
  209. case BUTTON_NEXT_ID://下一首
  210. if (musicController != null) {
  211. musicController.next();
  212. }
  213. break;
  214. case BUTTON_COSE_ID://关闭
  215. onCancelMusicNotifi();
  216. if (musicController != null) {
  217. musicController.release();
  218. }
  219. break;
  220. default:
  221. break;
  222. }
  223. }
  224. }
  225. }
  226. }