Android plays background music through startservice

For an overview of the basic use of startservice and its life cycle, see overview of basic use methods of startservice in Android.

This article demonstrates the basic use process of startservice through a simple example of playing background music. The specific contents are as follows

The system interface is as follows:

There are two buttons on the interface, "play music and exit activity" and "stop playing music". In this example, we control musicservice to play or stop playing music by manipulating the button of activity.

I put a file named music.mp3 under the / RES / raw folder of the resource directory, so that we can refer to the music file through r.raw.music in the program. The resource file in the / RES / raw folder will maintain its original appearance and will not be compiled into binary.

Musicservice is a service for playing background music. Its code is as follows:

The code of musicactivity is as follows:

After we click the button "play music and exit activity", we first start musicservice through the activity's startservice, and then we immediately call the activity's finish method to destroy the current activity. You may ask why you want to destroy the current activity? The reason why we call the finish method of activity here is not from the perspective of function, but from the perspective of understanding the operation of code: after executing the finish method of activity, the current activity is destroyed. On the interface, it seems that the current UI is lost and the application exits, but wait a moment and you will hear the background music. This confirms a feature of service from the side: like activity, service is also a basic application component. Service can run silently in the Android background without any UI interface without relying on any activity.

After calling startservice, the Android framework receives the intent information. For the first time, it will create an instance of musicservice and execute the oncreate callback method of musicservice. Oncreate will only be called once in the service life cycle. We initialize r.raw.music as a media player in its oncreate method and call the prepare method of the media player. Then we set the player to cycle. It should be noted that in the actual production environment, we should register the player's setonpreparedlistener and call the prepareasync () method. To simplify the code, we only call the player's synchronization method prepare ().

After the oncreate method is called, Android will automatically call back its onstartcommand method. In fact, each time the startservice of context is called, the onstartcommand callback method will be triggered, so onstartcommand may be called many times in the service life cycle. Therefore, we make a judgment in onstartcommand of musicservice to judge whether the player is playing. If the current player is not playing, we call the player's start method to play background music.

After we click the button "play music and exit activity", musicservice starts to play background music, but the activity is destroyed and the UI interface of the program is missing. In order to stop playing background music, we need to click the application icon again, reopen musicactivity, and then click the stop playing music button on the interface. At this time, we will call the stopservice method of activity. After receiving the intention to stop the service, Android framework will call back the ondestroy method of musicservice, In this method, we stop playing music and release media player resources.

This article is just a simple example of playing background music to demonstrate the basic use process of starting service through startservice. The code is not optimized. I hope it will be helpful for you to learn service.

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
分享
二维码
< <上一篇
下一篇>>