Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the music control of native lock screen page by Android

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "Android how to achieve native lock screen page music control", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Android how to achieve native lock screen page music control" this article.

Android5.0 proposes a new concept of MediaSession for interaction between the player and the controller, which replaces the previous RemoteControlClient and provides a more flexible client-side model.

But the MediaSession framework can only be used on Android 5, so how can it be compatible with lower versions? Google also provides MediaSessionCompact compatibility packs in support-v4 (version 21 and above). Through it, you can tell the Android system and other applications what they are playing and what types of playback controls they support.

When playing music, the monitoring on the lock screen button can be realized through MediaSessionCompat, which can be converted into its own operation.

This paper introduces the realization of lock screen interface for music playback control function based on MediaSessionCompat.

Package com.yobbom.jiheboxplayer.service;import android.os.Build;import android.support.v4.media.MediaMetadataCompat;import android.support.v4.media.session.MediaSessionCompat;import android.support.v4.media.session.PlaybackStateCompat;import com.yobbom.jiheboxplayer.model.Music;/** * Created by Administrator on 2019-4-3. * / 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 PlayService playService; private MediaSessionCompat mediaSession; public static MediaSessionManager get () {return SingletonHolder.instance;} private static class SingletonHolder {private static MediaSessionManager instance = new MediaSessionManager () } private MediaSessionManager () {} / / the control logic of music is all in the PlayService service. Pass the service instance and interact with MediaSessionManager. Private void init (PlayService playService) {this.playService = playService; setupMediaSession ();} private void setupMediaSession () {mediaSession = new MediaSessionCompat (playService, TAG); / / indicate the supported key information type mediaSession.setFlags (MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS) MediaSession.setCallback (callback); / / set the listening callback mediaSession.setActive (true); / / must be set to true so that you can start to receive all kinds of information} / / when you click the control button, update the playback status public void updatePlaybackState () {int state = (AudioPlayer.get (). IsPlaying () | | AudioPlayer.get (). IsPreparing ())? PlaybackStateCompat.STATE_PLAYING: the third parameter PlaybackStateCompat.STATE_PAUSED; / / must be 1, otherwise the length of time displayed on the lock screen will be problematic mediaSession.setPlaybackState (/ / listening events (playback, pause, last, next) new PlaybackStateCompat.Builder () .setActions (MEDIA_SESSION_ACTIONS) .setState (state, AudioPlayer.get (). GetAudioPosition (), 1) .build ()) } / / when playing a song, you need to update the song information on the screen public void updateMetaData (Music music) {if (music = = null) {mediaSession.setMetadata (null); return;} / / Log.d (TAG, "parseMp3File name:" + mmr.extractMetadata (MediaMetadataRetriever.METADATA_KEY_TITLE)); / / Log.d (TAG, "parseMp3File album:" + mmr.extractMetadata (MediaMetadataRetriever.METADATA_KEY_ALBUM)) / / Log.d (TAG, "parseMp3File Singer:" + mmr.extractMetadata (MediaMetadataRetriever.METADATA_KEY_ARTIST)); / / Log.d (TAG, "parseMp3File bitrate:" + mmr.extractMetadata (MediaMetadataRetriever.METADATA_KEY_BITRATE)); / / Log.d (TAG, "parseMp3File duration:" + mmr.extractMetadata (MediaMetadataRetriever.METADATA_KEY_DURATION)); / / Log.d (TAG, "parseMp3File Type:" + mmr.extractMetadata (MediaMetadataRetriever.METADATA_KEY_MIMETYPE) MediaMetadataCompat.Builder metaData = new MediaMetadataCompat.Builder () .putString (MediaMetadataCompat.METADATA_KEY_TITLE, music.getTitle ()) .putString (MediaMetadataCompat.METADATA_KEY_ARTIST, music.getArtist ()) .putString (MediaMetadataCompat.METADATA_KEY_ALBUM, music.getAlbum ()) .putString (MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, music.getArtist ()) .putLong (MediaMetadataCompat.METADATA_KEY_DURATION Music.getDuration () .putBitmap (MediaMetadataCompat.METADATA_KEY_ALBUM_ART, CoverLoader.get () .loadThumb (music)) If (Build.VERSION.SDK_INT > = Build.VERSION_CODES.LOLLIPOP) {metaData.putLong (MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, AppCache.get (). GetLocalMusicList (). Size ());} mediaSession.setMetadata (metaData.build ()) } / / initialize the callback, which is used to listen to the button event private MediaSessionCompat.Callback callback = new MediaSessionCompat.Callback () {@ Override public void onPlay () {AudioPlayer.get () .playPause ();} @ Override public void onPause () {AudioPlayer.get () .playPause ();} @ Override public void onSkipToNext () {AudioPlayer.get () .next () } @ Override public void onSkipToPrevious () {AudioPlayer.get () .prev ();} @ Override public void onStop () {AudioPlayer.get () .stopPlayer ();} @ Override public void onSeekTo (long pos) {AudioPlayer.get () .seekTo ((int) pos);}} } these are all the contents of the article "how to achieve native lock screen music control in Android". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report