1

サービスを使用してバックグラウンドミュージックを再生しています。問題は、アクティビティが終了しても音楽が再生され続けることです。

これがサービスを開始するメインアクティビティからのコードです

            Intent svc=new Intent(HomeActivity.this, BackgroundSoundService.class);
    startService(svc);

BackgroundSoundService.java

         public class BackgroundSoundService extends Service {
             private static final String TAG = null;
             public static MediaPlayer player;
             public IBinder onBind(Intent arg0) {

                 return null;
             }
             @Override
             public void onCreate() {
                 super.onCreate();
                 Log.d("atMedia", "Backround Music playing");
                 player = MediaPlayer.create(this, R.raw.background);
                 player.setLooping(true); // Set looping
                 player.setVolume(100,100);

             }
             public int onStartCommand(Intent intent, int flags, int startId) {
                 player.start();
                 return 1;
             }

             public void onStart(Intent intent, int startId) {
                 // TO DO
             }
             public IBinder onUnBind(Intent arg0) {
                 // TO DO Auto-generated method
                 return null;
             }

             public void onStop() {

             }
             public void onPause() {

             }
             @Override
            public void onDestroy() {
                super.onDestroy();
                 player.stop();
                 player.release();
             }

             @Override
             public void onLowMemory() {

             }

}

4

2 に答える 2

3

サービス内でselfStop()メソッドを呼び出す必要があります。またはstopService()APIを使用します

于 2012-09-25T06:22:04.507 に答える
3

MainActivityからのサービスを停止してみてください。

Intent svc=new Intent(HomeActivity.this, BackgroundSoundService.class);     
stopService(svc);
于 2012-09-25T06:26:19.880 に答える