📄 audiocanvas.java
字号:
package com.nokia.example.mmapi.mediasampler.viewer;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.media.MediaException;
import com.nokia.example.mmapi.mediasampler.MediaSamplerMIDlet;
import com.nokia.example.mmapi.mediasampler.data.Media;
import com.nokia.example.mmapi.mediasampler.data.MediaFactory;
import com.nokia.example.mmapi.mediasampler.model.PlayerPool;
/**
* Audio play canvas. Audio clip is played when a key is pressed.
*/
public class AudioCanvas extends Canvas implements CommandListener
{
private PlayerPool pool;
private MediaSamplerMIDlet midlet;
private Displayable returnScreen;
protected String[] supportedMediaNames;
protected String[] unsupportedMediaNames;
protected int countOfPlayers = 0;
protected int volume = 100;
private boolean initDone;
private boolean infoMode;
private Command infoCommand = new Command("Info", Command.SCREEN, 1);
private Command backCommand = new Command("Back", Command.BACK, 1);
public AudioCanvas(MediaSamplerMIDlet midlet, Displayable returnScreen,
int latency)
{
this.midlet = midlet;
this.returnScreen = returnScreen;
pool = new PlayerPool(midlet, latency);
initSounds();
// Init volume level of Players in pool
pool.setVolumeLevel(volume);
addCommand(backCommand);
addCommand(infoCommand);
setCommandListener(this);
}
/**
* Release loaded resources
*/
public void releaseResources()
{
pool.releaseResources();
}
/**
* Implemented CommandListener method.
*/
public void commandAction(Command cmd, Displayable d)
{
if (cmd == backCommand)
{
if (infoMode)
{
infoMode = false;
addCommand(infoCommand);
repaint();
}
else
{
Display.getDisplay(midlet).setCurrent(returnScreen);
}
}
else if (cmd == infoCommand)
{
infoMode = true;
removeCommand(infoCommand);
repaint();
}
}
public void keyPressed(int key)
{
int keyCode = key - KEY_NUM0;
int gameAction = getGameAction(key);
// Check is the selected audio available for playing.
if (keyCode > 0 && keyCode <= countOfPlayers)
{
try
{
pool.playSound(keyCode - 1);
}
catch (MediaException e)
{
// Swallow the exception.
}
}
else if (gameAction == UP)
{
increaseVolume();
}
else if (gameAction == DOWN)
{
decreaseVolume();
}
}
/**
* Paint the canvas.
*/
protected void paint(Graphics g)
{
int x = 0;
int y = 0;
int w = getWidth();
int h = getHeight();
g.setColor(0xFFFFFF);
g.fillRect(x, y, w, h);
Font fontPlain = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_SMALL);
Font fontBold = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
Font.SIZE_SMALL);
g.setColor(0x000000);
if (infoMode)
{
boolean multiSupport = pool.supportsMulplePlayers();
boolean mixSupport = "true".equals(System
.getProperty("supports.mixing"));
g.setFont(fontBold);
y = paintTextRow(g, "Supports multiple players:", x, y);
g.setFont(fontPlain);
y = paintTextRow(g, "" + multiSupport, x, y);
g.setFont(fontBold);
y = paintTextRow(g, "Supports audio mixing:", x, y);
g.setFont(fontPlain);
y = paintTextRow(g, "" + mixSupport, x, y);
if (unsupportedMediaNames.length > 0)
{
g.setFont(fontBold);
g.setColor(0x000000);
y = paintTextRow(g, "Unsupported sounds:", x, y);
g.setFont(fontPlain);
for (int i = 0; i < unsupportedMediaNames.length; i++)
{
String str = unsupportedMediaNames[i];
String strToPaint = str;
y = paintTextRow(g, strToPaint, x, y);
}
}
}
else
{
g.setFont(fontBold);
y = paintTextRow(g, "Sound key mapping:", x, y);
g.setFont(fontPlain);
for (int i = 0; i < supportedMediaNames.length; i++)
{
String str = supportedMediaNames[i];
String strToPaint = (i + 1) + " = " + str;
y = paintTextRow(g, strToPaint, x, y);
}
y = paintTextRow(g, "Volume level: " + volume, x, y);
}
}
/**
* Renders a text row to Canvas.
*/
private int paintTextRow(Graphics g, String text, int x, int y)
{
int w = getWidth();
Font font = g.getFont();
for (int j = 0; j < text.length(); j++)
{
char c = text.charAt(j);
int cw = font.charWidth(c);
if (x + cw > w)
{
x = 0;
y += font.getHeight();
}
g.drawChar(c, x, y, Graphics.TOP | Graphics.LEFT);
x += cw;
}
y += font.getHeight();
return y;
}
private void increaseVolume()
{
volume += 10;
if (volume > 100)
{
volume = 100;
}
pool.setVolumeLevel(volume);
repaint();
}
private void decreaseVolume()
{
volume -= 10;
if (volume < 0)
{
volume = 0;
}
pool.setVolumeLevel(volume);
repaint();
}
/**
* Loads the medias available on this canvas. Loaded sounds are passed to
* PlayerPool class which creates players and initializes player states.
*/
protected void initSounds()
{
if (!initDone)
{
countOfPlayers = 0;
Vector supportedMedias = new Vector();
Vector unsupportedMedias = new Vector();
// Sound media clips
Media[] medias = MediaFactory.getSoundMedias();
for (int i = 0; i < medias.length; i++)
{
Media media = medias[i];
String mediaName = null;
try
{
mediaName = media.getFile() + " [" + media.getType() + "]";
pool.addMedia(media);
supportedMedias.addElement(mediaName);
countOfPlayers++;
}
catch (MediaException e)
{
System.out.println("unsupported media: " + media.getType());
unsupportedMedias.addElement(mediaName);
}
}
// Tone sequences
String mediaName = null;
try
{
mediaName = "Tone sequence";
pool.addToneSequence(MediaFactory.getToneSequence());
supportedMedias.addElement(mediaName);
countOfPlayers++;
}
catch (MediaException e)
{
System.out.println("Unsupported type: Tone sequence");
e.printStackTrace();
unsupportedMedias.addElement(mediaName);
}
// Stores results of success and failed Players to String array
supportedMediaNames = new String[supportedMedias.size()];
supportedMedias.copyInto(supportedMediaNames);
unsupportedMediaNames = new String[unsupportedMedias.size()];
unsupportedMedias.copyInto(unsupportedMediaNames);
initDone = true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -