如何防止我的媒体播放器在我切换活动时停止而不是在我点击 HOME 时停止?

编程入门 行业动态 更新时间:2024-10-27 10:25:52
本文介绍了如何防止我的媒体播放器在我切换活动时停止而不是在我点击 HOME 时停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

I have an application that plays music. (Also you can close on options menu.)

但问题是当你按下home键时,音乐一直在播放,我想在用户按下home键时停止音乐.

But the problem is that when you press the home button, the music keeps playing, I want to stop the music when the user presses the HOME Button.

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

I tried to put onPause() and onStop() method to music.stop(). But it fails because when I start a new activity, the music also stops but I don't want it to, only when the Home button is pressed.

我的代码是:

@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();

}

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

Here The OPTIONS ACTIVITY where the music starts to play, But when I pass the menu activity, the music also stops.

推荐答案

我可以建议两种方法

1) 让它在 onPause() 中,创建一个 flag/boolean,默认设置为 false 并且将 true 在任何情况下,例如新活动启动、后按等.因此,如果调用 onPause 并且 flag 为 false,您可以停止音乐.

1) let it be in onPause(), create a flag/boolean that will set to false by default and will be true in every-case such as new activity startup, on back press etc. so if onPause is called and flag is false you can stop the music.

2)你已经有后台service,你可以继续检查哪个活动在前台,如果是homeScreen你可以停止音乐.

2) you have background service already, you can keep checking which activity is in foreground, if is homeScreen you can stop the music.

- 了解您的应用程序是否不在前台.正如布赖恩在另一个答案中所建议的那样.请参阅​​在这里您可以找到应用程序是前台还是后台并采取相应措施

Edit :- to know if your app is not in the foreground. as suggested by Brian in another answer. see here you can find if app is foreground or in background and take action accordingly

这篇关于如何防止我的媒体播放器在我切换活动时停止而不是在我点击 HOME 时停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-23 03:55:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034903.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:媒体播放器   是在   在我   而不   如何防止

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!