// 在这里主要实现导入声音后对声音大小和频率的控制。
要播放声音,首先要得到player 接口的实例子
public class GameCan extends Canvas {
private Player player;
private VolumeControl vc;//创建音量的控制;
private TempoControl tc;//创建频率的控制;
private int vcal,tcal;
public GameCan() {
try {
player = Manager.createPlayer(getClass().getResourceAsStream(
"/title.mid"), "audio/midi");//创建接口
player.prefetch();//获取设备;
vc=(VolumeControl) player.getControl("VolumeControl");
vcal= vc.getLevel();//获取并得到音量控制;
tc=(TempoControl) player.getControl("TempoControl");
tcal=tc.getTempo();//获取和得到音频控制;
player.setLoopCount(-1);//声音循环次数,-1代表无限循环;
player.start();//启动声音;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void paint(Graphics g) {
g.setColor(0);//这里可以随便画些什么````
g.fillRect(0, 0, 29, 78);
}
protected void keyPressed(int keyCode){//按键响应;下边的代码大家应该可以理解
int action=getGameAction(keyCode);
switch(action){
case UP:
vcal=Math.min(vcal+10, 100);
vc.setLevel(vcal);
break;
case DOWN:
vcal=Math.max(0, vcal-10);
vc.setLevel(vcal);
break;
case RIGHT:
tcal=Math.min(tc.getMaxRate(), tcal+10000);
tc.setTempo(tcal);
break;
case LEFT:
tcal=Math.max(0, tcal-10000);
tc.setTempo(tcal);
break;
}
switch(keyCode){
case KEY_NUM0:
try {
player.start();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case KEY_NUM1:
try {
player.stop();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case KEY_NUM3:
try {
Manager.playTone(60, 1000, 80);
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}