Play sound with java.

java 代码
  1. import java.io.IOException;   
  2. import java.net.MalformedURLException;   
  3.   
  4. import javax.media.ControllerEvent;   
  5. import javax.media.ControllerListener;   
  6. import javax.media.EndOfMediaEvent;   
  7. import javax.media.Manager;   
  8. import javax.media.MediaLocator;   
  9. import javax.media.NoPlayerException;   
  10. import javax.media.Player;   
  11. import javax.media.Time;   
  12.   
  13. public class SimplePlayer implements ControllerListener {   
  14.   
  15.     // media Player   
  16.     Player player = null;   
  17.   
  18.     public synchronized void controllerUpdate(ControllerEvent event) {   
  19.   
  20.         if (player == null)   
  21.             return;   
  22.   
  23.         if (event instanceof EndOfMediaEvent) {   
  24.             player.setMediaTime(new Time(0));   
  25.             //player.close();   
  26.         }   
  27.     }   
  28.   
  29.     public SimplePlayer() {   
  30.   
  31.         String mediaFile = "file:\\D:\\tencent\\qq\\tmsound\\Global.wav";   
  32.         MediaLocator mrl = null;   
  33.   
  34.         try {   
  35.             // Create a media locator from the file name   
  36.             if ((mrl = new MediaLocator(mediaFile)) == null)   
  37.                 Fatal("Can't build URL for " + mediaFile);   
  38.   
  39.             // Create an instance of a player for this media   
  40.             try {   
  41.                 player = Manager.createPlayer(mrl);   
  42.             } catch (NoPlayerException e) {   
  43.                 System.out.println(e);   
  44.                 Fatal("Could not create player for " + mrl);   
  45.             }   
  46.   
  47.             player.addControllerListener(this);   
  48.   
  49.         } catch (MalformedURLException e) {   
  50.             Fatal("Invalid media file URL!");   
  51.         } catch (IOException e) {   
  52.             Fatal("IO exception creating player for " + mrl);   
  53.         }   
  54.   
  55.     }   
  56.   
  57.     public void start() {   
  58.         if (player != null)   
  59.             player.start();   
  60.     }   
  61.   
  62.     void Fatal(String s) {   
  63.         System.err.println("FATAL ERROR: " + s);   
  64.         throw new Error(s);   
  65.     }   
  66.   
  67.     public static void main(String[] args) throws Exception {   
  68.         SimplePlayer sp = new SimplePlayer();   
  69.         sp.start();   
  70.     }   
  71. }   

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值