当我们杀死(滑动)最近的应用程序时,音乐播放器如何工作

时间:2016-03-28 12:43:12

标签: android

当我播放音乐并回家时,音乐正在运行,但是当我杀死应用时,音乐会重新启动。我想杀死应用程序而不是重新启动时运行音乐。 请帮我。 我的代码是:---- MainActivity.java

public class MainActivity extends ActionBarActivity {

Context context;
private Button mPlay, mStop;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mPlay = (Button) findViewById(R.id.button1);
    mStop = (Button) findViewById(R.id.button2);
    mPlay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent service = new Intent(MainActivity.this, Myservice.class);
            startService(service);
        }
    });
    mStop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent name = new Intent(MainActivity.this, Myservice.class);
            stopService(name);
        }
    });
}

}

我的服务类是:---

public class Myservice extends Service implements
MediaPlayer.OnErrorListener{

private final IBinder mBinder = new ServiceBinder(); 
    MediaPlayer mPlayer;
    private int length = 0;

    public Myservice() { } 

    public class ServiceBinder extends Binder {
        Myservice getService()
         {
            return Myservice.this;
         }
    }

    @Override
    public IBinder onBind(Intent arg0){return mBinder;}

    @Override
    public void onCreate (){
      super.onCreate(); 

       mPlayer = MediaPlayer.create(this, R.raw.follow);
       mPlayer.setOnErrorListener(this);

       if(mPlayer!= null)
        {
            mPlayer.setLooping(true);
            mPlayer.setVolume(100,100);
        }


        mPlayer.setOnErrorListener(new OnErrorListener() {

      public boolean onError(MediaPlayer mp, int what, int       
          extra){

            onError(mPlayer, what, extra);
            return true;
        }
          });
    }


    @Override
    public int onStartCommand (Intent intent, int flags, int startId)

    {
         mPlayer.start();
         return START_STICKY;

    }

    public void pauseMusic()
    {
        if(mPlayer.isPlaying())
        {
            mPlayer.pause();
            length=mPlayer.getCurrentPosition(); 

        }
    }

    public void resumeMusic()
    {
        if(mPlayer.isPlaying()==false)
        {
            mPlayer.seekTo(length);
            mPlayer.start();
        }
    }

    public void stopMusic()
    {
        mPlayer.stop();
        mPlayer.release();
        mPlayer = null;
    }


    @Override
    public void onDestroy ()

    {
        super.onDestroy();
        if(mPlayer != null)
        {
        try{
         /*mPlayer.stop();
         mPlayer.release();*/
            Toast.makeText(getApplicationContext(), "ondestroy", 2000).show();
            }finally {
                mPlayer = null;
            }
        }

    }

    public boolean onError(MediaPlayer mp, int what, int extra) {

        Toast.makeText(this, "music player failed", Toast.LENGTH_SHORT).show(); 
        if(mPlayer != null)
        {
            try{
                mPlayer.stop();
                mPlayer.release();
            }finally {
                mPlayer = null;
            }
        }
        return false;
    }

}

1 个答案:

答案 0 :(得分:0)

在您的服务中添加以下方法,并在媒体开始播放时调用该方法。前台服务是高优先级服务,不经常被android系统杀死,启动前台服务需要通知。您可以通过调用stopForeground(true)手动停止前台服务。请关注媒体播放State of Media Playback (Android Dev Summit 2015)

的基础知识
private static final String NOTIFICATION_ID = 91;

    private void runAsForeground(){ 
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent=PendingIntent.getActivity(this, 0,
                notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

        Notification notification=new NotificationCompat.Builder(this)
                                    .setSmallIcon(R.drawable.ic_launcher)
                                    .setContentText("Media is playing")
                                    .setContentIntent(pendingIntent).build();

        startForeground(NOTIFICATION_ID, notification);

    }