如何防止媒体播放器在我切换活动时停止,但在按 HOME 时不停止?

2023-12-20

我有一个播放音乐的应用程序。 (您也可以关闭选项菜单。)

但问题是,当您按下主页按钮时,音乐会继续播放,我想在用户按下主页按钮时停止音乐。

我尝试将 onPause() 和 onStop() 方法放在 music.stop() 中。但它失败了,因为当我开始一项新活动时,音乐也会停止,但我不希望它停止,只有当按下主页按钮时才会停止。

我的代码是:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.options);
     it=(RadioButton)findViewById(R.id.radio0);
     ti=(RadioButton)findViewById(R.id.radio1);
     but=(ToggleButton)findViewById(R.id.toggleButton1);
     music=MediaPlayer.create(Options.this, R.raw.avril);
   mainmenu=new Intent("com.KAYA.ingilizce_gelistirme.BASLANGIC");


    but.setOnClickListener(new View.OnClickListener()   {

        @Override
        public void onClick(View v) {
            if(but.isChecked()) {
                if(!music.isPlaying())
            music.start();
            }
            else
            if(music.isPlaying())
            music.pause();

        }
    });
     it.setChecked(true);
     Button menu=(Button)findViewById(R.id.button1);
     menu.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            startActivity(mainmenu);
        }
    });

}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub


    startActivity(new Intent(mainmenu));
    }


@Override
protected void onPause() {

    if(music.isPlaying()){
    music.pause();
    music.release();
    }
    but.setChecked(false);

    super.onPause();

}



@Override
protected void onResume() {
    super.onResume();

}

这里是音乐开始播放的选项活动,但是当我通过菜单活动时,音乐也停止了。


您需要启动单独的服务来播放音乐。

method 1

此方法对于大型应用程序具有更好的扩展性。

在您的音乐服务中定期调用以下类(例如,每 3 秒一次),以查看您的应用程序是否在后台。信用:检查android应用程序是否在前台? https://stackoverflow.com/questions/8489993/check-android-application-is-in-foreground-or-not

class ForegroundCheckTask extends AsyncTask<Context, Void, Boolean> {
    @Override
    protected Boolean doInBackground(Context... params) {
        final Context context = params[0];
        return isAppOnForeground(context);
    }

    private boolean isAppOnForeground(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        if (appProcesses == null) {
            return false;
        }
        final String packageName = context.getPackageName();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
                return true;
            }
        }
        return false;
    }
}

method 2

接下来,当您检测到您的应用程序已进入后台时,您需要关闭您的服务。看这里:检查 Android 应用程序是否在后台运行 https://stackoverflow.com/questions/3667022/android-is-application-running-in-background/5862048#5862048。请注意,您不想在每个类中手动重写 onPause 和 onResume。您希望尽可能使用共同的祖先。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何防止媒体播放器在我切换活动时停止,但在按 HOME 时不停止? 的相关文章

随机推荐