Android service based music player

This paper develops a service-based music player. The music is played by the service running in the background. When the playing state in the background changes, the program will notify the foreground activity to update the interface by sending a broadcast; When you click the interface button of activity, the system will change the playback status by sending a broadcast notification to the background service.

The foreground activity interface has two buttons to control play / pause and stop respectively, and two text boxes to display the name of the song and singer being played. The code of foreground activity is as follows:

Activityreceiver () is used to respond to the broadcast sent by the background service. The program will change the playback status according to the message in the broadcast intent and update the button icon in the program interface.

Onclick sends the broadcast according to the clicked button. When sending the broadcast, the pressed button ID will be sent.

Next is the background service, which will send broadcasts when the playback state changes. The code is as follows:

Myreceiver is used to receive the broadcast sent by the foreground activity and change the playing state of the service according to the broadcast message content. When the playing state changes, the service sends a broadcast to the outside world, the broadcast message will be received by the foreground activity, and the foreground activity will update the interface according to the broadcast message.

In order to enable the music player to play songs in order, the program adds an oncompletionlistener listener to mediaplayer. When mediaplayer plays, it will automatically play the next song.

Run the program, and the effect diagram is as follows:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>