An example of the background playback function of the Android music based on service _android

Source: Internet
Author: User
Tags sqlite database stub

This article is an example of how Android is based on the service to implement music background playback. Share to everyone for your reference, specific as follows:

Service is a long life cycle and there is no user interface program, when the program in each activity switch, we can use the service to achieve background music playback, even when the program quit to the background, the music is still playing. Here we give a concrete example of the implementation:

Of course, first add a MP3 song to the Resource folder:

To achieve music playback, you need to place two buttons in the interface to control the playback and stopping of music, by using StartService and StopService to achieve these two features:

Modify the Servicedemoavtivity.java code under SRC to add code for the following button event:

Button start = (Button) Findviewbyid (R.id.start);
Button stop = (button) Findviewbyid (r.id.stop);
Button.onclicklistener listener = new Button.onclicklistener () {
  @Override public
  void OnClick (View v) {
    // TODO auto-generated Method Stub
    Intent Intent = new Intent (Getapplicationcontext (), musicservice.class);
    Switch (V.getid ()) {case
      r.id.start:startservice (intent);
      Case R.id.stop:stopservice (intent), break;
    }
  }
;
Start.setonclicklistener (listener);
Stop.setonclicklistener (listener);

Here is the more important part of the service. Create a musicservice that inherits from the service and then controls playback of the music through the start and stop methods. Here is the key code in Musicservice.java:

public void OnStart (Intent Intent, int startid) {
    //TODO auto-generated method Stub
    Super.onstart (Intent, Starti d);
    Toast.maketext (This, "OnStart", Toast.length_long). Show ();
    Player = Mediaplayer.create (this, r.raw.eason);
    Player.setlooping (true);
    Player.start ();
}
public void OnDestroy () {
    //TODO auto-generated method Stub
    Super.ondestroy ();
    Toast.maketext (This, "OnDestroy", Toast.length_long). Show ();
    Player.stop ();
}

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

<application
    android:icon= "@drawable/ic_launcher"
    android:label= "@string/app_name" >
    < Activity
      android:name= ". Servicedemoactivity "
      android:label=" @string/app_name ">
      <intent-filter>
        <action android: Name= "Android.intent.action.MAIN"/>
        <category android:name= "Android.intent.category.LAUNCHER"/>
      </intent-filter>
    </activity>
    <service android:name= "Musicservice"/>
</ Application>

The entire example is constructed and deployed to the emulator or mobile phone to enable background playback.

More interested readers of Android-related content can view the site: "Summary of the usage tips for Android service components", "Summary of operational skills for Android programming", "Android Resource Operation skills Summary", " Android File Operation Tips Summary, "Android operation SQLite Database Skills Summary", "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android Development introduction and Advanced Course", " Android View tips Summary and a summary of the use of Android controls

I hope this article will help you with the Android program.

Related Article

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: info-contact@alibabacloud.com 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.