01: // Copyright (c) 2002 Cunningham & Cunningham, Inc.
02: // Read license.txt in this directory.
03:
04: package eg.music;
05:
06: public class MusicPlayer {
07:
08: static Music playing = null;
09: static double paused = 0;
10:
11: // Controls /////////////////////////////////
12:
13: static void play(Music m) {
14: if (paused == 0) {
15: Music.status = "loading";
16: double seconds = m == playing ? 0.3 : 2.5;
17: Simulator.nextPlayStarted = Simulator.schedule(seconds);
18: } else {
19: Music.status = "playing";
20: Simulator.nextPlayComplete = Simulator.schedule(paused);
21: paused = 0;
22: }
23: }
24:
25: static void pause() {
26: Music.status = "pause";
27: if (playing != null && paused == 0) {
28: paused = (Simulator.nextPlayComplete - Simulator.time) / 1000.0;
29: Simulator.nextPlayComplete = 0;
30: }
31: }
32:
33: static void stop() {
34: Simulator.nextPlayStarted = 0;
35: Simulator.nextPlayComplete = 0;
36: playComplete();
37: }
38:
39: // Status ///////////////////////////////////
40:
41: static double secondsRemaining() {
42: if (paused != 0) {
43: return paused;
44: } else if (playing != null) {
45: return (Simulator.nextPlayComplete - Simulator.time) / 1000.0;
46: } else {
47: return 0;
48: }
49: }
50:
51: static double minutesRemaining() {
52: return Math.round(secondsRemaining() / .6) / 100.0;
53: }
54:
55: // Events ///////////////////////////////////
56:
57: static void playStarted() {
58: Music.status = "playing";
59: playing = MusicLibrary.looking;
60: Simulator.nextPlayComplete = Simulator
61: .schedule(playing.seconds);
62: }
63:
64: static void playComplete() {
65: Music.status = "ready";
66: playing = null;
67: }
68: }
|