summaryrefslogtreecommitdiffstats
path: root/tdehtml/java/org/kde/kjas/server/KJASAudioClip.java
blob: 3a40cf6e069d79328670cff3b72153ea24eb16ac (plain)
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
98
package org.kde.kjas.server;

import java.applet.*;
import java.net.*;
import java.util.*;
/**
* Background Audioclip Loader and Player.
* @author Till Krech (till@snafu.de)
*/
public class KJASAudioClip implements AudioClip
{
    private AudioClip theClip;
    private final static int PLAYING = 1;
    private final static int LOOPING = 2;
    private final static int STOPPED = 3;
    private int state;
    private static Hashtable cache = new Hashtable();

    /**
    * creates a new Audioclip.
    * The AudioClip is loaded in background. The Constructor returns immediately.
    */
    public KJASAudioClip(URL url)
    {
        state = STOPPED;
        theClip = (AudioClip)cache.get(url);
        if (theClip == null) {
            final URL theUrl = url;
         
            new Thread
            (
                new Runnable() {
                    public void run() {
                        theClip = java.applet.Applet.newAudioClip(theUrl);
                        cache.put(theUrl, theClip);
                        if (state == LOOPING) {
                            theClip.loop();
                        } else if (state == PLAYING) {
                            theClip.play();
                        }
                    }
                }, "AudioClipLoader " + url.getFile()
            ).start();
        }           
    }

    /**
    * play continously when the clip is loaded
    */
    public void loop()
    {
        state = LOOPING;
        if (theClip != null) {
            new Thread
            (
                new Runnable() {
                    public void run() {
                       theClip.loop();
                    }
                }, "AudioClipLooper "
            ).start();           
        }  
    }

    /**
    * play when the clip is loaded
    */
    public void play()
    {
        state = PLAYING;
        if (theClip != null) {
            new Thread
            (
                new Runnable() {
                    public void run() {
                       theClip.play();
                    }
                }, "AudioClipPlayer "
            ).start();           
        }  
    }

    /**
    * stop the clip
    */
    public void stop()
    {
        state = STOPPED;
        if (theClip != null) {
            theClip.stop();
        }
    }
    
    public void finalize() {
        stop();
    }
}