安卓:媒体播放器播放多次,如果我继续pressing后退按钮,并重新启动应用程序媒体播放器、重新启动、应用程序、按钮

2023-09-12 06:03:59 作者:誓言丿蠱惑人心

当我打我的歌和preSS的后退按钮返回,返回家乡,音乐继续播放。当我再次启动应用程序,音乐播放两次。我认为这是onResume方法,因为我注释掉的方法和问题停了下来。我如何获得onResume正常工作?我尝试使用,如果(backgroundMusic.isplaying())的onResume内,但应用程序崩溃时,我从另一个活动恢复。我究竟做错了什么?

  //全球媒体播放器
MediaPlayer的backgroundMusic;

/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    loadBackgroundMusic();

}

私人无效loadBackgroundMusic(){

    //将MP3转换成目标并启动它
    backgroundMusic = MediaPlayer.create(这一点,R.raw.backgrounmusic);
    backgroundMusic.setLooping(真正的);
    backgroundMusic.start();

}
        @覆盖
        保护无效的onPause(){
            super.onPause();
            backgroundMusic.release();

        }

        @覆盖
        保护无效onResume(){
            super.onResume();
            loadBackgroundMusic();

        }

        @覆盖
        保护无效的onStop(){
            super.onStop();
            backgroundMusic.release();
        }
 

解决方案

我真的不知道你想要的行为。在我看来,这是一个二:

音乐应该是只打了,而我的活动是可见的。

Photoshop做水晶透明媒体播放器教程

如果是这种情况,你应该仔细看的文档中的活动生命周期的。这将为例子告诉大家, onResume()也将被称为创建活动的第一次。

所以,解决办法是启动 onResume音乐(),并停止对的onPause()或类似

在开始我的活动一次,音乐应该是,即使我回到主屏幕打出

在这种情况下,你真的想要一个服务。

When I play my song and press the back button to return to return to home, the music continues playing. When I launch the app again, the music plays twice. I think it's the onResume method because I commented out the method and the problem stopped. How do I get onResume work properly? I tried using if(backgroundMusic.isplaying()) inside the onResume, but the app crashes when I resume from another activity. What am I doing wrong?

//global mediaplayer
MediaPlayer backgroundMusic;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);  
    loadBackgroundMusic();

}

private void loadBackgroundMusic() {

    //load mp3 into object and start it
    backgroundMusic = MediaPlayer.create(this,R.raw.backgrounmusic);
    backgroundMusic.setLooping(true);
    backgroundMusic.start();

}
        @Override
        protected void onPause() {
            super.onPause();
            backgroundMusic.release();

        }

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

        }

        @Override
        protected void onStop() {
            super.onStop();
            backgroundMusic.release();
        }

解决方案

I'm not really sure about the behavior you want. As I see it, it's one of two:

Music should be played only while my Activity is visible

If this is the case, you should look closer on the documentation for the Activity Lifecycle. This will for example tell you that onResume() will also be called the first time the Activity is created.

So the solution would be to start the music in onResume() and stop on onPause() or similar.

After starting my activity once, music should be played even if i return to home screen

In this case you really want a Service.