Skip to content

Commit ecdabb4

Browse files
authored
Merge pull request #65 from JellevanAbbema/master
Added setMixerName
2 parents 9ff4cb1 + f8f1760 commit ecdabb4

File tree

3 files changed

+70
-8
lines changed

3 files changed

+70
-8
lines changed

src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayer.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,12 @@ public class StreamPlayer implements StreamPlayerInterface, Callable<Void> {
9191
private final Object audioLock = new Object();
9292

9393
// -------------------VARIABLES---------------------
94-
94+
/** Name of the mixer to use */
9595
private String mixerName;
9696

97+
/** The current mixer */
98+
private Mixer mixer = null;
99+
97100
/** The current line buffer size. */
98101
private int currentLineBufferSize = -1;
99102

@@ -520,7 +523,7 @@ private void createLine() throws LineUnavailableException, StreamPlayerException
520523
mixerName = getMixers().get(0);
521524

522525
// Continue
523-
final Mixer mixer = getMixer(mixerName);
526+
mixer = getMixer(mixerName);
524527
if (mixer == null) {
525528
outlet.setSourceDataLine((SourceDataLine) AudioSystem.getLine(lineInfo));
526529
mixerName = null;
@@ -529,8 +532,6 @@ private void createLine() throws LineUnavailableException, StreamPlayerException
529532
outlet.setSourceDataLine((SourceDataLine) mixer.getLine(lineInfo));
530533
}
531534

532-
outlet.setSourceDataLine((SourceDataLine) AudioSystem.getLine(lineInfo));
533-
534535
// --------------------------------------------------------------------------------
535536
logger.info(() -> "Line : " + outlet.getSourceDataLine());
536537
logger.info(() -> "Line Info : " + outlet.getSourceDataLine().getLineInfo());
@@ -1055,6 +1056,33 @@ private Mixer getMixer(final String name) {
10551056
return mixer;
10561057
}
10571058

1059+
/**
1060+
* Set the name of the mixer to use. This should be called before opening a Line.
1061+
*
1062+
* @param mixerName the name
1063+
*/
1064+
public void setMixerName(String mixerName) {
1065+
this.mixerName = mixerName;
1066+
}
1067+
1068+
/**
1069+
* Returns the name of the mixer
1070+
*
1071+
* @return the name of the mixer
1072+
*/
1073+
public String getMixerName(){
1074+
return mixerName;
1075+
}
1076+
1077+
/**
1078+
* Returns the mixer that is currently being used, if there is no line created this will return null
1079+
*
1080+
* @return The Mixer being used
1081+
*/
1082+
public Mixer getCurrentMixer(){
1083+
return mixer;
1084+
}
1085+
10581086
/**
10591087
* Returns Gain value.
10601088
*

src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayerInterface.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,13 @@ public interface StreamPlayerInterface {
257257
*/
258258
void setLineBufferSize(int size);
259259

260+
/**
261+
* Set the name of the mixer. This should be called before opening a Line.
262+
*
263+
* @param mixerName the name
264+
*/
265+
void setMixerName(String mixerName);
266+
260267
/**
261268
* Sets Pan value. Line should be opened before calling this method. Linear
262269
* scale : -1.0 ... +1.0

src/test/java/com/goxr3plus/streamplayer/stream/StreamPlayerMethodsTest.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323
import java.util.Map;
2424
import java.util.logging.Logger;
2525

26-
import javax.sound.sampled.AudioFileFormat;
27-
import javax.sound.sampled.AudioSystem;
28-
import javax.sound.sampled.SourceDataLine;
29-
import javax.sound.sampled.UnsupportedAudioFileException;
26+
import javax.sound.sampled.*;
3027

3128
import org.junit.jupiter.api.BeforeEach;
3229
import org.junit.jupiter.api.Test;
@@ -618,5 +615,35 @@ void equalizerKey() {
618615
fail("Test not done");
619616
}
620617

618+
@Test
619+
void setMixer() throws StreamPlayerException {
620+
//Get all available mixers
621+
List<String> mixers = player.getMixers();
622+
623+
//Use the last mixer (this is never the default)
624+
String mixerName = mixers.get(mixers.size()-1);
625+
626+
//Set the mixer
627+
player.setMixerName(mixerName);
628+
629+
//Create a line, this will either use the set mixer or set the name to null
630+
player.open(audioFile);
631+
632+
//The name of the mixers should correspond
633+
assertEquals(mixerName, player.getMixerName());
634+
635+
//Get the mixer of the used mixerName
636+
Mixer mixer = null;
637+
final Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();
638+
for (Mixer.Info mixerInfo : mixerInfos) {
639+
if (mixerInfo.getName().equals(mixerName)) {
640+
mixer = AudioSystem.getMixer(mixerInfo);
641+
break;
642+
}
643+
}
644+
645+
//The mixer that is being used should be the same as the one we found
646+
assertEquals(player.getCurrentMixer(), mixer);
647+
}
621648

622649
}

0 commit comments

Comments
 (0)