gpt4 book ai didi

Android 媒体播放器在后台播放,但在应用程序被终止时不会停止

转载 作者:IT王子 更新时间:2023-10-28 23:31:48 25 4
gpt4 key购买 nike

我是 Android 新手,所以我遇到了问题。在 Android 中,我想在我的音乐播放器启动后立即播放背景音乐并让它继续播放,即使 Activity 从一个变为另一个。我试过这段代码:

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 中定义一个全局 bool 变量,并在 onCreate() 中的 setContentView(....) 之前将其设置为 true

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

然后更新onPause()

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

onResume() 作为

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

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

使用 bool 变量和两个方法更新所有三个 Activity 。

关于Android 媒体播放器在后台播放,但在应用程序被终止时不会停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18871288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com