PlayerCanvas.java
上传用户:kyckim
上传日期:2007-12-11
资源大小:332k
文件大小:4k
- package Tonttu;
- import java.io.IOException;
- import javax.microedition.lcdui.Command;
- import javax.microedition.lcdui.CommandListener;
- import javax.microedition.lcdui.Display;
- import javax.microedition.lcdui.Displayable;
- import javax.microedition.lcdui.Canvas;
- import javax.microedition.lcdui.Font;
- import javax.microedition.lcdui.Graphics;
- import javax.microedition.lcdui.Image;
- import javax.microedition.media.Manager;
- import javax.microedition.media.Player;
- import javax.microedition.media.control.VolumeControl;
- public class PlayerCanvas extends Canvas implements CommandListener{
-
- Player player = null;
- private VolumeControl vc = null;
-
- private String musicFile = null;
- int volume = 30;
-
- private Command[] command = new Command[2];
-
- PlayerCanvas() {
- command[0] = new Command("Exit", Command.EXIT, 1);
- command[1] = new Command("File", Command.SCREEN, 2);
- addCommand(command[0]);
- addCommand(command[1]);
- setCommandListener(this);
- }
-
- void getPlayer(String file) {
- try {
- musicFile = file;
- player = Manager.createPlayer(TonttuMIDlet.filerForm.PATH + "/" + musicFile);
- player.realize();
- // player.prefetch();
- repaint();
- vc = (VolumeControl)player.getControl("VolumeControl");
- vc.setLevel(volume);
- player.start();
- repaint();
- } catch(Exception e) {
- closePlayer();
- }
- }
-
- public void paint(Graphics g) {
- int width = getWidth();
- int height = getHeight();
- g.setColor(255, 255, 255);
- g.fillRect(0, 0, width, height);
- g.setColor(0, 0, 0);
- if(player != null) {
- if(player.getState() == Player.PREFETCHED) {
- g.fillTriangle((width/2 - 35), (height/2 - 55) , (width/2 - 35), (height/2 + 25), (width/2 + 35), (height/2 -15));
- } else if(player.getState() == Player.STARTED) {
- g.fillRect((width/2 - 35), (height/2 - 55), 20, 80);
- g.fillRect((width/2 + 15), (height/2 - 55), 20, 80);
- }
- g.setFont(Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_PLAIN,Font.SIZE_SMALL));
- if(musicFile.length() > 26) {
- g.drawString(musicFile.substring(0, 24) + "...", (width - 10), (height - 50), Graphics.RIGHT|Graphics.TOP);
- } else {
- g.drawString(musicFile, (width - 10), (height - 50), Graphics.RIGHT|Graphics.TOP);
- }
- try {
- g.drawImage(Image.createImage("/volume.png"), (width - 40), (height - 29), Graphics.RIGHT|Graphics.TOP);
- } catch(IOException e) {
- }
- g.drawString(Integer.toString(volume/10), (width - 20), (height - 30), Graphics.RIGHT|Graphics.TOP);
- if(volume != 100) {
- g.fillTriangle((width - 14), (height - 29) , (width - 18), (height - 22), (width - 10), (height -22));
- }
- if(volume != 0) {
- g.fillTriangle((width - 14), (height - 13) , (width - 18), (height - 20), (width - 10), (height -20));
- }
- }
- }
-
- void closePlayer() {
- if(player != null) {
- player.close();
- player = null;
- }
- repaint();
- }
-
- public void keyPressed(int keyCode) {
- if(player != null) {
- if(getGameAction(keyCode) == Canvas.FIRE) {
- if(player.getState() == Player.PREFETCHED) {
- try {
- player.start();
- repaint();
- } catch(Exception e) {
- closePlayer();
- }
- } else if(player.getState() == Player.STARTED) {
- try {
- player.stop();
- repaint();
- } catch(Exception e) {
- closePlayer();
- }
- }
- }
- if(getGameAction(keyCode) == Canvas.UP) {
- volume = vc.getLevel();
- volume += 10;
- volume = vc.setLevel(volume);
- repaint();
- }
- if(getGameAction(keyCode) == Canvas.DOWN) {
- volume = vc.getLevel();
- volume -= 10;
- volume = vc.setLevel(volume);
- repaint();
- }
- }
- }
-
- public void commandAction(Command c, Displayable d) {
- if(c == command[0]) {
- closePlayer();
- TonttuMIDlet.tonttuMIDlet.exitApp();
- }
- if(c == command[1]) {
- Display.getDisplay(TonttuMIDlet.tonttuMIDlet).setCurrent(TonttuMIDlet.tonttuMIDlet.filerForm);
- }
- }
- }