⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 playerdemo.java

📁 jBuilderX无线应用开发源代码
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import java.util.*;
import java.io.*;

public class PlayerDemo extends MIDlet implements CommandListener,Runnable {
	private Display   display;
	private Form form;
	private Gauge indicate,volume;
    //程序中用到的命令
    private final static Command play = new Command("Play", Command.ITEM, 1);
    private final static Command stop = new Command("Stop", Command.ITEM, 1);
	private final static Command exit = new Command("Exit", Command.EXIT, 1);

	private Player player=null;
    private VolumeControl vc=null;
    
    private boolean playing;      
    private Thread t;    
    private long mediaLength=0,position=0;

    public PlayerDemo(){
		try {
			//通过资源文件创建播放midi的Player
    		InputStream is = getClass().getResourceAsStream("/music.mid");
			player = Manager.createPlayer(is, "audio/midi");	
    		player.prefetch();
			//从Player中获得VolumeControl
			vc=(VolumeControl)player.getControl("VolumeControl");
			//得到媒体文件的长度
			mediaLength=player.getDuration();
		} 
		catch (IOException ioe){
			ioe.printStackTrace();
		} 
		catch (MediaException me){
			me.printStackTrace();
		}
		//创建指示播放进度的Gauge
		indicate=new Gauge("current",false,(int)(mediaLength/1000000),0);
		indicate.setPreferredSize(150,20);
		//创建调节音量的Gauge
		volume=new Gauge("Volume",true,15,15);
		volume.setPreferredSize(150,20);
		//创建程序主界面,并设置相关命令
		form=new Form("Player Demo");
		form.append(indicate);
		form.append(volume);
		form.addCommand(play);
		form.addCommand(stop);
		form.addCommand(exit);
		form.setCommandListener(this);
    }

    public void startApp() {
		display  = Display.getDisplay(this);	
		display.setCurrent(form);
		try{
			if(player!=null){
				//开始播放
				player.start();	 
				playing=true;
				t=new Thread(this);
				t.start();
			}
		}
		catch(MediaException e){
			e.printStackTrace();
		}
    }

    public void pauseApp() {
		try{
			if(player!=null){
				//暂停播放
				playing=false;
				player.stop();
			}
		}
		catch(MediaException e){
			e.printStackTrace();
		}
	}

    public void destroyApp(boolean unconditional){
		if(player!=null){
			//关闭Player
			player.close();
		}
    }

    public void commandAction(Command c, Displayable s){
		if(c == play) {
			try{  
				//开始播放
				if(player!=null){
					player.start();	 
					playing=true;
					t=new Thread(this);
					t.start();
				}	
			}
			catch(MediaException e){
				e.printStackTrace();
			}
		}
		else if(c == stop) {
			try{ 
				if(player!=null){
					//停止播放
					playing=false;
					player.stop();
				}
			}
			catch(MediaException e){
				e.printStackTrace();
			}
		}
		else if(c == exit) {
			//退出程序
			playing=false;
			if(player!=null){
				player.close();
			}
			destroyApp(false);
			notifyDestroyed();
		}
    }
   
    public void run(){
		while(playing){
			//延时
			try{
				Thread.sleep(100);
			}
			catch(InterruptedException e){
				e.printStackTrace();
			}
			if(vc!=null){
				//设置音量
				vc.setLevel(volume.getValue()*7);
			}
			if(player!=null){
				//设置当前的播放位置
				position=player.getMediaTime();
				if(position!=Player.TIME_UNKNOWN){
					indicate.setValue((int)(position/1000000));
				}
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -