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() {/** * com.android.music.metachanged * com.android.music.queuechanged", * "com.android.music.playbackcomplete" * "com.android.music.playstatechanged" */mbr = new MusicBroadcastReceiver();IntentFilter intentFilter = new IntentFilter();intentFilter.addAction("com.android.music.metachanged");intentFilter.addAction("com.android.music.queuechanged");intentFilter.addAction("com.android.music.playbackcomplete");intentFilter.addAction("com.android.music.playstatechanged");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 = "com.android.music.musicservicecommand"; 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 R.id.pre:{intent.putExtra(CMDNAME, CMDPREVIOUS);}break;case R.id.play:{if(playing){intent.putExtra(CMDNAME,CMDPAUSE); ((ImageView)findViewById(R.id.play)).setImageResource(R.drawable.music_play); }else{ intent.putExtra(CMDNAME,CMDPLAY); ((ImageView)findViewById(R.id.play)).setImageResource(R.drawable.music_pause); }}break;case R.id.next:{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.