Example of background playback function of music based on Android service

This paper describes the background playback function of music based on Android service. Share with you for your reference, as follows:

Service is a program with a long life cycle and no user interface. When the program switches between activities, we can use service to play background music. Even when the program exits to the background, the music is still playing. Below we give the implementation of specific examples:

Of course, first add an MP3 song to the resource folder:

To play music, two buttons need to be placed in the interface to control the playback and stop of music. These two functions are realized by using startservice and stopservice:

Modify servicedemoavtivity. Under Src Add the following button event code to the Java code:

The following is the more important part of the service. Create a musicservice, inherit from the service, and then control the playback of music through the start and stop methods. Here is musicservice Key codes in Java:

Of course, you also need to declare musicservice in androidmainfest:

The whole example is constructed and deployed to the simulator or mobile phone to realize background playback.

For more Android related content, readers who are interested can view the topics on this site: summary of Android service component usage skills, summary of activity operation skills of Android programming, summary of Android resource operation skills, summary of Android file operation skills, summary of Android SQLite database operation skills, summary of Android JSON format data operation skills Summary of Android database operation skills, introduction and advanced tutorial of Android development, summary of Android view skills and summary of Android control usage

I hope this article will help you in Android programming.

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