Android Study Notes (21)-use the service background to play mediaplayer music

Source: Internet
Author: User

1. Service Introduction

Official explanation:ServiceIs
An application component that can perform long-running operations in the background and does not provide a user interface. another application component can start a service and it will continue to run in the background even if the user switches to another

That is to say, a service is a component that can run in the background without providing a user interface. Other applications can start a service, and even switch to another application, it can run in the background.

2. Service Lifecycle

The lifecycle of the android service is not as complex as the activity. It only inherits the oncreate (), onstart (), and ondestroy () methods. When we start the service for the first time, the oncreate () and onstart () methods are called successively. when the service is stopped, the ondestroy () method is executed. Note that if the service has been started, when we start the service again, we will not execute the oncreate () method, but directly execute the onstart () method.

3. The following is an example of playing music:

First create the directory raw in res, and then add MP3 files to it.

Inherit a service class:

Package COM. moruna. studys; import android. app. service; import android. content. intent; import android. media. mediaplayer; import android. OS. ibinder; public class myservice extends Service {private mediaplayer MP; @ overridepublic void oncreate () {// todo auto-generated method stub // initialize the Music Resource try {system. out. println ("create player"); // create a mediaplayer object MP = new mediaplayer (); MP = mediaplayer. create (myservice. this, R. raw. alarm); // MP. prepare ();} catch (illegalstateexception e) {// todo auto-generated catch blocke. printstacktrace ();} super. oncreate () ;}@ overridepublic void onstart (intent, int startid) {// todo auto-generated method stub // start playing music MP. start (); // process the event after the music is played. MP. setoncompletionlistener (New mediaplayer. oncompletionlistener () {public void oncompletion (mediaplayer MP) {// todo auto-generated method stub // loop playback try {MP. start ();} catch (illegalstateexception e) {// todo auto-generated catch blocke. printstacktrace () ;}}); // An error occurred while playing the music. setonerrorlistener (New mediaplayer. onerrorlistener () {public Boolean onerror (mediaplayer MP, int what, int extra) {// todo auto-generated method stub // release the resource try {MP. release ();} catch (exception e) {e. printstacktrace () ;}return false ;}}); super. onstart (intent, startid) ;}@ overridepublic void ondestroy () {// todo auto-generated method stub // stop playing music and release resource MP when the service is stopped. stop (); MP. release (); super. ondestroy () ;}@ overridepublic ibinder onbind (intent) {// todo auto-generated method stubreturn NULL ;}}

Call myservice in activity to start music or close music. Even if the application is exited, if the service is not disabled, it will play back in the background until it is disabled.

Start music in the inheritance class alarm activity:

// Play the alert intent intentsv = new intent (alarm. This, myservice. Class); startservice (intentsv );

Disable music in the activity inheritance class alarm:

// Disable intent intentsv = new intent (alarm. This, myservice. Class); stopservice (intentsv );

In this way, the service can play the music to be played in the background. Of course, where you need to start or close the music depends on your own needs.

Note that do not forget to register:

Add the following content to the application in androidmanifest. xml:

<service android:name=".MyService" />

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.