This article introduces the MediaPlayer local music player. When the application is no longer on the frontend and is not using it, we need to establish a Service to ensure that the audio continues to play.
Interaction between Activity and Service binding is the focus of this article (here we need to note that Activity cannot directly access methods in Service objects, so we have introduced it here, this is also a consideration for service security and other aspects ).
Directly run the Code:
Layout file: activity_main:
MainActivity:
Package com. multimediademo6audioservice; import android. app. activity; import android. content. componentName; import android. content. context; import android. content. intent; import android. content. serviceConnection; import android. OS. bundle; import android. OS. IBinder; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class MainActivity extends Activity imple Ments OnClickListener {private Button startServiceButton, stopServiceButton, fastBackward, fastForward; private Intent intentService; private SimpleAudioService simpleAudioService; boolean flag = false; @ Overrideprotected void onCreate (Bundle savedInstanceState. onCreate (savedInstanceState); setContentView (R. layout. activity_main); init ();}/*** instantiate component */private void init () {startServiceButton = (Bu Tton) findViewById (R. id. startServiceButton); stopServiceButton = (Button) findViewById (R. id. stopServiceButton); fastBackward = (Button) findViewById (R. id. fastBackward); fastForward = (Button) findViewById (R. id. fastForward); startServiceButton. setOnClickListener (this); stopServiceButton. setOnClickListener (this); fastBackward. setOnClickListener (this); fastForward. setOnClickListener (this); intentService = ne W Intent (this, SimpleAudioService. class) ;}@ Overridepublic void onClick (View v) {switch (v. getId () {/***** enable */case R. id. startServiceButton: startService (intentService);/*** A bindService command is used to establish a connection with the service. The bindService command is used to name the object (* The ServiceConnection interface below) as serviceConnection. */BindService (intentService, serviceConnection, Context. BIND_AUTO_CREATE); flag = true; break;/*** close */case R. id. stopServiceButton: if (flag) {unbindService (serviceConnection); stopService (intentService); flag = false;} break;/*** fast return */case R. id. fastBackward: if (flag) {simpleAudioService. backwardFun ();} break;/*** fast forward */case R. id. fastForward: if (flag) {simpleAudioService. forwardFun ();} break; default: bre Ak;}/*** serviceConnection is a ServiceConnection object, which is an interface used to listen to the status of the bound service */private ServiceConnection serviceConnection = new ServiceConnection () {/*** click the Enable button to call the onServiceConnected method in the serviceConnection object. * Passing an IBinder object to this method * is actually created and submitted from the service itself. This IBinder object will be of the SimpleAudioServiceBinder type and we will create it in the service. * It will have a method used to return our service itself and become getService, so that we can directly perform operations on the objects returned by this method. * // @ Overridepublic void onServiceConnected (ComponentName, IBinder sasBinder) {simpleAudioService = (SimpleAudioService. simpleAudioServiceBinder) sasBinder ). getService ();}/*** this method is used to process the disconnection from the service. * // @ Overridepublic void onServiceDisconnected (ComponentName name) {simpleAudioService = null ;}};}
Enabled service class: SimpleAudioService:
Package com. multimediademo6audioservice; import android. app. service; import android. content. intent; import android. media. mediaPlayer; import android. media. mediaPlayer. onCompletionListener; import android. OS. binder; import android. OS. IBinder; import android. util. log;/*** enabled Service **/public class SimpleAudioService extends Service implements OnCompletionListener {private String TAG = "zhongyao"; private Media Player player; public class SimpleAudioServiceBinder extends Binder {SimpleAudioService getService () {return SimpleAudioService. this ;}} private final IBinder sasBinder = new SimpleAudioServiceBinder (); @ Overridepublic IBinder onBind (Intent intent) {return sasBinder ;} /*** when you click Enable in MainActivity and call startService to enable the Service, * the following two methods will be called: onCreate (), onStartCommand () */@ Overridepublic void onCreate () {super. onCreate (); Log. v (TAG, "onCreate"); player = MediaPlayer. create (this, R. raw. good); player. setOnCompletionListener (this) ;}@ Overridepublic int onStartCommand (Intent intent, int flags, int startId) {Log. d (TAG, "onStartCommand"); if (! Player. isPlaying () {player. start ();} return super. onStartCommand (intent, flags, startId);}/*** the onDestroy () method is called when stopService is stopped. * // @ Overridepublic void onDestroy () {super. onDestroy (); Log. d (TAG, "onDestroy"); if (player. isPlaying () {player. stop ();} player. release ();}/*** called after playback */@ Overridepublic void onCompletion (MediaPlayer mp) {stopSelf ();}/*** method in service: fast Return */public void backwardFun () {if (player. isPlaying () {player. seekTo (player. getCurrentPosition ()-2500);}/*** service method: Fast forward */public void forwardFun () {if (player. isPlaying () {player. seekTo (player. getCurrentPosition () + 2500 );}}}
Download source code:
Click to download source code