...

显示标签为“实验”的博文。显示所有博文
显示标签为“实验”的博文。显示所有博文

2008年5月2日星期五

java多媒体学习初探【原】

最近学习java多媒体编程 就安装了jmf(java media framework,java媒体架构)
安装很顺利,一点障碍都没有(呵呵,这点还是比较自豪的)
再接着我就到网上找示例代码,很顺利找到了几个简单的例子!
很简单,是人都能看懂(当然了,我是说有java基础的)
于是,我选择了一个貌似比较正确的拿来运行
呵呵 美中不足的是它还是有缺陷,就是那个累没有实现javax.media.ControllerListener接口的方法
这点对于我来说比较简单,加上一个空方法public void controllerUpdate(ControllerEvent ss)
这个可爱的编译程序就不找我麻烦了!不错还是比较和蔼 呵呵
Process completed.(处理完成---呵呵,我也显摆一下我的English)
这是每个编程序的人最喜欢的讯息 呵呵 好消息(No news is good news--这是我们的期待哈)
Unable to handle format(来消息了,坏消息!)--不能操纵的格式!
我就去查格式表,因为我输入的文件格式是avi(由于历史原因,这个格式有很多种编码格式),格式表中是支持这个格式的
我想是不是 代码还有问题?我又检查代码,对照着帮助文档(英文的--我应该自豪一下),那个javax.media.Player的实例player没有执行start()方法,于是我有美滋滋的加上这个方法,呵呵 谢天谢地
我还是看见了成功之母
看来问题就出在这个avi上了,以前接触过这种文件(有兴趣的去查查 呵呵)
我就换了个mp3文件,哇!
我听到了美妙的歌声(才知道什么叫天籁!!)
我又将那个start()方法屏蔽掉
在运行一下!呵呵 果然这个方法是必须的!!必须的!!呵呵
下面是源码:
安全通过!!

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.PrefetchCompleteEvent;
import javax.media.RealizeCompleteEvent;
import javax.media.Time;
public class JMFSample implements ControllerListener {
public static void main(String[] args) {
JMFSample sp = new JMFSample();
sp.play();
}

private Frame f;
private Player player;
private Panel panel;
private Component visual;
private Component control = null;

public void controllerUpdate(ControllerEvent ss){//一开始这个方法没有被实现!呵呵
}

public void play(){
f = new Frame("JMF Sample1");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
if(player != null) {
player.close();
}
System.exit(0);
}
});
f.setSize(500,400);
f.setVisible(true);
URL url = null;
try {
//准备一个要播放的视频文件的URL
url = new URL("file:E:/abc.mp3");
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
//通过调用Manager的createPlayer方法来创建一个Player的对象
//这个对象是媒体播放的核心控制对象
player = Manager.createPlayer(url);
} catch (NoPlayerException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
//对player对象注册监听器,能噶偶在相关事件发生的时候执行相关的动作
player.addControllerListener(this);
//让player对象进行相关的资源分配
player.realize();
player.start();//这个是必须的!!!
}
}

2008年4月22日星期二

Socket通信实验-经验总结【原】

最近又练习了一下sokect通信的程序 吃了点苦头 但又感觉到一丝甜蜜呵呵 得到一点经验 那就是要习惯用flush()几乎所有的输出流都有flush() 我太菜了 !!不用这个方法出现的情况就是:
1、你的程序没有编译错误
2、你的程序不会抛出任何异常
3、你的程序只会在readLine()处等待,让你不知所措!!
下面是服务器的代码:
/**
* @(#)NetServer.java
*
*
* @author
* @version 1.00 2008/4/20
*/
package samples;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.InetAddress;
public class NetServer extends ServerSocket implements Runnable{
public NetServer() throws java.io.IOException{
super();
}
public NetServer(int port) throws java.io.IOException{
super(port);

}

public void decide(){//判断得到命令该执行哪段程序
}
public int read(Socket client){//读入来自客户端的命令并将其转化为相应的整型变量
int cmd=0;
String scmd="";
try{
System.out.println("Begin To Read");
java.io.BufferedReader in=new java.io.BufferedReader(
new java.io.InputStreamReader(
client.getInputStream()
)
);

if(in.ready()==false){//输入流是否准备好!
scmd=in.readLine();
}
System.out.println("From client:"+scmd);
cmd=1;
}catch(java.io.IOException ioe){
System.out.println(ioe.toString());
}
return cmd;
}
public void run(){
int j=6;

try{

Socket client;
System.out.println("this is server running:");
client=this.accept();//等待客户端
while(true){
if(client!=null){
System.out.println("reading……");
this.read(client);//读取来自客户端的信息
}

j--;


new Thread(this).sleep(0);
}
}catch(InterruptedException e){
System.out.println(e.toString());
}catch(java.io.IOException ioe){
System.out.println(ioe.toString());
}

}
public static void main(String args[]){
try{
NetServer ns=new NetServer(3333);
new Thread(ns).start();//启动服务器
}catch(java.io.IOException ioe){
System.out.println(ioe.toString());
}
}
}

下面是客户端的代码:
/**
* @(#)NetClient.java
*
*
* @author
* @version 1.00 2008/4/20
*/
package samples;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.InetAddress;
import java.net.SocketAddress;
import java.io.BufferedReader;
public class NetClient extends Socket implements Runnable{
public NetClient() {
super();
}
public NetClient(java.net.InetAddress address, int port) throws java.io.IOException {
super(address,port);
}
public NetClient(InetAddress address, int port, InetAddress localAddr, int localPort) throws java.io.IOException{
super(address,port,localAddr,localPort);
}
public void linkToServer(SocketAddress server){
try{
this.connect(server);
}catch(java.io.IOException ioe){
System.out.println(ioe.toString());
}catch(java.nio.channels.IllegalBlockingModeException ibm){
System.out.println(ibm.toString());
}catch(java.lang.IllegalArgumentException iae){
System.out.println(iae.toString());
}

}
public String getCMD(){//读取用户输入的命令,其实就是一段字符串 呵呵

System.out.println("Please input your Command:");
try{
//这里先假设程序能正确读入命令;并且用户也不是傻子--不输命令;
BufferedReader i= new BufferedReader(new java.io.InputStreamReader(System.in));
String s=i.readLine();
System.out.println("s:"+s);
return s;//new BufferedReader(new java.io.InputStreamReader(System.in)).readLine();
}catch(java.io.IOException ioe){
System.out.println(ioe.toString());
System.out.println("Please input your Command twice:");
return "error";
}
}
public void toServer(String cmd){//将命令发往服务端
try{
java.io.PrintWriter pw=new java.io.PrintWriter(
new java.io.OutputStreamWriter(this.getOutputStream())
);
pw.println(cmd);
pw.flush();//就是这个flush()让我郁闷了好几天!!
}catch(java.io.IOException ioe){
System.out.println(ioe.toString());
}
}
public void run(){
int i=6;
while(i>=0){
this.toServer(this.getCMD());//将命令发往服务器
try{
new Thread(this).sleep(1000);
}catch(InterruptedException e){
System.out.println(e.toString());
}
}
}
public static void main(String args[]){
try{
//启动客户端,并连接至指定端口
NetClient nc=new NetClient(java.net.InetAddress.getLocalHost(),3333);//,java.net.InetAddress.getLocalHost(),2222);
new Thread(nc).start();
}catch(java.io.IOException ioe){
System.out.println(ioe.toString());
}
}
}

2008年4月20日星期日

多线程的实验【原】

分别使用了Thread类 Runnable接口实现相同的计算过程:
下面这段代码是用Thread类实现:
/**
* @(#)MThread.java
*
*
* @author
* @version 1.00 2008/4/19
*/

public class MThread extends Thread {
private static int i=0;
private int num;
private int flag;
public MThread() {
new MThread(0);
}
public MThread(int flag){
this.num=++i;
this.flag=flag;
}
public void run(){
while(this.flag>=0){
System.out.println("The Thread "+this.num+":"+this.flag);
this.flag--;
try{
this.sleep((new java.util.Random()).nextInt(1000));
}catch(InterruptedException e){
System.out.println(e.toString());
}
}
}
public static void main(String args[]){
MThread mt0=new MThread(10);
MThread mt1=new MThread(20);
MThread mt2=new MThread(30);
mt0.start();
mt1.start();
mt2.start();

}
}
下面这段代码用Runnable接口实现:
/**
* @(#)cRunnable.java
*
*
* @author
* @version 1.00 2008/4/19
*/

public class cRunnable implements Runnable{
private static int i=0;
private int num;
private int flag;
public cRunnable() {
}
public cRunnable(int flag){
this.num=++i;
this.flag=flag;
}
public void run(){
while(this.flag>=0){
System.out.println("The Thread "+this.num+":"+this.flag);
this.flag--;
try{
new Thread(this).sleep((new java.util.Random()).nextInt(1000));
}catch(InterruptedException e){
System.out.println(e.toString());
}
}
}
public static void main(String args[]){
cRunnable cr0=new cRunnable(10);
cRunnable cr1=new cRunnable(10);
cRunnable cr2=new cRunnable(10);
new Thread(cr0).start();
new Thread(cr1).start();
new Thread(cr2).start();
}
}
呵呵是不是很无聊 这么简单的程序还拿出来炫耀!!
我果真很无聊啊!!