Added music code

This commit is contained in:
Willem Cazander 2022-01-28 11:59:57 +01:00
parent da3a2edd90
commit 85f0a34e20
4 changed files with 255 additions and 0 deletions

View file

@ -0,0 +1,131 @@
package love.distributedrebirth.demo4d.music;
import java.util.ArrayList;
import java.util.List;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Music.OnCompletionListener;
import com.badlogic.gdx.files.FileHandle;
public class MusicManager {
private final MusicSong introSong;
private final MusicSong creditsSong;
private final List<MusicSong> backgroundSongs;
private final NextSongListener nextSongListener;
private MusicSong currentSong = null;
public MusicManager() {
backgroundSongs = new ArrayList<>();
introSong = new MusicSong(Gdx.audio.newMusic(Gdx.files.internal("music/panoramacircle-waterfowl.mp3")),"panoramacircle-waterfowl");
creditsSong = new MusicSong(Gdx.audio.newMusic(Gdx.files.internal("music/idtech-doom-sigil.mp3")), "idtech-doom-sigil");
nextSongListener = new NextSongListener();
}
public void addBackgroundMusic(FileHandle file) {
Music music = Gdx.audio.newMusic(file);
music.setOnCompletionListener(nextSongListener);
backgroundSongs.add(new MusicSong(music, file.name()));
}
public void init() {
addBackgroundMusic(Gdx.files.internal("music/sanctumwave-risen.mp3"));
addBackgroundMusic(Gdx.files.internal("music/sanctumwave-devine-intellect.mp3"));
addBackgroundMusic(Gdx.files.internal("music/theselfhelpgroup-temple-os.mp3"));
addBackgroundMusic(Gdx.files.internal("music/sanctumwave-nightwalk.mp3"));
}
public void dispose() {
introSong.music.dispose();
creditsSong.music.dispose();
for (MusicSong song:backgroundSongs) {
song.music.dispose();
}
}
public List<MusicSong> getBackgroundSongs() {
return backgroundSongs;
}
public String getCurrentSongName() {
if (currentSong != null) {
return currentSong.getName();
} else {
return "None";
}
}
public void stop() {
if (currentSong != null) {
currentSong.music.stop();
}
}
public void play(MusicSongType type) {
MusicSong nextSong = null;
if (MusicSongType.INTRO.equals(type)) {
nextSong = introSong;
} else if (MusicSongType.CREDITS.equals(type)) {
nextSong = creditsSong;
} else {
int currentBackground = backgroundSongs.indexOf(currentSong);
if (currentBackground == -1) {
nextSong = backgroundSongs.get(0);
} else {
nextSong = currentSong;
}
}
play(nextSong);
}
public void play(MusicSong song) {
if (song == null) {
return;
}
stop();
currentSong = song;
currentSong.music.play();
}
class NextSongListener implements OnCompletionListener {
@Override
public void onCompletion(Music music) {
next();
play(currentSong);
}
}
public void next() {
int currentBackground = backgroundSongs.indexOf(currentSong);
if (currentBackground == -1) {
return; // some other
}
if (currentBackground == backgroundSongs.size()-1) {
currentBackground = -1; // loop to start
}
boolean play = currentSong.music.isPlaying();
currentSong.music.stop();
currentSong = backgroundSongs.get(currentBackground+1);
if (play) {
currentSong.music.play();
}
}
public void prev() {
int currentBackground = backgroundSongs.indexOf(currentSong);
if (currentBackground == -1) {
return; // some other
}
if (currentBackground == 0) {
currentBackground = backgroundSongs.size(); // loop to end
}
boolean play = currentSong.music.isPlaying();
currentSong.music.stop();
currentSong = backgroundSongs.get(currentBackground-1);
if (play) {
currentSong.music.play();
}
}
}

View file

@ -0,0 +1,96 @@
package love.distributedrebirth.demo4d.music;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import imgui.ImGui;
import imgui.flag.ImGuiCond;
import imgui.flag.ImGuiSelectableFlags;
import imgui.flag.ImGuiTableColumnFlags;
import imgui.flag.ImGuiTableFlags;
import imgui.type.ImBoolean;
import love.distributedrebirth.demo4d.ImGuiRendererMain;
import love.distributedrebirth.demo4d.Demo4DMain;
import net.spookygames.gdx.nativefilechooser.NativeFileChooserCallback;
import net.spookygames.gdx.nativefilechooser.NativeFileChooserConfiguration;
public class MusicPlayerRenderer extends ImGuiRendererMain {
public MusicPlayerRenderer(Demo4DMain main) {
super(main);
}
@Override
public void render(ImBoolean widgetOpen) {
ImGui.setNextWindowPos(100, 100, ImGuiCond.FirstUseEver);
ImGui.setNextWindowSize(320, 240, ImGuiCond.FirstUseEver);
ImGui.begin("Music Player", widgetOpen);
ImGui.text("Current Song:");
ImGui.sameLine();
ImGui.text(main.music.getCurrentSongName());
ImGui.separator();
if (ImGui.button("Play")) {
main.music.play(MusicSongType.BACKGROUND);
}
ImGui.sameLine();
if (ImGui.button("<")) {
main.music.prev();
}
ImGui.sameLine();
if (ImGui.button(">")) {
main.music.next();
}
ImGui.sameLine();
if (ImGui.button("Stop")) {
main.music.stop();
}
ImGui.sameLine();
if (ImGui.button("Add")) {
NativeFileChooserConfiguration conf = new NativeFileChooserConfiguration();
conf.directory = Gdx.files.absolute(System.getProperty("user.home"));
conf.mimeFilter = "audio/*";
conf.title = "Choose audio file";
main.fileChooser.chooseFile(conf, new NativeFileChooserCallback() {
@Override
public void onFileChosen(FileHandle file) {
main.music.addBackgroundMusic(file);
}
@Override
public void onCancellation() {
}
@Override
public void onError(Exception exception) {
}
});
}
int flags = ImGuiTableFlags.ScrollX | ImGuiTableFlags.RowBg | ImGuiTableFlags.BordersOuter | ImGuiTableFlags.BordersV;
ImGui.beginTable("playlist", 3, flags);
ImGui.tableSetupColumn("#", ImGuiTableColumnFlags.NoHide);
ImGui.tableSetupColumn("Play");
ImGui.tableSetupColumn("Name");
ImGui.tableHeadersRow();
int i=1;
for (MusicSong song:main.music.getBackgroundSongs()) {
ImGui.pushID(i);
ImGui.tableNextRow();
ImGui.tableNextColumn();
ImGui.selectable(""+i, song.isPlaying(), ImGuiSelectableFlags.None);
ImGui.tableNextColumn();
if (ImGui.smallButton(">")) {
main.music.play(song);
}
ImGui.tableNextColumn();
ImGui.selectable(song.getName(), song.isPlaying(), ImGuiSelectableFlags.None);
ImGui.popID();
i++;
}
ImGui.endTable();
ImGui.end();
}
}

View file

@ -0,0 +1,21 @@
package love.distributedrebirth.demo4d.music;
import com.badlogic.gdx.audio.Music;
public class MusicSong {
protected final Music music;
private final String name;
public MusicSong(Music music, String name) {
this.music = music;
this.name = name;
}
public String getName() {
return name;
}
public boolean isPlaying() {
return music.isPlaying();
}
}

View file

@ -0,0 +1,7 @@
package love.distributedrebirth.demo4d.music;
public enum MusicSongType {
INTRO,
CREDITS,
BACKGROUND
}