服务器端applet用jmf播放声音文件出错

sanbing 2004-07-31 03:30:46
一个applet例子程序,使用JMF来放语音。
我用Jbuildx在windows xp上编译通过,并可以成功放音。把class/html/wav文件放在web服务器(linux上)上,再在windows Xp上用appletviewer运行出错,客户端在安装jmf4windows的程序前后现象相同。
在windows/linux(web服务器)两个环境下,同一目录下均有class/html/wav三个文件,分别为:SimplePlayerApplet.class/spa.html/test.wav,
Java文件SimplePlayerApplet.java如下:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.lang.String;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.IOException;
import java.util.Properties;
import javax.media.*;
//import com.sun.media.util.JMFSecurity;

/**
* This is a Java Applet that demonstrates how to create a simple
* media player with a media event listener. It will play the
* media clip right away and continuously loop.
*
* <!-- Sample HTML
* <applet code=SimplePlayerApplet width=320 height=300>
* <param name=file value="sun.avi">
* </applet>
* -->
*/
public class SimplePlayerApplet extends Applet implements ControllerListener {

// media Player
Player player = null;
// component in which video is playing
Component visualComponent = null;
// controls gain, position, start, stop
Component controlComponent = null;
// displays progress during download
Component progressBar = null;
boolean firstTime = true;
long CachingSize = 0L;
Panel panel = null;
int controlPanelHeight = 0;
int videoWidth = 0;
int videoHeight = 0;

/**
* Read the applet file parameter and create the media
* player.
*/
public void init() {
//$ System.out.println("Applet.init() is called");
setLayout(null);
setBackground(Color.white);
panel = new Panel();
panel.setLayout( null );
add(panel);
panel.setBounds(0, 0, 320, 240);

// input file name from html param
String mediaFile = null;
// URL for our media file
MediaLocator mrl = null;
URL url = null;

// Get the media filename info.
// The applet tag should contain the path to the
// source media file, relative to the html page.

if ((mediaFile = getParameter("FILE")) == null)
Fatal("Invalid media file parameter");

try {
url = new URL(getDocumentBase(), mediaFile);
mediaFile = url.toExternalForm();
} catch (MalformedURLException mue) {
}

try {
// Create a media locator from the file name
if ((mrl = new MediaLocator(mediaFile)) == null)
Fatal("Can't build URL for " + mediaFile);

/*
try {
JMFSecurity.enablePrivilege.invoke(JMFSecurity.privilegeManager,
JMFSecurity.writePropArgs);
JMFSecurity.enablePrivilege.invoke(JMFSecurity.privilegeManager,
JMFSecurity.readPropArgs);
JMFSecurity.enablePrivilege.invoke(JMFSecurity.privilegeManager,
JMFSecurity.connectArgs);
} catch (Exception e) {}
*/

// Create an instance of a player for this media
try {
player = Manager.createPlayer(mrl);
} catch (NoPlayerException e) {
System.out.println(e);
Fatal("Could not create player for " + mrl);
}

// Add ourselves as a listener for a player's events
player.addControllerListener(this);

} catch (MalformedURLException e) {
Fatal("Invalid media file URL!");
} catch (IOException e) {
Fatal("IO exception creating player for " + mrl);
}

// This applet assumes that its start() calls
// player.start(). This causes the player to become
// realized. Once realized, the applet will get
// the visual and control panel components and add
// them to the Applet. These components are not added
// during init() because they are long operations that
// would make us appear unresposive to the user.
}
...全文
198 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
mituzhishi 2005-07-25
  • 打赏
  • 举报
回复
祝贺楼主,您真是自学成材!!
sanbing 2004-07-31
  • 打赏
  • 举报
回复
搞定了,原因:
1 程序运行需要JMF包inside,所以打成JAR包且里面有JMF程序就可以
2 改为用JAR发布,html需要进行一些修改,在本例中applet标记中要加入archive标记,如下:
<APPLET NAME="SipAppletPhone"
CODE="SimplePlayerApplet"
ARCHIVE="SimplePlayerApplet.jar"
WIDTH=690 HEIGHT=390>
<param name="FILE" value="test.wav">
</APPLET>
标记不能写错,文件名也不能写错,code里的".class"可以写也可以不写
3 好象和安装JMF没有太大的关系,我把JMF uninstall掉了,没有重启,在windows下还可以用appletviewer访问web server上的applet,感觉不应该
sanbing 2004-07-31
  • 打赏
  • 举报
回复
spa.html文件如下:
<html>
<head>
<title>SimplePlayerApplet</title>
</head>
<body>
<APPLET NAME="SipAppletPhone"
CODE="SimplePlayerApplet.class"
WIDTH=690 HEIGHT=390>
<param name="FILE" value="test.wav">
</APPLET>

</body>
</html>
另一文件test.wav为wav格式声音文件
在jbuilderx下可以正常运行(播放声音文件),放在web服务器上,出现以下错误:
C:\Documents and Settings\Owner\jbproject\SimplePlayerApplet\classes>appletviewe
r http://211.71.83.164/test/spa.html
java.lang.NoClassDefFoundError: javax/media/ControllerListener
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:157)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:123)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:561)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:617)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:546)
at sun.applet.AppletPanel.run(AppletPanel.java:298)
at java.lang.Thread.run(Thread.java:534)
用jbuilder打成JAR包后(把jmf打在包里),放在服务器上(并改了spa.html),还是出现同样的错误
如何解决?
sanbing 2004-07-31
  • 打赏
  • 举报
回复
/**
* Start media file playback. This function is called the
* first time that the Applet runs and every
* time the user re-enters the page.
*/

public void start() {
//$ System.out.println("Applet.start() is called");
// Call start() to prefetch and start the player.
if (player != null)
player.start();
}

/**
* Stop media file playback and release resource before
* leaving the page.
*/
public void stop() {
//$ System.out.println("Applet.stop() is called");
if (player != null) {
player.stop();
player.deallocate();
}
}

public void destroy() {
//$ System.out.println("Applet.destroy() is called");
player.close();
}


/**
* This controllerUpdate function must be defined in order to
* implement a ControllerListener interface. This
* function will be called whenever there is a media event
*/
public synchronized void controllerUpdate(ControllerEvent event) {
// If we're getting messages from a dead player,
// just leave
if (player == null)
return;

// When the player is Realized, get the visual
// and control components and add them to the Applet
if (event instanceof RealizeCompleteEvent) {
if (progressBar != null) {
panel.remove(progressBar);
progressBar = null;
}

int width = 320;
int height = 0;
if (controlComponent == null)
if (( controlComponent =
player.getControlPanelComponent()) != null) {

controlPanelHeight = controlComponent.getPreferredSize().height;
panel.add(controlComponent);
height += controlPanelHeight;
}
if (visualComponent == null)
if (( visualComponent =
player.getVisualComponent())!= null) {
panel.add(visualComponent);
Dimension videoSize = visualComponent.getPreferredSize();
videoWidth = videoSize.width;
videoHeight = videoSize.height;
width = videoWidth;
height += videoHeight;
visualComponent.setBounds(0, 0, videoWidth, videoHeight);
}

panel.setBounds(0, 0, width, height);
if (controlComponent != null) {
controlComponent.setBounds(0, videoHeight,
width, controlPanelHeight);
controlComponent.invalidate();
}

} else if (event instanceof CachingControlEvent) {
if (player.getState() > Controller.Realizing)
return;
// Put a progress bar up when downloading starts,
// take it down when downloading ends.
CachingControlEvent e = (CachingControlEvent) event;
CachingControl cc = e.getCachingControl();

// Add the bar if not already there ...
if (progressBar == null) {
if ((progressBar = cc.getControlComponent()) != null) {
panel.add(progressBar);
panel.setSize(progressBar.getPreferredSize());
validate();
}
}
} else if (event instanceof EndOfMediaEvent) {
// We've reached the end of the media; rewind and
// start over
player.setMediaTime(new Time(0));
player.start();
} else if (event instanceof ControllerErrorEvent) {
// Tell TypicalPlayerApplet.start() to call it a day
player = null;
Fatal(((ControllerErrorEvent)event).getMessage());
} else if (event instanceof ControllerClosedEvent) {
panel.removeAll();
}
}

void Fatal (String s) {
// Applications will make various choices about what
// to do here. We print a message
System.err.println("FATAL ERROR: " + s);
throw new Error(s); // Invoke the uncaught exception
// handler System.exit() is another
// choice.

}
}
Java开发技术大全 电子版 第1篇Java基础知识入门. 第1章Java的开发运行环境2 1.1Java的运行环境与虚拟机2 1.2Java的开发环境4 1.2.1JDK的安装4 1.2.2如何设置系统环境变量6 1.2.3编译命令的使用8 1.2.4解释执行命令的使用10 1.2.5UltraEdit的使用11 1.3一个简单的Java应用程序14 1.4一个简单的Java小程序16 1.5本章小结18 第2章Java语言基础19 2.1Java语言的特点19 2.2Java程序的构成21 2.3数据类 型23 2.3.1基本数据类型23 2.3.2常量25 2.3.3变量26 2.3.4整型数据27 .2.3.5浮点型数据29 2.3.6字符型数据30 2.3.7布尔型数据32 2.3.8变量赋初值33 2.3.9变量的作用域34 2.3.10数据类型转换34 2.4运算符与表达式37 2.4.1算术运算符和算术表达式38 2.4.2关系运算符和关系表达式43 2.4.3逻辑运算符和逻辑表达式44 2.4.4条件运算符和条件表达式48 2.4.5位运算符和位运算表达式50 2.4.6赋值运算符和赋值表达式53 2.4.7表达式的求值顺序55 2.5流程控制语句58 2.5.1三种基本控制结构58 2.5.2表达式语句和空语句59 2.5.3块语句60 2.5.4if~else分支语句61 2.5.5多路分支switch~case语句69 2.5.6当型循环while语句71 2.5.7直到型循环do~while语句72 2.5.8当型循环for语句74 2.5.9循环的嵌套78 2.5.10跳转语句break80 2.5.11跳转语句continue82 2.6程序文本的风格84 2.6.1注释84 2.6.2程序的格式编排87 2.7基础语法实战演习88 2.7.1判断闰年88 2.7.2求最大公约数和最小公倍数89 2.7.3Fibonacci数列90 2.7.4逆向输出数字91 2.7.5求水仙花数92 2.7.6输出图形93 2.7.7输出九九口诀表94 2.8本章小结95 第2篇Java面向对象编程 第3章对象和类98 3.1面向对象的基本概念98 3.1.1对象98 3.1.2类99 3.1.3消息101 3.1.4面向对象的4个基本特征101 3.2类与对象104 3.2.1类的基本结构104 3.2.2类的声明104 3.2.3创建类体105 3.2.4对象的生命周期106 3.2.5对象的创建106 3.2.6对象的使用108 3.2.7对象的释放和垃圾收集机制108 3.3成员变量的定义与使用109 3.3.1成员变量的定义109 3.3.2成员变量的访问权限110 3.3.3实例成员变量和静态成员变量114 3.4方法的定义和实现116 3.4.1方法的声明117 3.4.2创建方法体与return语句117 3.4.3局部变量和成员变量的区别119 3.4.4方法的访问权限121 3.5方法的调用121 3.5.1方法调用的形式121 3.5.2方法调用的参数123 3.5.3隐含参数this127 3.6构造方法128 3.6.1无参数构造方法的定义和使用129 3.6.2带参数构造方法的定义和使用131 3.6.3this关键字和构造方法的调用132 3.7静态方法133 3.7.1静态方法的声明和定义134 3.7.2静态方法和实例方法的区别134 3.7.3静态代码块136 3.7.4再论静态成员变量137 3.8main()方法和命令行参数139 3.9结束方法141 3.10本地方法141 3.11本章小结144 第4章继承与多态145 4.1继承的基本原理145 4.2子类对父类的继承146 4.3属性隐藏和方法的覆盖148 4.3.1属性的隐藏148 4.3.2方法的覆盖151 4.4构造方法的继承154 4.5super的使用156 4.5.1用super引用父类的成员156 4.5.2使用super调用父类的构造方法157 4.6继承的内部处理158 4.7多态的基本概念159 4.8重载159 4.8.1普通方法的重载160 4.8.2构造方法的重载161 4.8.3重载的解析163 4.8.4重载与覆盖的区别165 4.9运行时多态165 4.9.1实例方法的运行时多态165 4.9.2成员变量运行时的表现167 4.9.3静态方法运行时

62,628

社区成员

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

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