The main function

  • Pause, and the playback status resumes
  • Switch from one song to the next
  • Playlist reset
  • Playback progress monitor
  • The volume listening

preface

Broadcast is a widely used mechanism for transmitting information between applications, while broadcastReceivers are components used to receive and respond to Broadcast pairs from systems and applications. In Android, the broadcast we want to send is an Intent that carries the data we want to send. Creating a BroadcastReceiver is as simple as inheriting BroadcastReceiver and rewriting onReceive(). BroadcastReceiver is also one of the four major components, so we need to register BroadcastReceiver. Different from the other four components, there are two ways to register BroadcastReceiver, namely static registration and dynamic registration.

And the reason to use the broadcast mechanism to make mobile phone music box; Because now are online music boxes, when we click to play a song, we need to take data from the server, and then play, this is actually more than a process, can be understood as multi-threaded, ordinary function call implementation effect is not good. Different from Binder mechanisms, broadcast senders and receivers do not need to know each other’s existence in advance. The advantage of this is that the components of the system can be loosely coupled together, making the system highly extensible and easy to integrate with other systems.

Music box code implementation

Layout file

A layout_update.xml file.

For the XML file, I used the Abel Layout layout. It has a tag that represents a row, which acts as a small container in the layout. The grid layout has no markers to represent columns, which are implemented by adding components to row markers, one column for each component. In this way, the interface Settings of the music box are relatively loose, which is convenient for me to carry out the simple layout of the music box. Each row is a TableRow, which sets the components of the layout. Each TableRow is a LinearLayout that ADAPTS the height of the image.

MainActivity. Java file

Here to realize sending broadcast and receiving music service return broadcast, user click operation.

1. Monitor the clicking events of 4 buttons, and send broadcast when clicking events occur;

2. Define a BroadcastReceiver to listen for broadcasts returned from a Service.

3. Set the state of the control system according to the information carried in the return broadcast, and change the pattern display and text display of the song information of the play key in the interface.

See the source repository musicService.java file for details

MusicService. Java file

Here is the music playback service, in this implementation of music playback logic. It implements play, pause and stop using a status record to achieve the effect of the current state. When realizing the function of the previous and the next songs, first of all, three songs are imported into the assets folder in the instance code, and there are only three groups of data in the music array. Current is used to record the subscript, and the key point of the previous and the next songs is to cross the boundary. When current<=0 and current>=3, If current=0, set current to the last item in the array, music.length-1. Normally if you click on the previous song, just subtract one from current and play the current song. The next implementation is similar, which is the case with current>=3. Since we might then add new songs ourselves, using current>=3 is not sufficient in all cases, so I used current>= musics.length to adjust the conditions to the length of the song list so that we could add songs without constantly having to change the code.

Then set up a BroadcastReceiver, receive broadcast information stored in variables; Then judge the status of the player through the switch judgment statement to achieve pause and play. (the last one next judgment statement is very troublesome, debugging before a point on the last one into the next loop; It took me ages to get it right.

int control = intent.getIntExtra("control", -1); If (status == 0x11) {// prepareAndPlay music prepareAndPlay(musics[current]); // prepareAndPlay music prepareAndPlay(musics[current]); status = 0x12; } else if (status == 0x12) {// Pause mplayer.pause (); Status = 0x13; } else if (status == 0x13) {// Start mplayer.start (); // Change status status = 0x12; } break; / / stop sound case 2: / / if the original is play or pause the if (status = = 0 x12 | | status = = 0 x13) {/ / stop play mPlayer. Stop (); status = 0x11; } break; Case 3: / / it is in a state of no play or pause the if (status = = 0 x11 | | status = = 0 x13) {if (current = = 0) {current = musics. Length - 1. } else {current=current-1; } prepareAndPlay(musics[current]); status=0x12; Else if(current==0) {current=musics.length-1; } else { current=current-1; } prepareAndPlay(musics[current]); } break; Case 4: / / in a state of no play or pause the if (status = = 0 x11 | | status = = 0 x13) {if (current = = musics. Length - 1) {current = 0; } // Prepare and play music else {current=current+1; } prepareAndPlay(musics[current]); status=0x12; } else if (status==0x12) { if(current==musics.length-1) { current=0; } else { current=current+1; } prepareAndPlay(musics[current]); } break; }Copy the code

After changing the update status, send a broadcast to change the player icon:

Intent sendIntent = new Intent(mainactivity.update_action); sendIntent.putExtra("update", status); sendIntent.putExtra("current", current); SendBroadcast (sendIntent) is received by the BroadcastReceiver in the Activity component.Copy the code

My music box source repository: code cloud gitee.com/SDVA8766304…