CreedVI / Raylib-J

Handmade Java binding for Raylib
zlib License
97 stars 16 forks source link

How do you play a sound in 0.5? #65

Closed dugongoman0 closed 3 months ago

dugongoman0 commented 3 months ago

i'm using the 0.5 update. How can I play a sound? I'm trying using LoadSound and PlaySound, but it does not seems to work. I'm pretty new to java, and i'm maybe messing up something. This is my code rlj.audio.InitAudioDevice(); Sound s = rlj.audio.LoadSound("/Raylib-J Game 2/sounds/click_2.wav"); rlj.audio.PlaySound(s);

CreedVI commented 3 months ago

I'd need to see your code in its entirety to be able to offer any real assistance, but I've included one of the tests for the current audio implementation below for you to compare against.

package com.raylib.java.Audio;

import com.raylib.java.Raylib;
import com.raylib.java.core.Color;
import com.raylib.java.raudioal.Music;

import static com.raylib.java.core.input.Keyboard.*;

public class AudioTest{
    static Raylib rlj;

    static String[] songTitles = {
            "Spring Village",
            "In the Dark of Downtown",
    };
    static Music[] songs = new Music[2];
    static int selectedSong = 0;

    public static void main(String[] args) {
        rlj = new Raylib(600, 450, null);

        rlj.audio.InitAudioDevice();

        songs[0] = rlj.audio.LoadMusicStream("resources/Audio/SpringVillage.ogg");
        songs[1] = rlj.audio.LoadMusicStream("resources/Audio/ItDoDT.mp3");

        rlj.core.SetTargetFPS(60);

        while(!rlj.core.WindowShouldClose()) {
            update();
            render();
        }
        for (Music m : songs){
            rlj.audio.UnloadMusicStream(m);
        }
        rlj.audio.CloseAudioDevice();
    }

    private static void update() {

        for(Music m : songs) {
            rlj.audio.UpdateMusicStream(m);
        }

        if(rlj.core.IsKeyPressed(KEY_SPACE)) {
            if(rlj.audio.IsMusicPlaying(songs[selectedSong])) {
                rlj.audio.PauseMusicStream(songs[selectedSong]);
            }
            else if(!rlj.audio.IsMusicPlaying(songs[selectedSong])) {
                rlj.audio.PlayMusicStream(songs[selectedSong]);
            }
            else {
                rlj.audio.ResumeMusicStream(songs[selectedSong]);
            }
        }
        if(rlj.core.IsKeyPressed(KEY_R)) {
            rlj.audio.StopMusicStream(songs[selectedSong]);
            rlj.audio.PlayMusicStream(songs[selectedSong]);
        }
        if(rlj.core.IsKeyPressed(KEY_ONE)) {
            selectedSong = 0;
        }
        else if(rlj.core.IsKeyPressed(KEY_TWO)) {
            selectedSong = 1;
        }
    }

    private static void render() {
        rlj.core.BeginDrawing();
        rlj.core.ClearBackground(Color.BLACK);

        rlj.text.DrawText(songTitles[selectedSong], 300 - (rlj.text.MeasureText(songTitles[selectedSong], 30)/2), 150, 30, Color.RAYWHITE);

        if(rlj.audio.IsMusicPlaying(songs[selectedSong])) {
            rlj.text.DrawText("Music is Playing", 300 - (rlj.text.MeasureText("Music is Playing", 30)/2), 225, 30, Color.WHITE);
        }
        else {
            rlj.text.DrawText("Music is not Playing", 300 - (rlj.text.MeasureText("Music is not Playing", 30)/2), 225, 30, Color.WHITE);
        }
        rlj.text.DrawText("Press SPACE to play/pause", 300 - (rlj.text.MeasureText("Press SPACE to play/pause", 15)/2), 300, 15, Color.WHITE);
        rlj.text.DrawText("Press R to restart", 300 - (rlj.text.MeasureText("Press R to restart", 15)/2), 325, 15, Color.WHITE);
        rlj.core.EndDrawing();
    }
}
dugongoman0 commented 3 months ago

this is the error that it's showing up: Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "buf" is null at java.base/java.io.ByteArrayInputStream.<init>(ByteArrayInputStream.java:109) at com.raylib.java.raudioal.rAudioAL.LoadMP3(rAudioAL.java:1188) at com.raylib.java.raudioal.rAudioAL.LoadWave(rAudioAL.java:232) at com.raylib.java.raudioal.rAudioAL.LoadSound(rAudioAL.java:275) at main.Ball.<init>(Ball.java:44) at main.Main.main(Main.java:22)

I've tried using an mp3, like in the example, but it doesn't work either I've loaded it like this: this.paddleBounceSound = this.rlj.audio.LoadSound("sfx/hitHurt.mp3"); and played it like this: this.rlj.audio.PlaySound(this.paddleBounceSound);

Edited: I don't know if it makes any difference, but i did not initialized this.paddleBounceSound with any value Sound paddleBounceSound;). And this is my directory: RaylibGame/src/main/Main.java, Ball.java, Paddle.java etc... and my sfx are in RaylibGame/src/main/sfx/hitHurt.mp3 I've done the InitAudioDevice, with the same Raylib istance. Thanks for responding btw

CreedVI commented 3 months ago

Alright, looking at the error produced my first question is are you loading any other assets with your project (e.g. images, sprites)?

If no I'd recommend ensuring your assets are included in your compile output.

dugongoman0 commented 3 months ago

No, i do not use any type of sprites, just basic shapes. Is there a way I can include them in eclipse? or do I have to compile it in the terminal everytime?

CreedVI commented 3 months ago

I do not use eclipse as my IDE so I cannot help you with that. Though Google provided me this link which may be helpful to you.

https://help.eclipse.org/latest/index.jsp?topic=%2Forg.eclipse.emf.cdo.doc%2Fhtml%2Fusers%2FDoc06_UsingResources.html

dugongoman0 commented 3 months ago

now it doesn't crash, but it does not seem to work. I tried chainging this line of codethis.paddleBounceSound = this.rlj.audio.LoadSound("/audio/hitHurt.mp3"); with some files that do not exist, and it does exactly the same thing, i can't ear anything

CreedVI commented 3 months ago

Try removing the leading slash from your filepath

dugongoman0 commented 3 months ago

now it crashes. I do not know if it can help, but as soon as the INFO says that i loaded it(with the leading slash), it says unloaded wave data from RAM. maybe just because it's exiting a function? i do not know