Skip to content

Commit

Permalink
Add playSound() function to MapTool
Browse files Browse the repository at this point in the history
- Add playSound(soundName) function to MapTool to play local sounds
- Currently 3 sounds available: "dink", "clink", and "door"
- Sounds only play on client's computer, but this could be easily changed
- Add command to play local sounds with playSound(fileName,filePath)
- Example: [playSound("music.mp3", "C:/")], assuming there is a music.mp3 in drive C:
- Proof of concept that demonstrates how sounds could be implemented
- First step to fulfill RPTools#615
  • Loading branch information
Merudo committed Aug 26, 2019
1 parent 42dbeb8 commit 4f52a75
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/java/net/rptools/lib/sound/SoundManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,15 @@ public void playSoundEvent(String eventId) {
}
}
}

/** Play the sound associated with the URL sound */
public void playSound(URL sound) {
if (sound != null) {
try {
SoundPlayer.play(sound);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public class MapToolLineParser {
PlayerFunctions.getInstance(),
RemoveAllFromInitiativeFunction.getInstance(),
ReturnFunction.getInstance(),
SoundFunctions.getInstance(),
StateImageFunction.getInstance(),
StringFunctions.getInstance(),
StrListFunctions.getInstance(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* This software Copyright by the RPTools.net development team, and
* licensed under the Affero GPL Version 3 or, at your option, any later
* version.
*
* MapTool Source Code is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public
* License * along with this source Code. If not, please visit
* <http://www.gnu.org/licenses/> and specifically the Affero license
* text at <http://www.gnu.org/licenses/agpl.html>.
*/
package net.rptools.maptool.client.functions;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import net.rptools.lib.sound.SoundManager;
import net.rptools.maptool.client.MapTool;
import net.rptools.parser.Parser;
import net.rptools.parser.ParserException;
import net.rptools.parser.function.AbstractFunction;

public class SoundFunctions extends AbstractFunction {

/** The singleton instance. */
private static final SoundFunctions instance = new SoundFunctions();

private SoundFunctions() {
super(1, 2, "playSound");
}

/**
* Gets the SoundFunctions instance.
*
* @return the instance.
*/
public static SoundFunctions getInstance() {
return instance;
}

@Override
public Object childEvaluate(Parser parser, String functionName, List<Object> args)
throws ParserException {
SoundManager soundManager = MapTool.getSoundManager();
URL sound;
if (args.size() == 1 || args.get(1).toString().equals("")) {
sound = soundManager.getRegisteredSound(args.get(0).toString());
} else {
try {
String path = args.get(1).toString() + args.get(0).toString();
File file = new File(path);
if (!file.exists()) {
throw new ParserException("Can't find file");
}
sound = file.toURI().toURL();
} catch (MalformedURLException e) {
throw new ParserException("Wrong URL");
}
}
if (sound == null) {
return 0;
} else {
soundManager.playSound(sound);
return 1;
}
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
#
Dink:net/rptools/maptool/client/sound/dink.mp3
Clink:net/rptools/maptool/client/sound/clink.mp3
Door:net/rptools/maptool/client/sound/HighSqueak1.mp3

0 comments on commit 4f52a75

Please sign in to comment.