Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 暂停不同活动上的主要背景音乐_Android_Android Activity_Media Player - Fatal编程技术网

Android 暂停不同活动上的主要背景音乐

Android 暂停不同活动上的主要背景音乐,android,android-activity,media-player,Android,Android Activity,Media Player,所以我正在开发一款android游戏。我的主要活动或介绍活动有游戏的背景音乐。总的来说,这是整个游戏的主要背景音乐。所以我让音乐继续播放: private void startBgSound() { // TODO Auto-generated method stub //int soundFile = R.raw.backgroundmusic; //AssetFileDescriptor afd = getResources().openRawResourceFd(

所以我正在开发一款android游戏。我的主要活动或介绍活动有游戏的背景音乐。总的来说,这是整个游戏的主要背景音乐。所以我让音乐继续播放:

 private void startBgSound() {
    // TODO Auto-generated method stub
    //int soundFile = R.raw.backgroundmusic;
    //AssetFileDescriptor afd = getResources().openRawResourceFd(soundFile);

    bgsound = new MediaPlayer();
    bgsound.reset();
    bgsound = MediaPlayer.create(this, R.raw.backgroundmusic);

    bgsound.setLooping(true);
    bgsound.setVolume(100, 100);
    bgsound.start();
 } 
通过将循环设置为true。当用户应该单击“播放”按钮(仍在主活动上)时,下一个活动位于主活动的顶部。但是在我的第二个活动中,我在那里放了一个暂停按钮,所以当用户点击这个按钮时,背景音乐也应该暂停


你有什么聪明的办法来做那件事吗?我被这个问题缠住了。如果你能帮我,我将不胜感激。谢谢。

在我的游戏中,我使用静态类处理音乐。只要在开始播放音乐时提供上下文,就可以在代码中的任何位置停止音乐

只要确保在用户离开或打开游戏时重置类,因为即使游戏已关闭,静态类也可能继续存在

下面是一个例子:

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

public class SoundHandler {
    private static MediaPlayer backgroundMusic;
    private static Context context;

    private static boolean isMuted = false;

    public static void setContext(Context cont){
        context = cont;
    }

    public static void playMusic(int resource){
        if(backgroundMusic != null) backgroundMusic.reset();

        try{
            backgroundMusic = MediaPlayer.create(context, resource);

            backgroundMusic.setLooping(true);
            backgroundMusic.setVolume(100, 100);

            if(!isMuted){
                backgroundMusic.start();
            }
        } catch (NullPointerException e){
            //Creating MediaPlayer failed. This happens randomly without any clear reasons.
            e.printStackTrace();
        }
    }

    public static void setMuted(boolean muted){
        if(backgroundMusic != null){
            if(muted){
                if(backgroundMusic.isPlaying()){
                    backgroundMusic.stop();
                    isMuted = true;
                }
            } else {
                if(!backgroundMusic.isPlaying()){
                    backgroundMusic.start();
                    isMuted = false;
                }
            }
        }
    }

    public static void quit(){
        if(backgroundMusic != null){
            backgroundMusic.release();
        }
    }
}

记得在onCreate()中设置上下文,并在onDestroy()中运行quit()函数。

您好,谢谢,您能给我一段代码让我开始吗?谢谢,我很快会给我的答案加上一条:)补充道。这是我自己的类的一个经过严格修改的版本,我还没有测试过它是否有错误,但你至少应该从中了解到这一点。嗨,非常基本的问题,你如何设置上下文?很抱歉,我是Android的新手,在使用该类播放任何歌曲之前,请将SoundHandler.setContext(此)放在onCreate()中。