简体   繁体   English

在屏幕上异步播放音乐-Libgdx Java

[英]Play music asynchronously over screens - Libgdx Java

I want background music to be played over various screens of my game the music is initially started in the first screen class: 我希望背景音乐可以在游戏的各个屏幕上播放,而音乐最初是在第一屏幕类中开始播放的:

    boolean backgroundMusicPlaying = backgroundMusic.isPlaying();

    public MainMenuScreen(Game1 gam){
    (...)
    if(backgroundMusicPlaying != true){
                backgroundMusic.play();
                backgroundMusic.setVolume(0.3f);
                backgroundMusic.setLooping(true);
                backgroundMusicPlaying = true;
                }
   (...)
 }

Problem 问题

The problem is when i return to this class after I have been to a previous screen in the game it restarts the music but i don't want this i want it to be a continuous loop. 问题是当我去过游戏中的上一个屏幕后回到班级时,它会重新开始音乐,但是我不希望这样,我希望它成为连续循环。

An example of how the screen's are changed to and from this class/screen: 屏幕如何在此类/屏幕之间切换的示例:

game.setScreen(new playOptions(game));

Move the backgroundMusic.isPlaying() call inside the method? backgroundMusic.isPlaying()调用移到方法内部?

public MainMenuScreen(Game1 gam){
(...)
boolean backgroundMusicPlaying = backgroundMusic.isPlaying();
if(backgroundMusicPlaying != true){
            backgroundMusic.play();
            backgroundMusic.setVolume(0.3f);
            backgroundMusic.setLooping(true);
            backgroundMusicPlaying = true;
            }

(...) (......)

Looks like you're calling some non-UI code in your activity. 看起来您在活动中正在调用一些非UI代码。 So you need to wrap your music player in an AsyncTask. 因此,您需要将音乐播放器包装在AsyncTask中。 This way your player won't block UI and won't be tied to it. 这样,您的播放器将不会阻止UI,也不会与其绑定。 This should like something like the following code. 这应该类似于以下代码。

public class MusicPlayer extends AsyncTask<Void, Void, Void>
{
    public String filename;
    public boolean backgroundMusicPlaying;
    public ??? backgroundMusic;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //this method will be running on UI thread
    }
    @Override
    protected Void doInBackground(Void... params) {
        //this method will be running on background thread so don't update UI frome here
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        //this method will be running on UI thread
    }


    public void playMusic() {
        // stub
    }

    public void pauseMusic() {
        // stub
    }

    public void setVolume(float level) {
        // stub
    }

    // etc
}

Just implement some methods in order to control the MusicPlayer, or just wrap those of backgroundMusic. 只需实现一些方法即可控制MusicPlayer,或仅包装backgroundMusic的方法。 Or just let backgroundMusic's class extend the AsyncTask class. 或者只是让backgroundMusic的类扩展AsyncTask类。

Read http://developer.android.com/reference/android/os/AsyncTask.html for more information. 阅读http://developer.android.com/reference/android/os/AsyncTask.html了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM