您好,登錄后才能下訂單哦!
今天小編給大家分享一下Android媒體通知欄多系統適配怎么實現的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
需要考慮的問題如下:
1,通知欄適配,音樂播放需要常駐,所以要維護一個通知欄。
2,音控處理,在安卓7.0及以下,通過MediaSessionCompat可控制鎖屏頁音樂播放。
3,對于耳機的處理,不管是線耳機還是藍牙耳機,耳機控制播放暫停,下一曲上一曲等操作。
4,打電話處理,在聽音樂的同時如果電話進來后掛斷,希望可以自動播放。
5,音頻播放焦點處理,如果有別的應用搶占焦點可進行暫停播放。還有就是進入APP時想擁有音頻焦點,都可以通過AudioManager進行處理。
創建通知管理類NotifyBuilderManager代碼如下:
package com.idujing.myapplication.manager; import android.annotation.SuppressLint; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; import android.os.Build; import androidx.core.app.NotificationCompat; import com.idujing.myapplication.R; /** * 音頻播放通知欄管理 */ public class NotifyBuilderManager { private final String TAG = getClass().getSimpleName(); public static final String ACTION_NEXT = "com.idujing.play.notify.next";// 下一首 public static final String ACTION_PREV = "com.idujing.play.notify.prev";// 上一首 public static final String ACTION_PLAY_PAUSE = "com.idujing.play.notify.play_state";// 播放暫停廣播 private static final int NOTIFICATION_ID = 0x123; private Service mContext; private Notification mNotification; private NotificationManager mNotificationManager; private NotificationCompat.Builder mNotificationBuilder; private MediaSessionManager mSessionManager; private PendingIntent mPendingPlay; private PendingIntent mPendingPre; private PendingIntent mPendingNext; private boolean isRunningForeground = false; public boolean isRunningForeground() { return isRunningForeground; } public NotifyBuilderManager(Service context) { this.mContext = context; mSessionManager = new MediaSessionManager(context, null); } /** * 初始化通知欄 */ private void initNotify() { mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); Class<?> clazz = null; try { clazz = Class.forName("具體的播放器類名"); } catch (ClassNotFoundException e) { e.printStackTrace(); } // 適配12.0及以上 int flag; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { flag = PendingIntent.FLAG_IMMUTABLE; } else { flag = PendingIntent.FLAG_UPDATE_CURRENT; } //綁定事件通過創建的具體廣播去接收即可。 Intent infoIntent = new Intent(mContext, clazz); PendingIntent pendingInfo = PendingIntent.getActivity(mContext, 0, infoIntent, flag); Intent preIntent = new Intent(); preIntent.setAction(ACTION_PREV); mPendingPre = PendingIntent.getBroadcast(mContext, 1, preIntent, flag); Intent playIntent = new Intent(); playIntent.setAction(ACTION_PLAY_PAUSE); mPendingPlay = PendingIntent.getBroadcast(mContext, 2, playIntent, flag); Intent nextIntent = new Intent(); nextIntent.setAction(ACTION_NEXT); mPendingNext = PendingIntent.getBroadcast(mContext, 3, nextIntent, PendingIntent.FLAG_IMMUTABLE); androidx.media.app.NotificationCompat.MediaStyle style = new androidx.media.app.NotificationCompat.MediaStyle() .setShowActionsInCompactView(0, 1, 2) .setMediaSession(mSessionManager.getMediaSession()); mNotificationBuilder = new NotificationCompat.Builder(mContext, initChannelId()) .setSmallIcon(R.mipmap.ic_launcher) .setPriority(NotificationCompat.PRIORITY_MAX) .setContentIntent(pendingInfo) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setStyle(style); isRunningForeground = true; } /** * 創建Notification ChannelID * * @return 頻道id */ private String initChannelId() { // 通知渠道的id String id = "music_01"; // 用戶可以看到的通知渠道的名字. CharSequence name = mContext.getString(R.string.app_name); // 用戶可以看到的通知渠道的描述 String description = "通知欄播放控制"; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { int importance = NotificationManager.IMPORTANCE_LOW; NotificationChannel channel = new NotificationChannel(id, name, importance); channel.setDescription(description); channel.enableLights(false); channel.enableVibration(false); mNotificationManager.createNotificationChannel(channel); } return id; } /** * 取消通知 */ public void cancelNotification() { if (mNotificationManager != null) { mContext.stopForeground(true); mNotificationManager.cancel(NOTIFICATION_ID); isRunningForeground = false; } } /** * 設置通知欄大圖片 */ private void updateCoverSmall() { Glide.with(mContext).asBitmap() .load(url) .into(new CustomTarget<Bitmap>() { @Override public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) { mNotificationBuilder.setLargeIcon(resource); mNotification = mNotificationBuilder.build(); mNotificationManager.notify(NOTIFICATION_ID, mNotification); } @Override public void onLoadCleared(@Nullable Drawable placeholder) { } @Override public void onLoadFailed(@Nullable Drawable errorDrawable) { super.onLoadFailed(errorDrawable); Log.e(TAG, "onLoadFailed: "); } }); } /** * 更新狀態欄通知 */ @SuppressLint("RestrictedApi") public void updateNotification(boolean isMusicPlaying) { if (mNotification == null) { initNotify(); } mSessionManager.updateMetaData(); if (mNotificationBuilder != null) { int playButtonResId = isMusicPlaying ? android.R.drawable.ic_media_pause : android.R.drawable.ic_media_play; if (!mNotificationBuilder.mActions.isEmpty()) { mNotificationBuilder.mActions.clear(); } mNotificationBuilder .addAction(android.R.drawable.ic_media_previous, "Previous", mPendingPre) // #0 .addAction(playButtonResId, "Pause", mPendingPlay) // #1 .addAction(android.R.drawable.ic_media_next, "Next", mPendingNext); mNotificationBuilder.setContentTitle("主標題"); mNotificationBuilder.setContentText("副標題"); updateCoverSmall(); mNotification = mNotificationBuilder.build(); mContext.startForeground(NOTIFICATION_ID, mNotification); mNotificationManager.notify(NOTIFICATION_ID, mNotification); } } }
創建音控管理類MediaSessionManager代碼如下:
package com.idujing.myapplication.manager; import android.content.Context; import android.os.Handler; import android.support.v4.media.MediaMetadataCompat; import android.support.v4.media.session.MediaSessionCompat; import android.support.v4.media.session.PlaybackStateCompat; /** * 主要管理Android 5.0以后線控和藍牙遠程控制播放 */ public class MediaSessionManager { private static final String TAG = "MediaSessionManager"; //指定可以接收的來自鎖屏頁面的按鍵信息 private static final long MEDIA_SESSION_ACTIONS = PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS | PlaybackStateCompat.ACTION_STOP | PlaybackStateCompat.ACTION_SEEK_TO; private final Context mContext; private MediaSessionCompat mMediaSession; private Handler mHandler; public MediaSessionManager(Context context, Handler handler) { this.mContext = context; this.mHandler = handler; setupMediaSession(); } /** * 是否在播放 * * @return */ protected boolean isPlaying() { //具體去實現 return false; } /** * 初始化并激活 MediaSession */ private void setupMediaSession() { mMediaSession = new MediaSessionCompat(mContext, TAG); //指明支持的按鍵信息類型 mMediaSession.setFlags( MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS ); mMediaSession.setCallback(callback, mHandler); mMediaSession.setActive(true); } /** * 更新正在播放的音樂信息,切換歌曲時調用 */ public void updateMetaData() { MediaMetadataCompat.Builder metaDta = new MediaMetadataCompat.Builder() .putString(MediaMetadataCompat.METADATA_KEY_TITLE, "title") .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, "Artist") .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, "Album") .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, "Artist") .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, 100); mMediaSession.setMetadata(metaDta.build()); int state = isPlaying() ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED; mMediaSession.setPlaybackState(new PlaybackStateCompat.Builder() .setActions(MEDIA_SESSION_ACTIONS) .setState(state, 0, 1) .build()); //鎖屏頁封面設置,高本版沒有效果,因為通知欄權限調整。 Glide.with(mContext).asBitmap(). load(url) .into(new CustomTarget<Bitmap>() { @Override public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) { metaDta.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, resource); mMediaSession.setMetadata(metaDta.build()); } @Override public void onLoadCleared(@Nullable Drawable placeholder) { } }); } public MediaSessionCompat.Token getMediaSession() { return mMediaSession.getSessionToken(); } /** * 釋放MediaSession,退出播放器時調用 */ public void release() { mMediaSession.setCallback(null); mMediaSession.setActive(false); mMediaSession.release(); } /** * API 21 以上 耳機多媒體按鈕監聽 MediaSessionCompat.Callback */ private MediaSessionCompat.Callback callback = new MediaSessionCompat.Callback() { @Override public void onPlay() { //具體自己實現 } @Override public void onPause() { } @Override public void onSkipToNext() { } @Override public void onSkipToPrevious() { } @Override public void onStop() { } @Override public void onSeekTo(long pos) { } }; }
創建音頻焦點控制類AudioAndFocusManager
通過音頻焦點控制,不管是別的應用搶占焦點,還是打電話都可以接收到狀態。
package com.idujing.myapplication.manager; import android.content.Context; import android.media.AudioFocusRequest; import android.media.AudioManager; import android.os.Build; import android.util.Log; import androidx.annotation.RequiresApi; /** * Description: 主要用來管理音頻焦點 */ public class AudioAndFocusManager { private static final String TAG = "AudioAndFocusManager"; private AudioManager mAudioManager; @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public AudioAndFocusManager(Context mContext) { mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); } /** * 請求音頻焦點 */ public void requestAudioFocus() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { AudioFocusRequest mAudioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN) .setOnAudioFocusChangeListener(audioFocusChangeListener) .build(); int res = mAudioManager.requestAudioFocus(mAudioFocusRequest); if (res == 1) { Log.e(TAG, "res=" + true); } } else { if (audioFocusChangeListener != null) { boolean result = AudioManager.AUDIOFOCUS_REQUEST_GRANTED == mAudioManager.requestAudioFocus(audioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); Log.e(TAG, "requestAudioFocus result=" + result); } } } /** * 關閉音頻焦點 */ public void abandonAudioFocus() { if (audioFocusChangeListener != null) { boolean result = AudioManager.AUDIOFOCUS_REQUEST_GRANTED == mAudioManager.abandonAudioFocus(audioFocusChangeListener); Log.e(TAG, "abandonAudioFocus result=" + result); } } /** * 音頻焦點改變監聽器 */ private AudioManager.OnAudioFocusChangeListener audioFocusChangeListener = focusChange -> { switch (focusChange) { case AudioManager.AUDIOFOCUS_LOSS://失去音頻焦點 case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT://暫時失去焦點 break; case AudioManager.AUDIOFOCUS_GAIN://獲取焦點 break; default: } }; }
以上就是“Android媒體通知欄多系統適配怎么實現”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。