Initial Sound Thread

This commit is contained in:
Furkan Mudanyali 2021-12-13 18:25:34 +03:00
parent fba115173c
commit ca974cc260
5 changed files with 82 additions and 5 deletions

BIN
assets/80-search-intro.wav Normal file

Binary file not shown.

BIN
assets/80-search-loop.wav Normal file

Binary file not shown.

View File

@ -0,0 +1,77 @@
package com.fmudanyali;
import javax.sound.sampled.Clip;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import java.util.LinkedList;
import java.util.Queue;
import java.io.File;
import java.io.IOException;
public class Audio {
private static Queue<String> audioQueue = new LinkedList<String>();
private static Clip clip;
public static void init() throws Exception{
clip = AudioSystem.getClip();
Thread thread = new Thread(){
public void run(){
String current = new String();
while(!Main.exit){
if(audioQueue.isEmpty()){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}else{
if(audioQueue.size() == 1){
clip.loop(Clip.LOOP_CONTINUOUSLY);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
clip.loop(0);
}
if(current != audioQueue.peek()){
current = audioQueue.peek();
try {
addAudio(current);
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
}
if(clip.getMicrosecondLength() == clip.getMicrosecondPosition() && audioQueue.size() > 1){
audioQueue.remove();
}
}
}
}
};
thread.start();
}
public static void playAudio(String file) throws Exception{
File f = new File(FileLoader.getFilePath(file));
AudioInputStream audioIn = AudioSystem.getAudioInputStream(f.toURI().toURL());
clip.open(audioIn);
clip.start();
}
private static void addAudio(String file) throws Exception{
File f = new File(FileLoader.getFilePath(file));
AudioInputStream audioIn = AudioSystem.getAudioInputStream(f.toURI().toURL());
clip.close();
clip.open(audioIn);
}
public static void queueAudio(String file){
audioQueue.add(file);
}
}

View File

@ -31,6 +31,7 @@ public class Main {
} }
Render.init(); Render.init();
Screen.init(); Screen.init();
Audio.init();
scenes.push(new MainMenu()); scenes.push(new MainMenu());
while(!scenes.empty() && !exit){ while(!scenes.empty() && !exit){
@ -38,5 +39,6 @@ public class Main {
Keyboard.getKeyboardState(); Keyboard.getKeyboardState();
scenes.peek().loop(); scenes.peek().loop();
} }
exit = true;
} }
} }

View File

@ -20,6 +20,7 @@ package com.fmudanyali.scenes;
import com.fmudanyali.Main; import com.fmudanyali.Main;
import com.fmudanyali.Screen; import com.fmudanyali.Screen;
import com.fmudanyali.characters.Player; import com.fmudanyali.characters.Player;
import com.fmudanyali.Audio;
import static org.libsdl.api.event.SdlEvents.*; import static org.libsdl.api.event.SdlEvents.*;
import static com.fmudanyali.Render.*; import static com.fmudanyali.Render.*;
@ -34,6 +35,8 @@ public class Game extends Scene {
public Game() throws Exception{ public Game() throws Exception{
Screen.makeBackground("scene1/tile.bmp"); Screen.makeBackground("scene1/tile.bmp");
Audio.queueAudio("80-search-intro.wav");
Audio.queueAudio("80-search-loop.wav");
} }
@Override @Override
@ -65,11 +68,6 @@ public class Game extends Scene {
player.movement(); player.movement();
Screen.scroll(); Screen.scroll();
++kek;
if(kek == 60){
System.out.println(escPressed);
kek = 0;
}
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, Screen.wallpaper, null, null); SDL_RenderCopy(renderer, Screen.wallpaper, null, null);