1, this example use the service to achieve a simple music playback function, the following is. (Click Start Play to open the service, music playback, click "Stop playing" to turn off the service, the music stops playing.) )
2. Core code:Musicservice.java:
Package Thonlon.example.cn.servicedemo;
import Android.app.Service;
import android.content.Intent;
import Android.media.MediaPlayer;
import Android.net.Uri;
import Android.os.IBinder;
import java.io.IOException;
/**
* Called when the service is bound
*/
Public class Musicservice extends Service {
private MediaPlayer MediaPlayer;
@Override
Public IBinder Onbind (Intent Intent) {
return null;
}
/**
* service is called after it is created
*/
@Override
Public void OnCreate () {
// play music on local resources
//MediaPlayer = Mediaplayer.create (this, R.RAW.SNH);
// play Music for file system
//File File = new file (Environment.getexternalstoragedirectory (), "Zshyn.mp3");
//MediaPlayer = new MediaPlayer ();
//try {
//Mediaplayer.setdatasource (File.getabsolutepath ()); //Set the playback source by obtaining an absolute path
//} catch (IOException e) {
//E.printstacktrace ();
//Play Music in the network
MediaPlayer = new MediaPlayer ();
uri uri = uri.parse ("Http://www.nxl123.cn/snh.mp3");
try {
Mediaplayer.setdatasource (this, URI); //or Direct Mediaplayer.setdatasource ("Http://www.nxl123.cn/snh.mp3");
} catch (IOException e) {
e.printstacktrace ();
}
}
/**
* Service is started after call
*/
@Override
public int Onstartcommand (Intent Intent, int flags, int startid) {
Mediaplayer.prepareasync (); // prepare the resource, which may block the main thread, so it is called on the child thread. You can use the Prepareasync function here.
//Prevent resources from playing music before they are ready, so set up a resource listener to prepare
//Indicates that the Onprepare method in the listener is called when the resource is ready to complete
Mediaplayer.setonpreparedlistener (New Mediaplayer.onpreparedlistener () {
@Override
Public void Onprepared (MediaPlayer MP) {
Mediaplayer.start (); //start playing music
}
});
//mediaplayer instances are created by the Create method, you do not need to call prepare () again before starting playback for the first time, because the Create method has already been called.
return Super.onstartcommand (Intent, flags, startid);
}
/**
* service is stopped after call
*/
@Override
Public void OnDestroy () {
mediaplayer.stop (); //Stop playing
mediaplayer.release (); //release prepared resources, and if you still need to use this object, you can first do not destroy
}
}
Mainactivity.java:
Package Thonlon.example.cn.servicedemo;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import Android.os.Bundle;
import Android.view.View;
Public class Mainactivity extends Appcompatactivity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
}
Public void OnClick (View v) {
Intent Intent = new Intent ();
Intent.setclass (this, musicservice.class);
switch (V.getid ()) {
Case R.id.btn_start_service:
StartService (intent);
Break ;
Case R.id.btn_stop_service:
StopService (intent);
Break ;
}
}
}
Using service components to implement a simple music player function--android basics