soundplayer.java
来自「这个是早期学习时写的RPG游戏 包括地图相关 战斗相关 NPC与 存储等 」· Java 代码 · 共 148 行
JAVA
148 行
package perGame;
import java.io.*;
import javax.microedition.media.*;
public class SoundPlayer
{
private static javax.microedition.media.Player curSound;
static final int MAXSOUND = 0xff;
static int soundGain = MAXSOUND;
private javax.microedition.media.Player sound;
public SoundPlayer(int index)
{
try
{
String a = "";
DataInputStream dis;
//得到声音文件
dis = new DataInputStream(a.getClass().getResourceAsStream("/a" + index));
if(index < 100)
{
sound = Manager.createPlayer(dis, "audio/midi");//声音对象
}
else
{
sound = Manager.createPlayer(dis, "audio/x-wav");
}
}
catch(Exception e)
{
sound = null;
}
}
public static void Stop()
{
/**@todo Implement this sdk.util.Sound abstract method*/
if(curSound == null)
{
return;
}
try
{
curSound.stop();
}
catch(Exception e)
{
}
try
{
curSound.deallocate();
}
catch(Exception e)
{
curSound = null;
}
}
private boolean doForPlay()
{
if(sound == null)
{
return false;
}
if(soundGain <= 0)
{
return false;
}
Stop();
return true;
}
public void loop()
{
if(this.soundGain == 0)
{
return;
}
/**@todo Implement this sdk.util.Sound abstract method*/
if(!doForPlay())
{
return;
}
try
{
sound.setLoopCount(10000);
sound.realize();
sound.prefetch();
sound.start();
curSound = sound;
}
catch(Exception e)
{
}
}
public void play()
{
if(this.soundGain == 0)
{
return;
}
if(curSound != null)
{
if(curSound.getState() == javax.microedition.media.Player.STARTED /*Sound.SOUND_PLAYING*/)
{
return;
}
}
playNew();
}
public void playNew()
{
if(this.soundGain == 0)
{
return;
}
if(!doForPlay())
{
return;
}
try
{
sound.setLoopCount(1);//设置播放的次数
sound.realize();
sound.prefetch();
sound.start();//开始播放声音
curSound = sound;
//sound.play(1);
}
catch(Exception e)
{
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?