I apologize if this is a duplicate, but nothing I have found here seems to work. I am working on a game for Desktop and Android in Java using libGDX. I added MIDI support by using an interface in my core module that is implemented in the platform-specific modules. Here is that interface:
public interface Midi {
void setLooping(boolean looping);
void pause();
void stop();
void setPosition(int pos);
void dispose();
long getPosition();
boolean isLooping();
void setVolume(float volume);
void onPause();
void onResume();
}
And my implementations in my Desktop and Android projects load and play the midi files perfectly. In the Android version, I can even set the volume easily. However I cannot figure out how to implement the volume on desktop. I want to be able to set the volume of the track to a float between 0 and 1 such that 0 is completely quiet, and 1 is the loudest. Here is my current implementation of the midi player on Desktop:
public class MidiPlayer implements Midi {
/**
* The sequencer to play the midi track.
*/
private Sequencer sequencer;
/**
* Whether or not the track is looping.
*/
private boolean looping;
public MidiPlayer(FileHandle fileHandle) {
try {
sequencer = MidiSystem.getSequencer();
sequencer.open();
sequencer.setSequence(fileHandle.read());
sequencer.setMicrosecondPosition(0);
sequencer.setLoopStartPoint(0);
sequencer.setLoopEndPoint(-1);
looping = false;
} catch (MidiUnavailableException | InvalidMidiDataException | IOException e) {
Gdx.app.error(getClass().getName(), e.getMessage(), e);
}
}
@Override
public void play() {
sequencer.start();
}
@Override
public void setLooping(boolean looping) {
this.looping = looping;
if(!looping) {
sequencer.setLoopCount(0);
} else {
sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
}
}
@Override
public void pause() {
sequencer.stop();
}
@Override
public void stop() {
sequencer.stop();
sequencer.setMicrosecondPosition(0);
}
@Override
public void setPosition(int pos) {
sequencer.setMicrosecondPosition(pos);
}
@Override
public void dispose() {
sequencer.close();
}
@Override
public long getPosition() {
return sequencer.getMicrosecondPosition();
}
@Override
public boolean isLooping() {
return this.looping;
}
@Override
public void setVolume(float volume) {
//I have tried several things here and none have worked
}
@Override
public void onPause() {
//ANDROID ONLY
}
@Override
public void onResume() {
//ANDROID ONLY
}
}
I have tried many different things, I have tried the following solutions: http://www.java2s.com/Code/Java/Development-Class/SettingtheVolumeofPlayingMidiAudio.htm
What's the method to control volume in an MIDI sequencer?
How to control the MIDI channel's volume
And none have worked.