Klasse 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}
  • Methodendetails

    • loadFromFile

      public static SoundAsset loadFromFile(String file)
      Creates a new sound asset referencing a file on harddrive.
      Parameter:
      file - the path to the sound file on the harddrive.
      Gibt zurück:
      a new sound asset instance.
      Example: Load sound "explosion.ogg" from "assets" subfolder in plugin directory
      1SoundAsset sound = SoundAsset.loadFromFile(getPath() + "/assets/explosion.ogg");
      2//do something with the sound asset
    • 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.
      Parameter:
      file - the path to the sound file on the harddrive.
      streamed - true to enable streaming, false to disable it (default).
      Gibt zurück:
      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.
      Parameter:
      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).
      Gibt zurück:
      a new sound asset instance.
      Example: Load sound "explosion.ogg" from jar file (in package rw.plugin.sounds)
      1//First param is the plugin, so if you load it in your "Plugin" class,
      2//you can simply pass "this" as parameter
      3SoundAsset sound = new SoundAsset(this, "/rw/plugin/sounds/explosion.ogg");
      4//do something with the sound asset
    • 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.
      Parameter:
      url - the URL to the stream.
      Gibt zurück:
      a new sound asset instance.
    • isStreamed

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