Android媒体播放器在后台播放,但不要停止应用程序杀死

我是Android的新手,所以我有一个问题。在Android中,只要我的音乐播放器启动,即使活动从一个更改为另一个,我也想播放背景音乐。我试过这个代码:

MediaPlayer music = MediaPlayer.create(MainActivity.this, R.drawable.bgscore);
music.start();

但是,当用户关闭应用程序时,声音应该停止,而不会。我该如何实现?

最佳答案
创建一个单独的类来处理几个条件

import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;

public class MusicManager {
    static final int MUSIC_PREVIOUS = -1;
    private static final String TAG = "MusicManager";
    static MediaPlayer mp;
    private static int currentMusic = -1;
    private static int previousMusic = -1;


    public static void start(Context context, int music) {
        start(context, music, false);
    }

    public static void start(Context context, int music, boolean force) {
        if (!force && currentMusic > -1) {
// already playing some music and not forced to change
            return;
        }

        if (music == MUSIC_PREVIOUS) {
            Log.d(TAG, "Using previous music [" + previousMusic + "]");
            music = previousMusic;
        }
        if (currentMusic == music) {
// already playing this music
            return;
        }
        if (currentMusic != -1) {
            previousMusic = currentMusic;
            Log.d(TAG, "Previous music was [" + previousMusic + "]");
// playing some other music, pause it and change
            pause();
        }
        currentMusic = music;
        Log.d(TAG, "Current music is now [" + currentMusic + "]");
        if (mp != null) {
            if (!mp.isPlaying()) {
                mp.start();
            }
        } else {
            mp = MediaPlayer.create(context, R.raw.backGroundMusic); //Ur BackGround Music
        }

        if (mp == null) {
            Log.e(TAG, "player was not created successfully");
        } else {
            try {
                mp.setLooping(true);
                mp.start();
            } catch (Exception e) {
                Log.e(TAG, e.getMessage(), e);
            }
        }
    }

    public static void pause() {
        if (mp != null) {
            if (mp.isPlaying()) {
                mp.pause();
            }
        }

// previousMusic should always be something valid
        if (currentMusic != -1) {
            {
                previousMusic = currentMusic;
                Log.d(TAG, "Previous music was [" + previousMusic + "]");
            }
            currentMusic = -1;
            Log.d(TAG, "Current music is now [" + currentMusic + "]");
        }
    }

    public static void release() {
        Log.d(TAG, "Releasing media players");
        try {
            if (mp != null) {
                if (mp.isPlaying()) {
                    mp.stop();
                }
                mp.release();
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage(), e);
        }

        if (currentMusic != -1) {
            previousMusic = currentMusic;
            Log.d(TAG, "Previous music was [" + previousMusic + "]");
        }
        currentMusic = -1;
        Log.d(TAG, "Current music is now [" + currentMusic + "]");
    }
}

然后在你的MainActivity中定义一个全局布尔变量,并在onCreate()的setContentView(….)之前设置为true,即

    boolean continueBGMusic;
    ....
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        continueBGMusic=true;
    setContentView(R.layout.activity_main);
    .....
    }

然后更新onPause()as

    public void onPause()
        {
            super.onPause();
            if(!continueBGMusic)
                MusicManager.pause();
    }

和onResume()as

    public void onResume()
        {
            super.onResume();

                continueBGMusic=false;
                MusicManager.start(this,R.raw.backGroundMusic);
    }

使用布尔变量和两种方法更新所有ur三个活动。

转载注明原文:Android媒体播放器在后台播放,但不要停止应用程序杀死 - 代码日志