Class SoundAsset

java.lang.Object
net.risingworld.api.assets.Asset
net.risingworld.api.assets.SoundAsset

public final class SoundAsset extends Asset
Represents a custom audio file (this could either be a sound effect or a music track).

Supported file formats: ogg, mp2, mp3, midi/mid, wav, flac
Example: Create a SoundAsset and play it for every player who joins the game
1public class MyPlugin extends Plugin implements Listener{
2 //Our reusable sound asset
3 public SoundAsset welcomeSound;
4
5 @Override
6 public void onEnable(){
7 //Create a SoundAsset once and reuse it for every player (not necessary)
8 welcomeSound = SoundAsset.loadFromFile(getPath() + "/welcome.ogg");
9
10 //Remember to register the event listener
11 registerEventListener(this);
12 }
13
14 @Override
15 public void onDisable(){
16 //...
17 }
18
19 @EventMethod
20 public void onPlayerSpawn(PlayerSpawnEvent event){
21 Player player = event.getPlayer();
22 player.playSound(welcomeSound, player.getPosition());
23 }
24
25}
  • Method Details

    • loadFromFile

      public static SoundAsset loadFromFile(String file)
      Creates a new sound asset referencing a file on harddrive.
      Parameters:
      file - the path to the sound file on the harddrive.
      Returns:
      a new sound asset instance.
    • loadFromFile

      public static SoundAsset loadFromFile(String file, boolean streamed)
      Creates a new sound asset referencing a file on harddrive. Optionally you can determine if the sound should be streamed or not. Usually the whole sound file is loaded into memory before playing it, but if streaming is enabled, the game reads the sound while playing. This reduces memory consumption, but increases cpu load. By default, streaming is disabled.

      Important: It only makes sense to stream large sound files, e.g music etc.
      Parameters:
      file - the path to the sound file on the harddrive.
      streamed - true to enable streaming, false to disable it (default).
      Returns:
      a new sound asset instance.
    • loadFromPlugin

      public static SoundAsset loadFromPlugin(Plugin plugin, String resource)
      Creates a new sound asset and loads the sound from the plugin jar file.
      Parameters:
      plugin - a reference to the plugin. This is required to make sure the game loads the resource from the correct plugin jar.
      resource - the path to the resource (inside the jar file).
      Returns:
      a new sound asset instance.
    • loadFromAssetBundle

      public static SoundAsset loadFromAssetBundle(AssetBundle bundle, String path)
    • load

      public static SoundAsset load(byte[] data)
    • loadFromURL

      public static SoundAsset loadFromURL(String url)
      Loads a stream from an URL. This could be a link to an online radio stream, for example.
      Parameters:
      url - the URL to the stream.
      Returns:
      a new sound asset instance.
    • isStreamed

      public boolean isStreamed()
      Determines if the sound is streamed or not.
      Returns:
      true if this sound will be streamed, false if not.