Skip to content

Commit

Permalink
(#3, #23) Add simple audio example
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasstarsz committed Aug 9, 2021
1 parent 21cab17 commit 68f2b0c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ sourceSets {
example {
compileClasspath += sourceSets.main.output
runtimeClasspath += sourceSets.main.output
output.setResourcesDir(java.destinationDirectory)

task example(type: Exec) {
doFirst {
Expand Down
1 change: 1 addition & 0 deletions src/example/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/** Examples for <a href="https://github.com/lucasstarsz/FastJ" target="_blank">The FastJ Game Engine</a>. */
module fastj.game {
requires fastj.library;
opens sound;
}
57 changes: 41 additions & 16 deletions src/example/java/tech/fastj/example/audio/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,53 @@
import tech.fastj.systems.audio.AudioManager;

import java.nio.file.Path;
import java.util.concurrent.TimeUnit;

public class Main {
public static void main(String[] args) {
/* FastJ Audio */

/* FastJ's audio engine provides a quick and easy way to play and control audio.
* It contains a few audio types, and deals with a few common situations:
* - load audio into memory, or stream audio from a file
* - play/pause/resume/stop options
* - events for each audio action (audio stream open/close, play/pause/resume/stop)
* - (memory-loaded audio only) looping and playback position options
* - (streamed audio only) gain/pan/balance/mute controls
public static void main(String[] args) throws InterruptedException {
/* Simple FastJ Audio */

/* FastJ's audio engine provides a simple way to play audio: AudioManager.playSound.
* This is the method you'll want to use to play audio without any hassle -- it can be
* called from anywhere, and will work to be as efficient as possible while playing.
*
* The method takes in a filepath -- the filepath to your sound file. A filepath can be
* either a Path or a URL. */

/* - A Path (java.nio.file.Path) is as what it implies -- a path to a file.
*
* As you can see, there's a lot to cover! So instead, I'll be covering the most basic of
* operations: simply playing a sound file. */
* This works best for situations where you don't need code portability -- with the
* existence of Path.of("path/to/some/audio.wav"), you'll have little-to-no trouble
* determining where the path leads.
*
* However, this comes at the cost of having a much harder time using the code in a
* distributed format. Path resolves its paths based on where the program is called,
* which most of the time does not work for programs that have shortcuts (like most
* executables on Windows!) */
AudioManager.playSound(Path.of("src/example/resources/sound/test_audio.wav"));


/* Simple Audio Playing */
/* For the sake of this example, I've put a second of sleep time after each audio sound.
* This gives you a chance to hear the audio being played. */
TimeUnit.SECONDS.sleep(1);

/* AudioManager.playSound
* It takes in a Path object, which is just the filepath to your sound file. */

AudioManager.playSound(Path.of("src/example/resources/sound/test_audio.wav"));
/* Now, let's look at the second option: a URL.
* - A URL (java.net.URL) is also a path to a file, but with slightly different rules and
* configuration.
*
* A URL takes a bit more to set up -- it requires a class loader, which you can get using:
*
* YourClass.class.getClassLoader();
*
* The cool thing about class loaders, however, is that they manage resources much easier
* than Path does when it comes to portable files. The usage below will work inside a
* jarfile as well as outside one. */
AudioManager.playSound(Main.class.getClassLoader().getResource("sound/test_audio.wav"));


/* For the sake of this example, I've put a second of sleep time after each audio sound.
* This gives you a chance to hear the audio being played. */
TimeUnit.SECONDS.sleep(1);
}
}

0 comments on commit 68f2b0c

Please sign in to comment.