How to Use the Android listening system music player to play music

Source: Internet
Author: User

Recently, when I was working on a project, I encountered a small problem. I needed to write a View to control the playing behavior of the system music player and get information about the music being played, maybe I just wanted to save some time at the beginning, so I didn't go directly to the source code, but searched the internet, but I was surprised that I didn't actually have this online. Why can't I say I have done this without my shoes? Hey, let's get started:

Obtain the information of the Music being played by the system. You can view the source code. The Music app has a MediaPlaybackService. I will not write the internal code for the java file. In short, the broadcast will be sent in four cases when playing music. These broadcasts contain information about the music being played, so we only need to write a broadcast to accept the information:

MusicBroadcastReceiver mbr = null; // Definition

Class musicbroadcastreceiver extends broadcastreceiver {@ overridepublic void onreceive (context, intent) {artistname = intent. getstringextra ("artist"); album = intent. getstringextra ("album"); track = intent. getstringextra ("track"); playing = intent. getbooleanextra ("playing", false); duration = intent. getlongextra ("duration", 3000); position = intent. getlongextra ("position", 1000); toast. Maketext (testactivity. this, "playing:" + playing, toast. length_short ). show (); If (PLAYING) {SD. setvisibility (view. visible);} else {If ((! SD. isopened () {SD. setvisibility (view. gone) ;}} if (PLAYING) {(imageview) findviewbyid (R. id. play )). setimageresource (R. drawable. music_pause);} else {(imageview) findviewbyid (R. id. play )). setimageresource (R. drawable. music_play);} (textview) findviewbyid (R. id. musicname )). settext (track); (textview) findviewbyid (R. id. musicinfo )). settext (artistname + "/" + album );}}

The above is a receiver of the broadcasting industry, which needs to be registered at an appropriate time. Because MediaPlaybackService sends a sticky broadcast, therefore, our receiver can also receive this broadcast when the application is opened, regardless of the TextView or ImageView above.

Register broadcast:

@Overrideprotected void onResume() {/** * *", * "" * "" */mbr = new MusicBroadcastReceiver();IntentFilter intentFilter = new IntentFilter();intentFilter.addAction("");intentFilter.addAction("");intentFilter.addAction("");intentFilter.addAction("");registerReceiver(mbr, intentFilter);super.onResume();}

Cancel broadcast:

@Overrideprotected void onPause() {if(mbr != null){unregisterReceiver(mbr);}super.onPause();}

Control the playing of Music:

A MediaButtonIntentReceiver is also accepted in the Music application. When we can send specific broadcasts to control the playing of Music, as shown below:

private static final String SERVICECMD = "";    private static final String CMDNAME = "command";/*    private static final String CMDTOGGLEPAUSE = "togglepause";    private static final String CMDSTOP = "stop";*/    private static final String CMDPAUSE = "pause";    private static final String CMDPLAY = "play";    private static final String CMDPREVIOUS = "previous";    private static final String CMDNEXT = "next";public void click(View v){Intent intent = new Intent(SERVICECMD);switch(v.getId()){case{intent.putExtra(CMDNAME, CMDPREVIOUS);}break;case{if(playing){intent.putExtra(CMDNAME,CMDPAUSE);        ((ImageView)findViewById(;        }else{        intent.putExtra(CMDNAME,CMDPLAY);        ((ImageView)findViewById(;                }}break;case{intent.putExtra(CMDNAME,CMDNEXT);}break;}sendBroadcast(intent);}

When broadcasting, you do not need to set Extra for Inent:

Private Static final string servicecmd = "com. android. music. musicservicecommand "; Private Static final string naming name =" command ";/* Private Static final string listing togglepause =" togglepause "; Private Static final string stopping stop =" stop "; */Private Static final string release pause = "pause"; Private Static final string release play = "play"; Private Static final string release previous = "previous "; private Static final string using next = "Next"; Replace with Private Static final string pause = "com. android. music. musicservicecommand. pause "; Private Static final string next =" com. android. music. musicservicecommand. next "; Private Static final string stop =" com. android. music. musicservicecommand. stop "; Private Static final string previous =" com. android. music. musicservicecommand. previous ";

Then new intent (pause/next/Stop/previous); can be sent through sendbroadcast (intent); but I have not personally verified it. You can try it, the first is verified.

Stick out the schematic diagram: Below is the figure I used to lock the screen. The left part is related to the screen lock and should not be affected by it.


Related Article

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: 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.