你如何在Android Studio上播放音乐?使用或不使用数组

时间:2017-02-24 20:41:45

标签: android-studio libgdx

我需要帮助制作歌曲Shuffle(混音)但我不知道如何以及我希望它每次都能转到另一种音乐而不重复。

public class MusicMix {

    private Music music1, music2, music3, music4, music5,music6,music7,music8,music9,music10,music11,music12,music13,music14,music15,music16,music17;

    music1 = Gdx.audio.newMusic(Gdx.files.internal("musicA.mp3"));
    music2 = Gdx.audio.newMusic(Gdx.files.internal("musicB.mp3"));
    music3 = Gdx.audio.newMusic(Gdx.files.internal("musicC.mp3"));
    music4 = Gdx.audio.newMusic(Gdx.files.internal("musicD.mp3"));
    music5 = Gdx.audio.newMusic(Gdx.files.internal("musicE.mp3"));
    music6 = Gdx.audio.newMusic(Gdx.files.internal("musicF.mp3"));
    music7 = Gdx.audio.newMusic(Gdx.files.internal("musicG.mp3"));
    music8 = Gdx.audio.newMusic(Gdx.files.internal("musicH.mp3"));
    music9 = Gdx.audio.newMusic(Gdx.files.internal("musicJ.mp3"));
    music10 = Gdx.audio.newMusic(Gdx.files.internal("musicK.mp3"));
    music11 = Gdx.audio.newMusic(Gdx.files.internal("musicL.mp3"));
    music12 = Gdx.audio.newMusic(Gdx.files.internal("musicM.mp3"));
    music13 = Gdx.audio.newMusic(Gdx.files.internal("musicN.mp3"));
    music14 = Gdx.audio.newMusic(Gdx.files.internal("musicO.mp3"));
    music15 = Gdx.audio.newMusic(Gdx.files.internal("musicP.mp3"));
    music16 = Gdx.audio.newMusic(Gdx.files.internal("musicQ.mp3"));
    music17 = Gdx.audio.newMusic(Gdx.files.internal("musicR.mp3"));
 }

2 个答案:

答案 0 :(得分:1)

首先创建一个应该让您的生活更轻松的数组。而不是一个一个地手动完成。

Array<Music> musics = new Array<Music>();
        for (int i = 65; i < 25; i++) {
            //The ascii of 65 = A
            char ascii = (char) i;
            Music music = Gdx.audio.newMusic(Gdx.files.internal("music" + ascii + ".mp3"));
            musics.add(music);
        }

然后在那之后调用这个方法。它是Libgdx中的内置方法。这种方法应该回答你的问题。

        musics.shuffle();

答案 1 :(得分:0)

我试图以这种方式实现您的要求。

public class TestGame extends Game {

    private Array<String> musicName;
    private Array<Music> musicList;
    private IntArray intArray;

    @Override
    public void create() {

        musicName=new Array<String>(new String[]{"sound/x1.ogg","sound/x2.ogg","sound/x3.ogg","sound/x4.ogg","sound/x5.ogg"});
        musicList=new Array<Music>();
        intArray=new IntArray();

        for (String path:musicName) {

            Music music=Gdx.audio.newMusic(Gdx.files.internal(path));
            musicList.add(music);
            intArray.add(musicName.indexOf(path,true));
            music.setOnCompletionListener(new OnComplete(this));
        }

        int value=MathUtils.random(musicList.size-1);
        intArray.removeValue(value);
        musicList.get(value).play();

    }

    public class OnComplete implements Music.OnCompletionListener {
        private TestGame testGame;
        public OnComplete(TestGame game){
            testGame=game;
        }

        @Override
        public void onCompletion(Music music) {
            testGame.playUnPlayedMusic();
        }
    }

    public void playUnPlayedMusic(){

        int value ;
        if(intArray.size>0) {
            value = intArray.get(MathUtils.random(intArray.size - 1));
            intArray.removeValue(value);

        }else {
            for (int i=0;i<musicList.size;i++)
                intArray.add(i);

            value = intArray.get(MathUtils.random(intArray.size - 1));
            intArray.removeValue(value);
        }

        Music music= musicList.get(value);
        music.play();
    }

    @Override
    public void render() {

    }
}