package com.J2MEdev.test;
import Javax.microedition.lcdui
import Javax.microedition.lcdui.Form;
import Javax.microedition.midlet
import Javax.microedition.midlet
import com.nokia.mid.sound.* ;
public class 諾基亞Sound extends MIDlet
{
private Display display;
private Form mainForm;
private int[] supportedFormat;
protected void startApp() throws MIDletStateChangeException
{
initMIDlet();
}
public void initMIDlet()
{
display = Display.getDisplay(this);
mainForm = new Form("諾基亞 Sound");
supportedFormat = Sound.getSupportedFormats();
if (supportedFormat.length == 0)
{
mainForm.append("No audio format supported!");
} else
{
for (int i = 0; i < supportedFormat.length; i++)
{
mainForm.append("" + supportedFormat[i] + " : "
+ Sound.getConcurrentSoundCount(supportedFormat[i]));
}
}
display.setCurrent(mainForm);
}
protected void pauseApp()
{
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException
{
}
}
程序運行在nokia 6108上顯示1:1表現本機支撐單音,並行播放數目也是1。
Sound類有兩個結構器
Sound(byte[] data, int type)
Sound(int freq, long duration)
我們重要講述後者,通過供給應結構器或者init(int freq,long duration)方法適當的頻率和持續時間來初始化聲音對象
Freq off 0
Ring freq A0 220
Ring freq B0b 233
Ring freq B0 247
Ring freq C0 262
Ring freq D0b 277
Ring freq D0 294
Ring freq E0b 311
Ring freq E0 330
Ring freq F0 349
Ring freq G0b 370
Ring freq G0 392
Ring freq A1b 416
Ring freq A1 440
Ring freq B1b 466
Ring freq B1 494
Ring freq C1 523
Ring freq D1b 554
Ring freq D1 587
Ring freq E1b 622
Ring freq E1 659
Ring freq F1 698
Ring freq G1b 740
Ring freq G1 784
Ring freq A2b 831
Ring freq A2 880
Ring freq B2b 932
Ring freq B2 988
Ring freq C2 1047
Ring freq D2b 1109
Ring freq D2 1175
Ring freq E2b 1245
Ring freq E2 1319
Ring freq F2 1397
Ring freq G2b 1480
Ring freq G2 1568
Ring freq A3b 1661
Ring freq A3 1760
Ring freq B3b 1865
Ring freq B3 1976
Ring freq C3 2093
Ring freq D3b 2217
Ring freq D3 2349
Ring freq E3b 2489
Ring freq E3 2637
Ring freq F3 2794
Ring freq G3b 2960
Ring freq G3 3136
Ring freq A4b 3322
Ring freq A4 3520
Ring freq B4b 3729
Ring freq B4 3951
Ring freq C4 4186
Ring freq D4b 4434
Ring freq D4 4698
Ring freq E4b 4978
Ring freq E4 5274
Ring freq F4 5588
Ring freq G4b 5920
Ring freq G4 6272
Ring freq A5b 6644
Ring freq A5 7040
Ring freq B5b 7458
Ring freq B5 7902
Ring freq C5 8372
Ring freq D5b 8870
Ring freq D5 9396
Ring freq E5b 9956
Ring freq E5 10548
Ring freq F5 11176
Ring freq G5b 11840
Ring freq G5 12544
Ring freq A6b 13288
下面我們編寫一個小程序來講述如何應用諾基亞 UI來處理聲音標題,我們的目標是當用戶按下按鍵的時候開端播放聲
package com.J2MEdev.tone;
import Javax.microedition.lcdui.Canvas;
import Javax.microedition.lcdui
import com.nokia.mid.ui.FullCanvas;
public class TonesCanvas extends FullCanvas
{
private TonesMIDlet midlet;
private int pressNum;
private int currentKey;
private TonePlayer player = new TonePlayer();
private static int freq[][] = { { 523, 554 }, { 587, 622 }, { 659, 659 },
{ 698, 740 }, { 784, 831 }, { 880, 932 }, { 988, 988 },
{ 1047, 1109 }, { 1175, 1245 }, { 1319, 1319 }, { 1397, 1480 } };
public static final int NAT = 0;
public static final int SHARP = 1;
private int x = this.getWidth() / 2;
private int y = this.getHeight() / 2;
private boolean sharp = false;
public TonesCanvas(TonesMIDlet midlet)
{
super();
this.midlet = midlet;
// TODO Auto-generated constructor stub
}
public void paint(Graphics g)
{
if (pressNum == -1)
{
return;
}
String note = (sharp == true) ? "#" : "";
g.setColor (128, 255, 18);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(124, 124, 0);
g.drawString(pressNum + note, x, y, Graphics.LEFT | Graphics.TOP);
}
public void keyPressed(int keyCode)
{
if (keyCode == Canvas.KEY_POUND)
{
sharp = !sharp;
} else
{
switch (keyCode)
{
case Canvas.KEY_NUM0:
pressNum = 0;
break;
case Canvas.KEY_NUM1:
pressNum = 1;
break;
case Canvas.KEY_NUM2:
pressNum = 2;
break;
case Canvas.KEY_NUM3:
pressNum = 3;
break;
case Canvas.KEY_NUM4:
pressNum = 4;
break;
case Canvas.KEY_NUM5:
pressNum = 5;
break;
case Canvas.KEY_NUM6:
pressNum = 6;
break;
case Canvas.KEY_NUM7:
pressNum = 7;
break;
case Canvas.KEY_NUM8:
pressNum = 8;
break;
case Canvas.KEY_NUM9:
pressNum = 9;
break;
case Canvas.KEY_STAR:
pressNum = 10;
break;
default:
pressNum = -1;
break;
}
if (pressNum != -1)
{
player.play(freq[pressNum][sharp == false ? NAT : SHARP]);
}
}
currentKey = keyCode;
repaint();
}
public void keyReleased(int keyCode)
{
if (keyCode == currentKey)
{
pressNum = -1;
player.stop();
}
repaint();
}
}
package com.J2MEdev.tone;
import com.nokia.mid.sound.Sound;
public class TonePlayer
{
private Sound sound;
public TonePlayer()
{
super();
sound = new Sound(0, 2000);
}
public void play(int freq)
{
sound.init(freq, 2000);
sound.play(1);
}
public void stop()
{
sound.stop();
}
package com.J2MEdev.tone;
import Javax.microedition.midlet.*;
import Javax.microedition.lcdui.*;
public class TonesMIDlet extends MIDlet
{
private final TonesCanvas canvas;
public TonesMIDlet()
{
canvas = new TonesCanvas(this);
}
public void startApp()
{
Display.getDisplay(this).setCurrent(canvas);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
void exitRequested()
{
destroyApp(false);
notifyDestroyed();
}
}