내 모든 활동에서 배경 음악이 필요합니다. 응용 프로그램이 포 그라운드가 아닌 경우 중지되어야합니다. 2.3 용으로 개발할 때 ActivityLifeCycleCallBacks 클래스를 사용할 수 없습니다. Checking if an Android application is running in the background에서 솔루션을 구현 한 다음 미디어 플 레이어를 싱글 톤으로 만들어 서비스에 사용하기로 결정했습니다.Android mediaplayer 싱글 톤 서비스가 중지되지 않습니다.
모든 것이 잘 작동하고 집에서 누르면 메뉴에서 종료를 선택하거나 응용 프로그램이 소리가 멈추는 어떤 방식 으로든 돌아가도록합니다 ... 임의의 시간이 지나면 내가 다른 것을 할 때 또는 심지어 때 화면이 꺼지면 음악이 파란색에서 다시 시작됩니다. 작업 관리자에서 응용 프로그램을 종료해도 나중에 다시 시작됩니다.
이것은 내 첫 싱글턴이며 처음으로 서비스를 즐기기 때문에 나는 정말 기본적인 것을 놓치고 있다고 생각합니다. 나는 내가 서비스를 닫고있다라고 생각한다. 그러나 명백하게 나는 그렇지 않다. 여기 코드입니다 :
PlayAudio.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();
}
}
}
그리고 이것은 내가 모든 활동의 클래스에서 구현 한 방법이다
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);
}
}
}
해 주셔서 감사합니다. 여전히 코드가하는 일을 제대로 이해할 수는 없지만 제대로 작동합니다. StopPlayer() 대신에 작업을 일시 중지하고 onStop()을 호출 할 때만 중지하기로했습니다. 몇 번이라도 한 라인 씩 솔루션을 설명하겠습니까? 나는 약간의 인터넷 검색을 시도했다. 그러나 나는 아직도 길을 잃는다. – pinolo
@ pinolo, 여기는 front activity를 찾고 있습니다. 내 앱 화면이 전면에 있지 않으면 내 앱에서 사용자가 퇴장했음을 의미합니다. 나는 이것을 ActivityManager에서 얻을 수있다. 실행 작업 목록을 줄 것이다. 나는'taskInfo.get (0) .topActivity'에서 앞쪽 Activity를 얻을 수있다. 그럼 난 그냥 내 애플 리케이션의 활동과 비교. 일치하지 않는 경우 응용 프로그램이 닫힙니다. 당신이 그것을 얻을 희망! –