gpt4 book ai didi

java - 当应用进入后台时停止 MediaPlayer 服务

转载 作者:行者123 更新时间:2023-11-29 08:27:58 24 4
gpt4 key购买 nike

我有一个媒体播放器服务,可以在我的应用程序后台播放音乐,例如:

public class Music extends Service {
MediaPlayer player;

@Override
public IBinder onBind(Intent intent) {
return null;
}

public void onCreate() {
player = MediaPlayer.create(this, R.raw.music);
player.setLooping(true);
}

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


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

问题是,当用户更改应用程序或转到手机主屏幕(应用程序进入后台)时,音乐仍在播放。

我试图在 onStoponDestroy 方法中停止它,但是当我改变 Activity 时这会停止音乐,这是我不想要的(我想要在用户浏览 Activity 时继续播放音乐)。

更新

我试过广播:

我加了

LocalBroadcastManager.getInstance(this).registerReceiver(StateReceiver, new IntentFilter("status"));

在音乐服务的 onCreate 和接收事件的方法中:

private BroadcastReceiver StateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String status = intent.getStringExtra("status");
if (parseInt(String.valueOf(status)) == 0) {
player.stop();
}else{player.start();}

}
};

在 Application 类中我这样做了:

public class app_class extends Application implements Application.ActivityLifecycleCallbacks {
private static int resumed;
private static int paused;

private static String currentActivity;

@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
currentActivity = activity.getClass().getSimpleName();
}
public static String getCurrentActivity() {
return currentActivity;
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
send_status(1);
}

@Override
public void onActivityPaused(Activity activity) {
send_status(0);
}
private void send_status(int status_counter) {
Intent intent = new Intent("status");
intent.putExtra("status", String.valueOf(status_counter));
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
@Override
public void onActivityStopped(Activity activity) {

}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

}
@Override
public void onActivityDestroyed(Activity activity) {

}

但音乐不会恢复

最佳答案

您需要在停止后重新准备播放器。如果您想在您的应用程序进入后台时停止播放,并在您的应用程序进入前台时从头开始播放媒体。只需将您的 BroadcastReceiver 代码修改为:

private BroadcastReceiver StateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String status = intent.getStringExtra("status");
if (parseInt(String.valueOf(status)) == 0) {
player.stop();
} else {
player = MediaPlayer.create(this, R.raw.music);
player.setLooping(true);
player.start();
}

}
};

但是,如果您想暂停播放并从您离开的地方继续播放,请进行以下更改:在您的应用程序类中,在 onActivityDestroyed() 中:

@Override
public void onActivityDestroyed(Activity activity) {
send_status(2);
}

在 BroadcastReceiver 中:

private BroadcastReceiver StateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String status = intent.getStringExtra("status");
if (parseInt(String.valueOf(status)) == 0) {
player.pause(); //When app is in background and not killed, we just want to pause the player and not want to lose the current state.
} else if (parseInt(String.valueOf(status)) == 1) {
if (player != null)
player.start(); // If the player was created and wasn't stopped, it won't be null, and the playback will be resumed from where we left of.
else {
// If the player was stopped, we need to prepare it once again.
player = MediaPlayer.create(this, R.raw.music);
player.setLooping(true);
player.start();
}
} else if(player != null){
player.stop();
player.release();
}

}
};

另外,看看 MediaPlayer states .

关于java - 当应用进入后台时停止 MediaPlayer 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50838000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com