`
ktnd
  • 浏览: 17381 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

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. }   

 

分享到:
评论

相关推荐

    Android Programming for Developers -- CODE (2/2)

    1.Mastering the fundamentals of coding Java for Android. 2.Installing and setting up your Android development environment. 3.Building functional user interfaces with the Android Studio visual designer...

    Android Programming for Developers -- CODE (1/2)

    1.Mastering the fundamentals of coding Java for Android. 2.Installing and setting up your Android development environment. 3.Building functional user interfaces with the Android Studio visual designer...

    Runtime.rar_Java编程_Java_

    总结来说,"Runtime.rar"中的"Play a music with java"主题涵盖了Java运行时环境、Java Sound API的使用、异常处理等多个知识点。理解这些内容有助于开发者编写出功能丰富的Java应用程序,包括多媒体处理在内的一...

    ubmp3play:用Java制作的基本MP3播放器,仅用于学习

    4. **音频处理API**:Java Media Framework (JMF) 或 Java Sound API 可能被用到了ubmp3play中,这些API提供了读取、解码和播放音频文件的能力。开发者可以通过它们来控制音量、播放、暂停和停止MP3文件。 5. **...

    Spring In Action-2.1-01-@Component注解

    package soundsystem; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework....

    Swing Hacks

    This chapter covers four different ways to play sound, how to display waveforms, and how to embed MP3 support in your own programs. Chapter 11, Native Integration and Packaging The best software ...

    Android.Games.Practical.Programming.By.Example.Quickstart

    This book offers a step-by-step Android game development guide that's easy to follow with practical tips, illustrations, diagrams, and images, including a full game project explained gradually in each...

    Pro Silverlight2 in CSharp 2008

    create Silverlight pages that have interactive graphics, use vector animations, and play video and sound files. If this all sounds eerily familiar, it’s because the same trick has been tried before. ...

    VB编程资源大全(英文源码 控制)

    With Sound ) A Very Cute Screen Saver ,with source code, If you want more avi files then please mail me.<END><br>36 , superwriter.zip A complete Wordprocessor, with source code. You can save, open ...

    linux howto中文合集

    这个是合集~一般是这里最全的了 另外加的 PDF是英文的 .......中文白皮书.pdf Quota RCS-...

    Random Big Rich Man:垄断类游戏-开源

    Random Big Rich Man(瑞德大富翁) is monopoly-like game using random map to play. It's local game with 4 AI/Human player. Install precondition: Install java 1.8.0 Image or sound from below project or...

Global site tag (gtag.js) - Google Analytics