Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@hisasann
Created March 21, 2009 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hisasann/82871 to your computer and use it in GitHub Desktop.
Save hisasann/82871 to your computer and use it in GitHub Desktop.
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();}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment