Skip to content

Commit

Permalink
Fix music not looping correctly by removing sound id and use global m…
Browse files Browse the repository at this point in the history
…ethods
  • Loading branch information
xpenatan committed Jan 4, 2025
1 parent 440fc7f commit 885881d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,97 @@ public class Howl implements JSObject {
public static native Howl create(ArrayBufferViewWrapper arrayBufferView);

public native int play();

public native int play(int soundId);

public native void stop(int soundId);

public native void pause(int soundId);

@JSMethod("rate")
public native void setRate(float rate, int soundId);

@JSMethod("rate")
public native float getRate(int soundId);

@JSMethod("volume")
public native void setVolume(float volume, int soundId);

@JSMethod("volume")
public native float getVolume(int soundId);

@JSMethod("mute")
public native void setMute(boolean mute, int soundId);

@JSMethod("mute")
public native boolean getMute(int soundId);

@JSMethod("seek")
public native void setSeek(float seek, int soundId);

@JSMethod("seek")
public native float getSeek(int soundId);

@JSMethod("duration")
public native int getDuration(int spriteId);

@JSMethod("duration")
public native float getDuration();

@JSMethod("loop")
public native void setLoop (boolean loop , int soundId);

@JSMethod("loop")
public native boolean getLoop(int soundId);

@JSMethod("playing")
public native boolean isPlaying(int soundId);

@JSMethod("pannerAttr")
public native void setPannerAttr(HowlPannerAttr pannerAttr, int soundId);

@JSMethod("pannerAttr")
public native HowlPannerAttr getPannerAttr(int soundId);

@JSMethod("stereo")
public native void setStereo(float pan, int soundId);

@JSMethod("stereo")
public native float getStereo(int soundId);

// Globals
// ##### Globals

public native void stop();

public native void unload();

public native void pause();

@JSMethod("volume")
public native void setVolume(float volume);

@JSMethod("volume")
public native float getVolume();

public native float on(String event, HowlEventFunction function, int soundId);

public native float on(String event, HowlEventFunction function);

@JSMethod("stereo")
public native void setStereo(float pan);

@JSMethod("loop")
public native void setLoop (boolean loop);

@JSMethod("loop")
public native boolean getLoop();

@JSMethod("playing")
public native boolean isPlaying();

@JSMethod("seek")
public native void setSeek(float seek);

@JSMethod("seek")
public native float getSeek();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,106 +7,79 @@

public class HowlMusic implements Music {

private int soundId;
private Howl howl;

public HowlMusic(FileHandle fileHandle) {
byte[] bytes = fileHandle.readBytes();
ArrayBufferViewWrapper data = TypedArrays.getTypedArray(bytes);
howl = Howl.create(data);
soundId = -1;
}

@Override
public void play() {
if(soundId != -1) {
soundId = howl.play(soundId);
}
else {
soundId = howl.play();
}
howl.play();
}

@Override
public void pause() {
if(soundId != -1) {
howl.pause(soundId);
}
howl.pause();
}

@Override
public void stop() {
howl.stop();
soundId = -1;
}

@Override
public boolean isPlaying() {
if(soundId != -1) {
return howl.isPlaying(soundId);
}
return false;
return howl.isPlaying();
}

@Override
public void setLooping(boolean isLooping) {
if(soundId != -1) {
howl.setLoop(isLooping, soundId);
}
howl.setLoop(isLooping);
}

@Override
public boolean isLooping() {
if(soundId != -1) {
return howl.getLoop(soundId);
}
return false;
return howl.getLoop();
}

@Override
public void setVolume(float volume) {
if(soundId != -1) {
howl.setVolume(volume, soundId);
}
howl.setVolume(volume);
}

@Override
public float getVolume() {
if(soundId != -1) {
return howl.getVolume(soundId);
}
return 0f;
return howl.getVolume();
}

@Override
public void setPan(float pan, float volume) {
if(soundId != -1) {
howl.setStereo(pan, soundId);
howl.setVolume(volume, soundId);
}
howl.setStereo(pan);
howl.setVolume(volume);
}

@Override
public void setPosition(float position) {
if(soundId != -1) {
howl.setSeek(position, soundId);
}
howl.setSeek(position);
}

@Override
public float getPosition() {
return howl.getSeek(soundId);
return howl.getSeek();
}

@Override
public void dispose() {
howl.stop();
soundId = -1;
howl.unload();
howl = null;
}

@Override
public void setOnCompletionListener(OnCompletionListener listener) {
howl.on("end", () -> listener.onCompletion(HowlMusic.this), soundId);
howl.on("end", () -> listener.onCompletion(HowlMusic.this));
}
}

0 comments on commit 885881d

Please sign in to comment.