hisasann (owner)

Revisions

gist: 82871 Download_button fork
public
Public Clone URL: git://gist.github.com/82871.git
Embed All Files: show embed
WiiPlaySound.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
98
99
100
101
102
import java.io.IOException;
 
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
 
import wiiremotej.PrebufferedSound;
import wiiremotej.WiiRemote;
import wiiremotej.WiiRemoteJ;
import wiiremotej.event.WRButtonEvent;
import wiiremotej.event.WRIREvent;
import wiiremotej.event.WiiRemoteAdapter;
import wiiremotej.event.WiiRemoteListener;
 
public class WiiSample3 {
 
    private static PrebufferedSound prebuf;
    private static WiiRemote remote;
 
/**
* @param args
* @throws IOException
* @throws UnsupportedAudioFileException
*/
public static void main(String[] args) throws IOException, UnsupportedAudioFileException {
System.setProperty("bluecove.jsr82.psm_minimum_off", "true");
 
        WiiRemoteJ.setConsoleLoggingAll();
        while (remote == null) {
            try {
                remote = WiiRemoteJ.findRemote();
            }
            catch(Exception e) {
                remote = null;
                e.printStackTrace();
                System.out.println("Failed to connect remote. Trying again.");
            }
        }
remote.addWiiRemoteListener(simpleEventLisntener);
        remote.setAccelerometerEnabled(true);
        remote.setSpeakerEnabled(true);
        remote.setIRSensorEnabled(true, WRIREvent.BASIC);
        remote.setLEDIlluminated(0, true);
 
        System.out.println("Buffering audio file...");
        long time = System.currentTimeMillis();
        AudioInputStream audio = AudioSystem.getAudioInputStream(new java.io.File("Audio.au"));
        prebuf = WiiRemote.bufferSound(audio);
        time = System.currentTimeMillis()-time;
        time /= 1000;
        System.out.println("Prebuf done: " + time + " seconds.");
}
 
private static WiiRemoteListener simpleEventLisntener = new WiiRemoteAdapter() {
public void disconnected() {
System.out.println("Remote disconnected... Please Wii again.");
System.exit(0);
}
 
public void buttonInputReceived(WRButtonEvent evt) {
if (evt.wasPressed(WRButtonEvent.TWO)) System.out.println("2");
if (evt.wasPressed(WRButtonEvent.ONE)) System.out.println("1");
if (evt.wasPressed(WRButtonEvent.B)) System.out.println("B");
if (evt.wasPressed(WRButtonEvent.A)) System.out.println("A");
if (evt.wasPressed(WRButtonEvent.MINUS)) System.out.println("Minus");
if (evt.wasPressed(WRButtonEvent.HOME)) System.out.println("Home");
if (evt.wasPressed(WRButtonEvent.LEFT)) System.out.println("Left");
if (evt.wasPressed(WRButtonEvent.RIGHT)) System.out.println("Right");
if (evt.wasPressed(WRButtonEvent.DOWN)) System.out.println("Down");
if (evt.wasPressed(WRButtonEvent.UP)) System.out.println("Up");
if (evt.wasPressed(WRButtonEvent.PLUS)) System.out.println("Plus");
 
try {
// 1ボタンで再生
if (evt.wasPressed(WRButtonEvent.ONE)){
if (prebuf != null){
remote.playPrebufferedSound(prebuf, WiiRemote.SF_PCM8S);
}
// 2ボタンでストップ
}else if (evt.wasPressed(WRButtonEvent.TWO)){
remote.requestStatus();
if (remote.isPlayingSound())remote.stopSound();
// +ボタンでボリュームアップ
}else if (evt.wasPressed(WRButtonEvent.PLUS)){
if (remote.isSpeakerEnabled()){
double volume = (remote.getSpeakerVolume()*20+1)/20;
if (volume <= 1)remote.setSpeakerVolume(volume);
System.out.println("Volume: " + remote.getSpeakerVolume());
}
// -ボタンでボリュームダウン
}else if (evt.wasPressed(WRButtonEvent.MINUS)){
if (remote.isSpeakerEnabled()){
double volume = (remote.getSpeakerVolume()*20-1)/20;
if (volume >= 0)remote.setSpeakerVolume(volume);
System.out.println("Volume: " + remote.getSpeakerVolume());
}
}
}catch(Exception e){e.printStackTrace();}
}
};
}