- audioService.haveFun();
- }
- }
- }
2、Service类
- package demo.camera;
- import android.app.Service;
- import android.content.Intent;
- import android.media.MediaPlayer;
- import android.os.Binder;
- import android.os.IBinder;
- import android.widget.MediaController.MediaPlayerControl;
-
-
-
-
-
-
- public class AudioService extends Service implements MediaPlayer.OnCompletionListener{
-
- MediaPlayer player;
-
- private final IBinder binder = new AudioBinder();
- @Override
- public IBinder onBind(Intent arg0) {
-
- return binder;
- }
-
-
-
- @Override
- public void onCompletion(MediaPlayer player) {
-
- stopSelf();
- }
-
-
- public void onCreate(){
- super.onCreate();
-
- player = MediaPlayer.create(this, R.raw.tt);
- player.setOnCompletionListener(this);
- }
-
-
-
-
- public int onStartCommand(Intent intent, int flags, int startId){
- if(!player.isPlaying()){
- player.start();
- }
- return START_STICKY;
- }
-
- public void onDestroy(){
-
- if(player.isPlaying()){
- player.stop();
- }
- player.release();
- }
-
-
- class AudioBinder extends Binder{
-
-
- AudioService getService(){
- return AudioService.this;
- }
- }
-
-
- public void haveFun(){
- if(player.isPlaying() && player.getCurrentPosition()>2500){
- player.seekTo(player.getCurrentPosition()-2500);
- }
- }
- }
3、在清单文件AndroidManifest.xml中配置Service
<service
android:name=".AudioService" />
|