/*
* localaudioplayer.java
*
* created on 2006年10月9日, 下午10:52
*
* to change this template, choose tools | options and locate the template under
* the source creation and management node. right-click the template and choose
* open. you can then make changes to the template in the source editor.
*/
package netbeans.david.mah_jong;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import java.io.*;
/**
*作者:戴俊华 软件大三的游戏方向学生
*这个类实现了本地音乐资源的播放,以及状态的转换
*同时播放器的参数也可以设置
*希望有用
*转载请带上本地url以及作者
*/
public class localaudioplayer{
public player player;
public string filename;
public string format;
public boolean succ=true;
public localaudioplayer(string filename,string format, boolean loadfile)
{
this.format= format;
this.filename = filename;
if(loadfile)
loadfromeresource();//加载资源
}
public localaudioplayer(string filename, string format)
{
this.format = format;
this.filename =filename;
}

public localaudioplayer()
{
this.format = null;
this.filename =null;
}
public void loadfromeresource() {
try {
inputstream is = getclass().getresourceasstream(filename);///把资源加载进输入流
player = manager.createplayer(is, format);
}
catch(ioexception ex){
succ=false;
}
catch(mediaexception ex){
succ=false;
}
}
public void setloop()//设置一直循环播放
{
if(player!=null)
player.setloopcount(-1);
}
public void setvolume(int level)//音量控制 level范围是0--100
{
if(player!=null){
volumecontrol control = (volumecontrol)player.getcontrol("volumecontrol");
control.setlevel(level);
}
}
public void stop()
{
if(player!=null){
try{
player.stop();
}catch(mediaexception ex){
}
}
}
public void play()
{
if(player!=null){
try{
player.deallocate();//实现播放器的状态装换
player.realize();
player.prefetch();
player.start();
}catch(mediaexception ex){
succ=false;
}
}
}
public void replay()//重新播放一次,先释放资源再加载进入
{
close();
system.gc();//释放
loadfromeresource();//加载
play();
}
public void close()
{
if(player!=null){
player.close();//关闭播放器
player=null;
}
}
}