j2me中如何播放指定的声音文件??

xwei520 2004-09-06 03:46:33
如题;
有完整的程序更好!~
...全文
171 点赞 收藏 5
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
prok 2004-09-07
midp2.0可以
回复
chengfeng 2004-09-07
以下是声音播放代码,不过只支持MIDP2.0,如果是MIDP1.0则要使用厂商提供的开发包了。

import java.io.*;
import java.util.Hashtable;
import java.util.Enumeration;
import javax.microedition.media.*;
import javax.microedition.media.control.*;


public class MediaPlayer implements PlayerListener{
private Player player;
private InputStream is;
private String ctype = null;
private Hashtable players;

// private boolean startFlag = false;
// private boolean stopFlag = false;

private boolean loopPlay = false;
private String mediaUrl = null;
private String volumeLevel = null;
// private String thisTimeUrl;
private String lastTimeUrl = "";
private VolumeControl ctrl;

public MediaPlayer(){
// dThread = new Thread(this);
// dThread.start();
players = new Hashtable();

player = null;


}

public void setMediaLocation(String ml){
if (lastTimeUrl == ml)
{
Player tmpPlayer = (Player) players.get(ml);
if (tmpPlayer != null) {
player = tmpPlayer;
return;
}
}
lastTimeUrl = ml;
//System.out.println("create new");
mediaUrl = ml;
is = getClass().getResourceAsStream(ml);
if (ml.endsWith("wav")) {
ctype = "audio/x-wav";
}
else if(ml.endsWith("mid")){
ctype = "audio/midi";
}
else if(ml.endsWith("mp3")){
ctype = "audio/mpeg";
}
else ctype= "audio/x-tone-seq";

createPlayer();


}
private void createPlayer() {
try {
if (player != null)
{
player.stop();
player.close();
player = null;
System.gc();
}
Player player1 = Manager.createPlayer(is, ctype);
player1.addPlayerListener(this);
player1.realize();
players.put(mediaUrl,player1);
player = player1;
} catch (Exception e) {
System.out.println("createPlayer() "+mediaUrl+" "+e.toString());
e.printStackTrace();
}
}
public void setVolumeLevel(String vl){
volumeLevel = vl;
int level=Integer.valueOf(volumeLevel).intValue()*25;
if(player!=null){
ctrl = (VolumeControl)player.getControl("VolumeControl");
if (ctrl != null)
ctrl.setLevel(level);
}
// else System.out.println("setVolumeLevel:volume control error");
}
private void volumeLevelIs(String vl){
// VolumeControl ctrl;
int level=Integer.valueOf(volumeLevel).intValue()*25;
if(player!=null){
ctrl = (VolumeControl)player.getControl("VolumeControl");
if (ctrl != null)
ctrl.setLevel(level);
}
// else System.out.println("volumeLevelIs:volume control error");
}
public void start(){
try {
player.stop();
// volumeLevelIs(volumeLevel);
player.start();
}
catch (Exception ex){
ex.printStackTrace();
}

}


public void stop(){
try {
if (player != null) {
player.stop();
}
} catch (Exception ex) {System.out.println("stop() "+mediaUrl+" "+ex.toString());ex.printStackTrace();}
}

public void setPlayBackLoop(boolean flag)
{
if (flag==true)
loopPlay = true;
else loopPlay = false;

}

public void setMediaSource(byte abyte0[])
{
}

public void playerUpdate(Player sound, String event, Object eventData) {
if(loopPlay == true)
{
if(event==PlayerListener.END_OF_MEDIA)
start();
}

}

public void closePlayer() {

for (Enumeration e = players.elements() ; e.hasMoreElements() ;)
{
((Player)e.nextElement()).close();
}
players.clear();

}
}
回复
minghuitian 2004-09-06
up
回复
sbamdanb00 2004-09-06
midp1.0没声音,除非你用本地api,可以到各个厂家找文档
回复
xwei520 2004-09-06
UP!~
回复
相关推荐
发帖
J2ME
创建于2007-09-28

1.3w+

社区成员

Java J2ME
申请成为版主
帖子事件
创建了帖子
2004-09-06 03:46
社区公告
暂无公告