KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > client > functions > SoundOperators


1 package rero.client.functions;
2
3 import sleep.engine.*;
4 import sleep.runtime.*;
5 import sleep.interfaces.*;
6 import sleep.bridges.BridgeUtilities;
7
8 import java.util.*;
9 import java.io.*;
10 import java.applet.*;
11
12 import rero.client.*;
13
14 public class SoundOperators extends Feature implements Loadable
15 {
16    public void init()
17    {
18       getCapabilities().getScriptCore().addBridge(this);
19    }
20
21    public boolean scriptLoaded(ScriptInstance script)
22    {
23       script.getScriptEnvironment().getEnvironment().put("&loadSound", new loadSound());
24       script.getScriptEnvironment().getEnvironment().put("&soundPlay", new soundPlay());
25       script.getScriptEnvironment().getEnvironment().put("&soundLoop", new soundLoop());
26       script.getScriptEnvironment().getEnvironment().put("&soundStop", new soundStop());
27       return true;
28    }
29
30    public boolean scriptUnloaded(ScriptInstance script)
31    {
32       return true;
33    }
34
35    private static class loadSound implements Function
36    {
37       public Scalar evaluate(String JavaDoc name, ScriptInstance si, Stack locals)
38       {
39          String JavaDoc file = BridgeUtilities.getString(locals, null);
40
41          if (file != null && (new File(file)).exists())
42          {
43             try
44             {
45                AudioClip clip = Applet.newAudioClip((new File(file)).toURL());
46                return SleepUtils.getScalar(clip);
47             }
48             catch (Exception JavaDoc ex)
49             {
50                ex.printStackTrace();
51             }
52          }
53      
54          return SleepUtils.getEmptyScalar();
55       }
56    }
57
58    private static class soundPlay implements Function
59    {
60       public Scalar evaluate(String JavaDoc name, ScriptInstance si, Stack locals)
61       {
62          AudioClip clip = (AudioClip)BridgeUtilities.getObject(locals);
63          clip.play();
64
65          return SleepUtils.getEmptyScalar();
66       }
67    }
68
69    private static class soundLoop implements Function
70    {
71       public Scalar evaluate(String JavaDoc name, ScriptInstance si, Stack locals)
72       {
73          AudioClip clip = (AudioClip)BridgeUtilities.getObject(locals);
74          clip.loop();
75
76          return SleepUtils.getEmptyScalar();
77       }
78    }
79
80    private static class soundStop implements Function
81    {
82       public Scalar evaluate(String JavaDoc name, ScriptInstance si, Stack locals)
83       {
84          AudioClip clip = (AudioClip)BridgeUtilities.getObject(locals);
85          clip.stop();
86
87          return SleepUtils.getEmptyScalar();
88       }
89    }
90
91 }
92
Popular Tags