📄 sound.java~2~
字号:
package WealthGod141;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import java.io.*;
public class Sound
{
public static Player player ;
private String filename ;
private String format ;
public Sound(String filename,String format,int loopcount) //传入播放媒体的文件名,格式和是否立刻装载媒体
{
this.format = format;
this.filename = filename;
loadResource(loopcount); //装载资源
}
public void loadResource(int loopcount)
{
try
{
InputStream is = getClass().getResourceAsStream("/"+filename); //装载资源
player = Manager.createPlayer(is ,"audio/"+format); //创建播放器接口
setLoop(loopcount);
}
catch(IOException e)
{
System.out.println("Can't load"+filename);
System.out.println(e);
}
catch(MediaException e)
{
System.out.println("Can't create audio");
System.out.println(e);
}
}
public void setLoop (int loopcount)
{
if (player != null)
{
player.setLoopCount(loopcount) ; //循环
}
}
public void setVolume (int lager)
{
if (player != null)
{
try{
VolumeControl control = (VolumeControl) player.getControl("VolumeCotrol") ;
control.setLevel(lager) ; //设置音量
}
catch(Exception e){}
}
}
public void stop ()
{
if (player != null)
{
try
{
player.stop() ;
}
catch (MediaException e)
{
System.out.println("Can't stop audio") ;
System.out.println(e) ;
}
}
}
public void play ()
{
if (player != null)
{
try
{
player.realize() ;
player.prefetch() ;
player.start() ;
}
catch (MediaException e)
{
System.out.println("Can't play audio") ;
System.out.println(e) ;
}
}
}
public void replay ()
{
close() ;
System.gc() ;
play() ;
}
public void close ()
{
if (player != null)
{
player.close() ;
player = null ;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -