Android mediaplayer 单例服务不会停止播放

标签 android service singleton android-mediaplayer

我的所有 Activity 都需要有背景音乐。当应用程序不在前台时,它应该停止。因为我正在为 2.3 开发,所以我不能使用 ActivityLifeCycleCallBacks 类。我在 Checking if an Android application is running in the background 实现了解决方案然后决定让媒体播放器成为单例并在服务中使用它。

一切正常,如果我按主页,从菜单中选择退出,或者我让应用程序以任何方式进入背景,声音停止但是...... 在我做其他事情的某个随机时间之后,甚至当屏幕关闭,音乐会突然重新开始。即使我从任务管理器中终止应用程序,它也会在稍后再次启动。

这是我的第一个单例,也是我第一次玩服务,所以我想我错过了一些非常基本的东西。我想我正在关闭服务,但显然我没有。 这是代码:

播放音频.java

import ...
public class PlayAudio extends Service{
    private static final Intent Intent = null;
    MediaPlayer objPlayer;
    private int length = 0;
    boolean mIsPlayerRelease = true;
    private static PlayAudio uniqueIstance; //the singleton
    static PlayAudio mService;
    static boolean mBound = false; // boolean to check if the service containing this singleton is binded to some activity
    public static boolean activityVisible; // boolean to check if the activity using the player is foreground or not

//My attempt to make a singleton
public static PlayAudio getUniqueIstance(){
    if (uniqueIstance == null) {
        uniqueIstance = new PlayAudio();
    }
    return uniqueIstance;
}
public static boolean isActivityVisible() {
    return activityVisible;
  }  

  public static void activityResumed() {
    activityVisible = true;
  }

  public static void activityPaused() {
    activityVisible = false;
  }
static public ServiceConnection mConnection = new ServiceConnection() {// helper for the activity

    public void onServiceConnected(ComponentName className,
            IBinder service) {
        LocalBinder binder = (LocalBinder) service;
        mService = binder.getService();
        mBound = true;
    }

    public void onServiceDisconnected(ComponentName arg0) {
        mBound = false;
    }
};
public static Intent createIntent (Context context) { //helper for the activity using the player
    Intent intent = new Intent(context, PlayAudio.class);
    return intent;
}
private final IBinder mBinder = new LocalBinder();

public class LocalBinder extends Binder {
    PlayAudio getService() {
        // Return this instance so clients can call public methods
        return PlayAudio.this;
    }
}
@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}
public void onCreate(){
    super.onCreate();
    Log.d(LOGCAT, "Service Started!");
    objPlayer = MediaPlayer.create(this,R.raw.kickstarterreduced);
    objPlayer.setLooping(true);
    mIsPlayerRelease = false;
}

public int onStartCommand(Intent intent, int flags, int startId){
    objPlayer.start();
    Log.d(LOGCAT, "Media Player started!");
    if(objPlayer.isLooping() != true){
        Log.d(LOGCAT, "Problem in Playing Audio");
    }
    return 1;
}

public void onStop(){
    objPlayer.setLooping(false);
    objPlayer.stop();
    objPlayer.release();
    mIsPlayerRelease = true;
}

public void onPause(){
    if(objPlayer.isPlaying())
    {
        objPlayer.pause();
        length=objPlayer.getCurrentPosition(); // save the position in order to be able to resume from here
    }   
}
public void resumeMusic() // if length is 0 the player just start from zero
{   if (mIsPlayerRelease == true) {
    objPlayer = MediaPlayer.create(this,R.raw.kickstarterreduced);
    mIsPlayerRelease = false;
}
    if(objPlayer.isPlaying()==false )
    {
        if (length != 0) objPlayer.seekTo(length);
        objPlayer.start();
    }
}

}

这是我在每个 Activity 的类中实现的方法

SharedPreferences sharedPrefs;
PlayAudio playerIstanced;
public static boolean activityVisible;
@Override
public void onStart() {
    super.onStart();
    sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); 
   }
@Override
public void onResume() {
       super.onResume();
       playerIstanced= PlayAudio.getUniqueIstance(); //call singleton
       bindService(PlayAudio.createIntent(this), playerIstanced.mConnection, Context.BIND_AUTO_CREATE); // create the service
       if (sharedPrefs.getBoolean("sound", true) == true) {// if sound is enabled in option it will start the service
           startService(PlayAudio.createIntent(this));
           playerIstanced.mService.activityResumed();
           if (playerIstanced.mBound == true) {
           playerIstanced.mService.resumeMusic();
           }
       }

   }
   @Override
   public void onPause() {
       super.onPause();
       playerIstanced.mService.activityPaused();
       final Handler handler = new Handler();
       handler.postDelayed(new Runnable() {
         public void run() {
           //If the phone lags when changing activity (between onPause() and the other activity onResume() the music won't stop. If after 500ms onResume() is not called it means the activity went background...Am I messing with service here?
             if (playerIstanced.mService.isActivityVisible() != true) {
                 playerIstanced.mService.onPause();
             }
         }
       }, 500);

}
   @Override
   public void onStop(){
       super.onStop();
       // Unbind from the service
       if (playerIstanced.mService.mBound) {
           playerIstanced.mService.mBound = false;
           unbindService(playerIstanced.mService.mConnection);

       }
   }



}

最佳答案

当用户退出应用程序时自动停止音乐

这部分必须在每个 Activity 的 onPause 中:

public void onPause(){
    super.onPause();
        Context context = getApplicationContext();
                ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
                List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
                if (!taskInfo.isEmpty()) {
                  ComponentName topActivity = taskInfo.get(0).topActivity; 
                  if (!topActivity.getPackageName().equals(context.getPackageName())) {
                   StopPlayer();
                    Toast.makeText(xYourClassNamex.this, "YOU LEFT YOUR APP. MUSIC STOP", Toast.LENGTH_SHORT).show();
                  }
                }
      }

这部分必须在每个 Activity 的 onResume 中:

当用户恢复应用时自动播放音乐

Public void onResume()
    {
       super.onResume();
     StartPlayer();
    }

希望对你有帮助!! 你可以查看my answer根据这个主题,它可能会解决您的问题。

关于Android mediaplayer 单例服务不会停止播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18669541/

相关文章:

android - 使用底部导航栏在 Web View 中导航

java - RxJava - 合并两个 Observable

ios - 单例 : Pros, 缺点,设计问题

Java - 无限循环单例类方法调用

android - 访问连接被拒绝 new linkedin Android SDK

android - 文本到语音发音数字,如 "4th"、 "8ths"或 "2nd"

android - 我可以在后台使用 Android Wear 分析传感器数据吗?

android - 如何启动 android web 服务器服务?

python - Django 的后端处理

android - Kotlin 单例应用程序类