Skip to content

Commit

Permalink
Ensure current BASS stream channel is stopped first before playing a …
Browse files Browse the repository at this point in the history
…new one
  • Loading branch information
Rian8337 committed Nov 21, 2024
1 parent 2d0e275 commit 05a8f86
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/ru/nsu/ccfit/zuev/audio/BassSoundProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ public class BassSoundProvider {

public static final BassSoundProvider EMPTY = new BassSoundProvider();

private static final int SIMULTANEOUS_PLAYBACKS = 8;

private int sample = 0;
private int channel = 0;
private boolean looping;
Expand All @@ -30,7 +28,7 @@ public boolean prepare(final String fileName) {
free();

if (fileName != null && !fileName.isEmpty()) {
sample = BASS.BASS_SampleLoad(fileName, 0, 0, SIMULTANEOUS_PLAYBACKS, BASS.BASS_SAMPLE_OVER_POS);
sample = BASS.BASS_SampleLoad(fileName, 0, 0, 1, BASS.BASS_SAMPLE_OVER_POS);
BASS.BASS_SampleGetInfo(sample, sampleInfo);
applyAudioEffectsToSample();
} else {
Expand All @@ -45,7 +43,7 @@ public boolean prepare(final AssetManager manager, final String assetName) {

if (manager != null && assetName != null && !assetName.isEmpty()) {
BASS.Asset asset = new BASS.Asset(manager, assetName);
sample = BASS.BASS_SampleLoad(asset, 0, 0, SIMULTANEOUS_PLAYBACKS, BASS.BASS_SAMPLE_OVER_POS);
sample = BASS.BASS_SampleLoad(asset, 0, 0, 1, BASS.BASS_SAMPLE_OVER_POS);
BASS.BASS_SampleGetInfo(sample, sampleInfo);
applyAudioEffectsToSample();
} else {
Expand All @@ -64,6 +62,9 @@ public void play(float volume) {
return;
}

// Ensure the current channel is stopped first.
stop();

channel = BASS.BASS_SampleGetChannel(sample, BASS.BASS_SAMCHAN_STREAM | BASS.BASS_STREAM_AUTOFREE);
applyAudioEffectsToChannel();
BASS.BASS_ChannelSetAttribute(channel, BASS.BASS_ATTRIB_NOBUFFER, 1);
Expand All @@ -77,11 +78,13 @@ public void stop() {
}

BASS.BASS_ChannelStop(channel);
channel = 0;
}

public void free() {
BASS.BASS_SampleFree(sample);
sample = 0;
channel = 0;
}

public void setLooping(boolean looping) {
Expand Down

0 comments on commit 05a8f86

Please sign in to comment.