扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
在本页阅读全文(共3页)
public void playSound(EMSound sound, int count) {
if (!soundEnable) {
return;
}
try { //colico
if (soundPlaying) {
stopSound();
}
if (soundPlayer == null) {
soundPlayer = new Sound(sound.data, sound.type);
soundPlayer.setSoundListener(soundListener);
currentSound = null;
}
if (sound != currentSound) {
soundPlayer.release();
soundPlayer.init(sound.data, sound.type);
currentSound = sound;
}
soundPlayer.play(count);
}
catch (Exception ex) {
soundPlaying = false;
}
}
Sound[] soundPlayers;
public void playSound( EMSound sound[], int loc)
{
if (!soundEnable) { return; }
try {
if (soundPlaying) stopSound();
if (soundPlayers == null) {
soundPlayers = new Sound[sound.length];
System.out.println("Sounds == null");
for (int i=0; i<sound.length ; i++ ){
soundPlayers[i] = new Sound( sound[i].data, sound[i].type );
soundPlayers[i].setSoundListener( soundListener );
soundPlayers[i].init(sound[i].data, sound[i].type);
}
}
long now = System.currentTimeMillis();
soundPlayers[loc].play(1);
System.out.println("playing Sounds");
System.out.println("playing Sounds time"+(System.currentTimeMillis()-now) );
} catch(Exception ex) {
soundPlaying = false;
}
}
public void stopSound() {
if (!soundEnable) {
return;
}
if (soundPlayer != null) { //colico
soundPlayer.stop();
}
}
public boolean isSoundPlaying() {
return soundPlaying;
}
public boolean isSoundEnable() {
return soundEnable;
}
public void setSoundEnable(boolean e) {
if (!e) {
stopSound();
}
soundEnable = e;
}
在V300中
1).
public class EMSound
{
public String type;
public byte[] data;
public EMSound(byte[] data, String type)
{
this.type = type;
this.data = data;
}
}
2).
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.Manager;
import javax.microedition.media.Control.*;
3). //Sound soundPlayer;
PlayerListener soundListener = new EMSoundListener();
Player soundPlayer;
EMSound currentSound = null;
boolean soundPlaying = false;
boolean soundEnable = true;
class EMSoundListener
implements PlayerListener {
public void playerUpdate(Player player, String event, Object eventData) { //soundStateChanged(int event)
if (event == PlayerListener.STOPPED) {
soundPlaying = false;
}
if (event == PlayerListener.STARTED) {
soundPlaying = true;
}
}
}
public EMSound loadSound(String resfile, int resID) {
EMSound sound;
try {
InputStream is = getClass().getResourceAsStream(resfile + "/" + resID +
".mid");
int len = (int) is.skip(10000);
is.close();
is = getClass().getResourceAsStream(resfile + "/" + resID + ".mid");
byte[] barr = new byte[len];
is.read(barr);
is.close();
sound = new EMSound(barr, "audio/midi");
}
catch (Exception ex) {
sound = null;
}
return sound;
}
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者