我有一个媒体播放器服务,可以在我的应用程序的后台播放音乐,例如:
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();
}
问题在于,当用户更改应用程序或进入手机的主屏幕(应用程序进入后台)时,音乐仍在播放。
我试图在onStop
和onDestroy
方法中阻止它,但当我改变我不想要的活动时,这会停止播放音乐(我希望音乐继续播放)当用户浏览活动时)。
更新
我试过广播:
我添加了
LocalBroadcastManager.getInstance(this).registerReceiver(StateReceiver, new IntentFilter("status"));
in onCreate of music Service和接收事件的方法:
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) {
}
但音乐不会恢复
答案 0 :(得分:3)
停止播放器后,您需要重新准备播放器。如果您想在应用程序进入后台时停止播放,并在应用程序进入前台时从头开始播放媒体。只需将您的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();
}
}
};
但是,如果您要暂停播放并从您离开的位置继续播放,请进行以下更改: 在您的Application类的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。
答案 1 :(得分:1)
您可以在应用程序类中添加它,以检查您的应用程序是否在前台。
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new MyLifecycleHandler());
}
}
将此添加到您的应用程序中:
public class app_class extends Application implements Application.ActivityLifecycleCallbacks {
private static int resumed;
private static int paused;
private static String currentActivity;
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(this);
}
@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) {
}
}
收到服务广播后,您可以停止媒体播放。
修改强>
您需要将应用程序类更改为:
public static ArrayList<String> ArrayofName;
答案 2 :(得分:0)
你实现了onDestroy()吗?如果没有,我相信这可能是解决方案 - 你停止你的播放器或你在onDestroy()中运行服务的任何东西。
可以通过调用stopSelf()方法或通过调用来停止服务 调用Context.stopService()。
答案 3 :(得分:0)
除了使用RoyalGriffin回答的广播接收器外,音乐中断很少的问题可以通过实现LifecycleObserver
接口来解决。您可以知道应用何时处于前台或后台,这样就无需处理每个活动的状态。
使用他的方法在您的应用中实施Application.ActivityLifecycleCallbacks
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
和@OnLifecycleEvent(Lifecycle.Event.ON_START)
然后将此行添加到onCreate()
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
您的app_class应该是这样的:
public class app_class extends Application implements LifecycleObserver,
Application.ActivityLifecycleCallbacks{
private static int resumed;
private static int paused;
private static String currentActivity;
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(this);
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
private void onAppBackgrounded() {
Log.d(TAG, "App in background");
send_status(0, this);
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
private void onAppForegrounded() {
Log.d(TAG, "App in foreground");
send_status(1, this);
}
@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) {
}
}
答案 4 :(得分:0)
可以使用LifecycleObserver来简化模板:
class App : Application(), LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onAppInBackground() {
sendStatus(0)
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onAppInForeground() {
sendStatus(1)
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onAppDestroyed() {
sendStatus(2)
}
private fun sendStatus(statusCounter: Int) {
val intent = Intent("status")
intent.putExtra("status", statusCounter)
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
}
override fun onCreate() {
super.onCreate()
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
}
}
添加到build.gradle(应用程序)
implementation "androidx.lifecycle:lifecycle-runtime:2.1.0"
implementation "androidx.lifecycle:lifecycle-extensions:2.1.0"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.1.0"