Layout file
Three buttons are set up in the layout file to play, pause, and resume playback
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"><ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Start Playback"Android:onclick= "Play"/><ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Pause playback"Android:onclick= "Pause"/><ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Continue playing"Android:onclick= "Continueplay"/></LinearLayout>
Activity
Start the service in activity, implement the MP3 playback function in the service, and then expose the function to activity through a binder object. You can invoke the methods in the service in the activity.
PackageXidian.dy.com.chujia;ImportAndroid.content.ComponentName;Importandroid.content.Intent;Importandroid.content.ServiceConnection;ImportAndroid.os.Bundle;ImportAndroid.os.IBinder;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View; Public classMainactivityextendsappcompatactivity {IMusic music; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); Intent Intent=NewIntent ( This, Musicservice.class); StartService (Intent); Bindservice (Intent,Newmyconnection (), bind_auto_create); } Public voidPlay (View v) {music.play (); } Public voidPause (View v) {music.pause (); } Public voidContinueplay (View v) {music.continueplay (); } classMyConnectionImplementsserviceconnection{@Override Public voidonserviceconnected (componentname name, IBinder service) {music=(IMusic) service; } @Override Public voidonservicedisconnected (componentname name) {} }}Service
Create a MediaPlayer in the service to control music playback
PackageXidian.dy.com.chujia;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.media.MediaPlayer;ImportAndroid.os.Binder;ImportAndroid.os.IBinder;Importandroid.support.annotation.Nullable;Importjava.io.IOException;/*** Created by Dy on 2016/7/29.*/ Public classMusicserviceextendsService {MediaPlayer player; @Override Public voidOnCreate () {player=NewMediaPlayer (); Super. OnCreate (); } @Nullable @Override Publicibinder onbind (Intent Intent) {return NewMybinder (); } Public voidContinueplay () {Player.start (); } Public voidpause () {player.pause (); } Public voidPlay () {player.reset (); Try{Player.setdatasource ("/sdcard/bc.mp3"); Player.prepare (); Player.start (); } Catch(IOException e) {e.printstacktrace (); }} @Override Public voidOnDestroy () {Super. OnDestroy (); Player.stop (); Player.release (); } classMybinderextendsBinderImplementsimusic{@Override Public voidPlay () {Musicservice. This. Play (); } @Override Public voidpause () {Musicservice. This. Pause (); } @Override Public voidContinueplay () {musicservice. This. Continueplay (); } }}IMusic
Imusic is an interface class, primarily to enable the invocation of methods in the service in the activity.
Package Xidian.dy.com.chujia; /** */Publicinterface IMusic {void play () ; void pause (); void continueplay ();}
Manifest file
The service needs to be registered in the manifest file
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Xidian.dy.com.chujia"><ApplicationAndroid:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:supportsrtl= "true"Android:theme= "@style/apptheme"> <ActivityAndroid:name=". Mainactivity "Android:label= "Main interface"> <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity> <ServiceAndroid:name=". Musicservice "/></Application></Manifest>
Android MP3 player