语音播放器

mjjzg 2009-07-14 10:21:39
用java如何调用window自带的Windows Media Player播放器并播放一文件

用java写个语音播放器
谁能给个源码或是例子,高分相送,不胜感激
...全文
367 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
liyizhong1235 2009-11-14
  • 打赏
  • 举报
回复
楼上的播放器都不兼容的。。。。。麻烦搞个兼容的。可以不?
奸商德 2009-07-16
  • 打赏
  • 举报
回复

<OBJECT classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95"
codeBase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701"
height="256" id="MIDIPlayer" name="wmplayer"
type="application/x-oleobject"
width="320"
standby="加载 Microsoft Windows Media Player 组件..." VIEWASTEXT>
<PARAM NAME="src" VALUE="你要播放的文件">
<PARAM NAME="AutoStart" VALUE="true">
<PARAM NAME="AutoRewind" VALUE="true">
<PARAM NAME="AnimationAtStart" VALUE="false">
<PARAM NAME="ShowControls" VALUE="true">
<PARAM NAME="ClickToPlay" VALUE="true">
<PARAM NAME="EnableContextMenu" VALUE="true">
<PARAM NAME="EnablePositionControls" VALUE="true">
<PARAM NAME="Balance" VALUE="0">
<PARAM NAME="ShowStatusBar" VALUE="true">
<PARAM NAME="AutoSize" VALUE="0">
<PARAM NAME="PlayCount" VALUE="0">
<embed type="application/x-mplayer2" src="你要播放的文件" autostart="true" height="256" width="320">
</embed>
</OBJECT>

diggywang 2009-07-16
  • 打赏
  • 举报
回复
楼上的楼上,根本不靠谱,人家要Windows里的媒体播放器好不好?
我还是推荐DJNativeSwing,里面集成了浏览器、媒体播放器、Flash等常用组件。
超维电脑科技 2009-07-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 Mr_Von 的回复:]
LZ可以去 http://www.oschina.net/project/lang/19?tag=96 看一下, 這里有一些用java開發的播放器...
[/Quote]
好网站
蹭点分
無名VF 2009-07-15
  • 打赏
  • 举报
回复
LZ可以去 http://www.oschina.net/project/lang/19?tag=96 看一下, 這里有一些用java開發的播放器...
  • 打赏
  • 举报
回复

//调用Windows播放器
Runtime.getRuntime().exec(String cmd);
import java.io.IOException;
public class TestExecPlayer {
public static void main(String args[]){
String thePlayerPath = "C: \\Program Files \\Windows Media Player \\wmplayer.exe"; /*播放器的路径*/
try{
Process ps = Runtime.getRuntime().exec(thePlayerPath);
}catch (IOException e){ e.printStackTrace();
}
}
}
  • 打赏
  • 举报
回复
以下是声音播放代码,不过只支持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();

}
}
或者 直接调用windows自带的播放器

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧