首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

向高手!JMF播放音乐无法执行stop();命令

2012-09-28 
向高手求助!JMF播放音乐无法执行stop()命令!package TESTimport javax.media.*import java.io.Fileimp

向高手求助!JMF播放音乐无法执行stop();命令!
package TEST;
import javax.media.*;
import java.io.File;
import java.io.IOException;

public class SimpleAudioPlayer {
  private Player audioPlayer = null;
   

   
  public SimpleAudioPlayer(MediaLocator locator) 
  throws IOException,NoPlayerException,CannotRealizeException 
  {
  audioPlayer = Manager.createRealizedPlayer(locator); }
   
  public void play() {
  audioPlayer.prefetch();
  System.out.println("Run Prefetch");
  audioPlayer.start();
  System.out.println("Run start");
  }
   
  public void stop() {
  System.out.println("Run Stop");
  audioPlayer.close();
  System.out.println("Run CLOSE");
  }
   
  public static void main(String[] args){
  File audioFile = new File("C:\\WINDOWS\\Media\\chimes.wav");
  System.out.println("File exist is "+audioFile.exists());
  MediaLocator locator=new MediaLocator("file:"+audioFile.getAbsolutePath());
  System.out.println("Locator is: "+locator);
  try {
  SimpleAudioPlayer p1 = new SimpleAudioPlayer(locator);
   
  p1.play();
  p1.stop();
   
  } catch (Exception e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  } 
  }
   
   
}
-
运行后console输出的结果:

File exist is true
Locator is: file:C:\WINDOWS\Media\chimes.wav
Run Prefetch
Run start
Run Stop
Error: Unable to prefetch com.sun.media.PlaybackEngine@f5da06

Run CLOSE

但如果我屏蔽掉p1.stop();就可以正确播放声音

为什么我无法停止播放音乐文件呢?谢谢各位啊!!

[解决办法]
格式不对?
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 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:/c-d.avi");
} 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();
}
  
private int videoWidth = 0;
private int videoHeight = 0;
private int controlHeight = 30;
private int insetWidth = 10;
private int insetHeight = 30;
  
//监听player的相关事件
public void controllerUpdate(ControllerEvent ce) {
if (ce instanceof RealizeCompleteEvent) {
//player实例化完成后进行player播放前预处理
player.prefetch();
} else if (ce instanceof PrefetchCompleteEvent) {
if (visual != null)
return;

//取得player中的播放视频的组件,并得到视频窗口的大小
//然后把视频窗口的组件添加到Frame窗口中,
if ((visual = player.getVisualComponent()) != null) {
Dimension size = visual.getPreferredSize();
videoWidth = size.width;
videoHeight = size.height;
f.add(visual);
} else {
videoWidth = 320;
}

//取得player中的视频播放控制条组件,并把该组件添加到Frame窗口中
if ((control = player.getControlPanelComponent()) != null) {
controlHeight = control.getPreferredSize().height;
f.add(control, BorderLayout.SOUTH);
}

//设定Frame窗口的大小,使得满足视频文件的默认大小
f.setSize(videoWidth + insetWidth, videoHeight + controlHeight + insetHeight);
f.validate();

//启动视频播放组件开始播放
player.start();
} else if (ce instanceof EndOfMediaEvent) {
//当播放视频完成后,把时间进度条恢复到开始,并再次重新开始播放
player.setMediaTime(new Time(0));
player.start();
}
}

}

热点排行