posts - 32,comments - 8,trackbacks - 0

Oops! JMF Quick Start

 

Purpose:

学习完后能够学会操作JMF.

JMFjava media framework,能够控制流媒体

 

Reference :

http://blog.csdn.net/oscar999/archive/2006/12/11/1438694.aspx

 

Precondition:

Eclipse 3.3 europa

jmf-2_1_1e-windows-i586.exe

/Files/pixysoft/jmf-2_1_1e-windows-i586.part1.rar 
/Files/pixysoft/jmf-2_1_1e-windows-i586.part2.rar 
/Files/pixysoft/jmf-2_1_1e-windows-i586.part3.rar 
/Files/pixysoft/jmf-2_1_1e-windows-i586.part4.rar 



Quick Start:

新建一个java project,项目名为Oops_JMF

 

在项目里面添加一个lib目录,并添加以下jar文件,全部可以在jmf-2_1_1e-windows-i586.exe里面找到



 

src目录下面添加以下文件:

SimpleAudioPlayer.java

import javax.media.*;

import java.io.File;

import java.io.IOException;

import java.net.URL;

import java.net.MalformedURLException;

 

public class SimpleAudioPlayer

{

       
private Player audioPlayer = null;

 

       
public SimpleAudioPlayer(URL url) throws IOException, NoPlayerException,

                     CannotRealizeException

       {

              audioPlayer 
= Manager.createRealizedPlayer(url);

       }

 

       
public SimpleAudioPlayer(File file) throws IOException, NoPlayerException,

                     CannotRealizeException

       {

              
this(file.toURL());

       }

 

       
public void play()

       {

              audioPlayer.start();

       }

 

       
public void stop()

       {

              audioPlayer.stop();

              audioPlayer.close();

       }

}

 

TestCase.java

import java.io.File;

import java.io.IOException;

 

import javax.media.CannotRealizeException;

import javax.media.NoPlayerException;

 

public class TestCase

{

 

       
/**

        * 
@param args

        
*/

       
public static void main(String[] args)

       {

              File audioFile 
= new File("demo.mp3");

              
try

              {

                     SimpleAudioPlayer player 
= new SimpleAudioPlayer(audioFile);

                     System.out.println(
"music begin");

                     player.play();

                     System.out.println(
"music end");

 

              } 
catch (NoPlayerException e)

              {

                     
// TODO Auto-generated catch block

                     e.printStackTrace();

              } 
catch (CannotRealizeException e)

              {

                     
// TODO Auto-generated catch block

                     e.printStackTrace();

              } 
catch (IOException e)

              {

                     
// TODO Auto-generated catch block

                     e.printStackTrace();

              }

 

       }

 

}




在项目根目录下面放置一个demo.mp3文件,最后整个项目变成:

 

 

右键点击项目,run as java application



 

设置好运行环境



 

成功!

 

发现很有趣。整个application运行完了,但是音乐还在继续。估计内部开了线程。


posted on 2007-09-07 14:56 张辰 阅读(897) 评论(0)  编辑  收藏 所属分类: Dr. Oops

只有注册用户登录后才能发表评论。


网站导航: