当前位置: 代码迷 >> J2EE >> 向高手!JMF播放音乐无法执行stop();命令
  详细解决方案

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

热度:563   发布时间:2016-04-22 00:49:32.0
向高手求助!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) {
  相关解决方案