Obtain the music player status on the Android mobile phone

Source: Internet
Author: User

I recently worked on a screen lock project in my company. I need to display the status of the music player on the screen lock page, similar to the screen lock page of Xiaomi. There are similar functions available on the Internet. Let's make a summary here.

First of all, this data information is sent from the Service (COM. Android. Music. mediaplaybackservice) in the music player in the form of broadcast.

Some third-party players also offer this service. For example, if there are more than one player on the mobile phone to implement this service, chaos will occur (Xiaomi mobile phones also have this phenomenon ).

The main data includes the music file ID, author, and so on. The album images in the interface are queried from the data, which will be introduced later.

public long id;

Public String artistname; Public String album; Public String track; Public Boolean playing; public long duration; public long position;

Let's talk about the playback control. The functions that can be used on the screen lock are relatively simple, including playing, pausing, playing, and playing the last song and playing the next song.

This write playback control is to start a service and send the command as a parameter. Then, the service returns a broadcast and returns the status of the current music player.

Let's talk about album pictures. These album pictures are extracted from music files and have similar protocols and algorithms. We will not introduce them here. These things are implemented in Android systems, we just need to use it.

It's a complete tailism, and despise yourself ~~~

The preceding object class has an ID field. In this way, you can query mediastore from the media content provider of Android and mediastore by ID. audio. media. album_id: Open the corresponding file according to album_id.

/*** Get the album image played by the current music. If not, null * @ Param context * @ return */Public bitmap getalbumcover (context) {bitmap BMP = NULL is returned; if (ID <= 0) {return NULL;} uri = contenturis. withappendedid (mediastore. audio. media. external_content_uri, ID); cursor = context. getcontentresolver (). query (Uri, null, null); If (cursor! = NULL & cursor. movetonext () {long albumid = cursor. getlong (cursor. getcolumnindex (mediastore. audio. media. album_id); Uri albumuripath = Uri. parse (album_cover_uri_path); Uri albumiduri = contenturis. withappendedid (albumuripath, albumid); parcelfiledescriptor FD = NULL; try {FD = context. getcontentresolver (). openfiledescriptor (albumiduri, "R");} catch (filenotfoundexception e) {} If (FD! = NULL) {filedescriptor FDC = FD. getfiledescriptor (); BMP = bitmapfactory. decodefiledescriptor (FDC); try {If (FD! = NULL) {FD. Close () ;}} catch (ioexception e) {}} return BMP ;}}

The complete code is as follows:

Import Java. io. filedescriptor; import Java. io. filenotfoundexception; import Java. io. ioexception; import android. content. broadcastreceiver; import android. content. contenturis; import android. content. context; import android. content. intent; import android. content. intentfilter; import android. database. cursor; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android.net. uri; import android Oid. OS. handler; import android. OS. parcelfiledescriptor; import android. provider. mediastore;/*** remote music Control ** title: remotemusiccontrol. java <br> * Author: xiaqiulei@gmail.com <br> * Date: 2013-5-30 <br> * version: V1.0 */public class remotemusiccontrol {static final string playstate_changed = "com. android. music. playstatechanged "; static final string meta_changed =" com. android. music. metachanged "; static fi NAL string queue_changed = "com. android. music. queuechanged "; static final string playback_complete =" com. android. music. playbackcomplete "; static final string quit_playback =" com. android. music. quitplayback "; static final string service_action =" com. android. music. mediaplaybackservice "; static final string servicecmd =" com. android. music. musicservicecommand "; static final string naming name =" command "; sta TIC final string listing togglepause = "togglepause"; static final string listing stop = "stop"; static final string listing pause = "pause"; static final string listing play = "play "; static final string implements previous = "previous"; static final string implements next = "Next"; private context mcontext; private Boolean playing = false; private Boolean misregister = false; private onremotemusiccontrolcallback onremotemusiccontrolcallbac K; private handler hander; Public remotemusiccontrol (context) {mcontext = context;}/*** register */Public void register () {hander = new handler (); intentfilter = new intentfilter (); intentfilter. addaction (playstate_changed); intentfilter. addaction (meta_changed); // intentfilter. addaction (queue_changed); intentfilter. addaction (playback_complete); // intentfilter. addaction (quit_playback); mcon Text. registerreceiver (receiver, intentfilter); intent it = new intent (service_action); mcontext. startservice (it); misregister = true;}/*** deregister */Public void unregister () {If (guest er! = NULL) {mcontext. unregisterreceiver (receiver);} misregister = false; hander = NULL;}/*** Execute Command * @ Param cmd */void doremotemusic (string cmd) {intent = new intent (servicecmd); intent. putextra (cmdname, CMD); mcontext. sendbroadcast (intent);}/*** next song */Public void doremotemusicnext () {doremotemusic (previous next);}/*** previous song */Public void doremotemusicperv () {doremotemusic (audio previous);}/*** play */Public Void doremotemusicplay () {doremotemusic (audio play);}/*** pause */Public void doremotemusicpause () {doremotemusic (audio pause );} /*** pause or play */Public void doremotemusicplayorpause () {If (PLAYING) {doremotemusic (audio pause);} else {doremotemusic (audio play );}} /*** broadcast accepted by music service */broadcastreceiver receiver ER = new broadcastreceiver () {public void onreceive (context, intent) {final musicextra me = new m Usicextra (); me. id = intent. getlongextra ("ID", 0); me. artistname = intent. getstringextra ("artist"); me. album = intent. getstringextra ("album"); me. track = intent. getstringextra ("track"); me. playing = intent. getbooleanextra ("playing", false); me. duration = intent. getlongextra ("duration", 3000); me. position = intent. getlongextra ("position", 1000); playing = me. playing; If (hander! = NULL) {hander. Post (New runnable () {public void run () {If (onremotemusiccontrolcallback! = NULL) {onremotemusiccontrolcallback. oncallback (me) ;}}}};/*** register * @ return */Public Boolean isregister () {return misregister;} public onremotemusiccontrolcallback getonremotemusiccontrolcallback () {return onremotemusiccontrolcallback;} public void setonremotemusiccontrolcallback (onremotemusiccontrolcallback) {This. onremotemusiccontrolcallback = onremotemusiccontro Lcallback;}/*** music playback status */public static class musicextra {static final string album_cover_uri_path = "content: // media/external/Audio/albumart"; public long ID; public String artistname; Public String album; Public String track; Public Boolean playing; public long duration; public long position; Public String tostring () {return "musicextra [ID =" + ID + ", artistname =" + artistname + ", album =" + album + ", Track = "+ track +", playing = "+ playing +", duration = "+ duration +", position = "+ Position +"] ";} /*** get the album image played by the current music. If not, null * @ Param context * @ return */Public bitmap getalbumcover (context) {bitmap BMP = NULL is returned; if (ID <= 0) {return NULL;} uri = contenturis. withappendedid (mediastore. audio. media. external_content_uri, ID); cursor = context. getcontentresolver (). query (Uri, null, Null); If (cursor! = NULL & cursor. movetonext () {long albumid = cursor. getlong (cursor. getcolumnindex (mediastore. audio. media. album_id); Uri albumuripath = Uri. parse (album_cover_uri_path); Uri albumiduri = contenturis. withappendedid (albumuripath, albumid); parcelfiledescriptor FD = NULL; try {FD = context. getcontentresolver (). openfiledescriptor (albumiduri, "R");} catch (filenotfoundexception e) {} If (FD! = NULL) {filedescriptor FDC = FD. getfiledescriptor (); BMP = bitmapfactory. decodefiledescriptor (FDC); try {If (FD! = NULL) {FD. close () ;}} catch (ioexception e) {}} return BMP ;}} /*** remote music playback control space callback */public interface onremotemusiccontrolcallback {public void oncallback (musicextra extra );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.