-
Notifications
You must be signed in to change notification settings - Fork 0
/
snd.java
97 lines (80 loc) · 2.81 KB
/
snd.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* snd.java
*
* © Synthetic Dreams, 2003-2008
* Confidential and proprietary.
*/
package com.synthdreams.GalacticBlast;
import net.rim.device.api.ui.component.Dialog;
import java.io.InputStream;
import java.lang.Class;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import net.rim.device.api.system.Alert;
// Sound engine
class SND {
Player _musicPlayer; // Java media player
SND() { }
// Play a midi file for background music
void playMusic(String passMusic)
{
try
{
// Set InputStream to a midi file included as resource, as specified by
// passMusic
InputStream in = getClass().getResourceAsStream("/" + passMusic);
// Create a media player with mime type of audio/midi using our inputstream
_musicPlayer = javax.microedition.media.Manager.createPlayer(in, "audio/midi");
// Ready the data and start playing it. To loop indefinitely, we set loopcount
// to -1.
_musicPlayer.realize();
_musicPlayer.prefetch();
_musicPlayer.setLoopCount(-1);
_musicPlayer.start();
}
catch (Exception e)
{
Dialog.alert("Error playing music");
}
}
// Stop playing music
void stopMusic()
{
try
{
// Tell player to stop playing
_musicPlayer.stop();
}
catch (Exception e)
{
Dialog.alert("Error stopping music");
}
// Then release the data and close out the player
_musicPlayer.deallocate();
_musicPlayer.close();
}
// The Playsound method plays a simple combinations of tones to simulate a firing
// noise. This was necessary, as due to a bug or limitation of the BlackBerry 8830
// (the phone I do my testing on), playing a WAV file stopped the midi player and
// any other sound effects. Player doesn't appear to mix properly (if at all). However,
// a midi file can be played while using the Alert objects startAudio method which
// can play a sequence of tones, so this is what we've done for now.
void playSound()
{
// A sequence of frequencies and durations (eg 1400hz for 15ms, 1350hz for 15ms, etc)
short[] fire = {1400, 15, 1350, 15, 1320, 20, 1300, 20, 1250, 25, 1200, 35};
try
{
Alert.startAudio(fire, 100);
}
catch (Exception e)
{
Dialog.alert("Error playing sound effect.");
}
}
// Activates the phone's vibration functionality for a specific number of ms
void vibrate(int passMilli)
{
Alert.startVibrate(passMilli);
}
}