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.