中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > JDK核心API
教你如何使用javax.sound.midi包
作者:未知 时间:2005-07-24 21:14 出处:JR 责编:chinaitpower
              摘要:教你如何使用javax.sound.midi包
  1. package lookbook.midi;
  2. import javax.sound.midi.*;
  3. import java.io.*;
  4. import java.net.*;
  5. /**
  6.  * <p>Title: </p>
  7.  * <p>Description: how to use java midi from javax.sound.midi's Package</p>
  8.  * <p>Copyright: Copyright (c) 2003</p>
  9.  * <p>Company: </p>
  10.  * @author lookbook
  11.  * @version 1.0
  12.  */
  13. public class MidiMain {
  14.   private static String midiFile = "town.mid";
  15.   private static String midiURI = "http://hostname/midifile";
  16.   private Sequence sequence =null;
  17.   public MidiMain() {
  18.     this.loadAndPlay();
  19.   }
  20.   public void loadAndPlay(){
  21.     try {
  22.         // From file
  23.         sequence = MidiSystem.getSequence(new File(midiFile));
  24.         // From URL
  25. //        sequence = MidiSystem.getSequence(new URL("http://hostname/midifile"));
  26.         // Create a sequencer for the sequence
  27.         Sequencer sequencer = MidiSystem.getSequencer();
  28.         sequencer.open();
  29.         sequencer.setSequence(sequence);
  30.         //Determining the Duration of a Midi Audio File
  31.         double durationInSecs = sequencer.getMicrosecondLength() / 1000000.0;
  32.         System.out.println("the duration of this audio is "+durationInSecs+"secs.");
  33.         //Determining the Position of a Midi Sequencer
  34.         double seconds = sequencer.getMicrosecondPosition() / 1000000.0;
  35.         System.out.println("the Position of this audio is "+seconds+"secs.");
  36.         //Setting the Volume of Playing Midi Audio
  37.         if (sequencer instanceof Synthesizer) {
  38.                 Synthesizer synthesizer = (Synthesizer)sequencer;
  39.                 MidiChannel[] channels = synthesizer.getChannels();
  40.                 // gain is a value between 0 and 1 (loudest)
  41.                 double gain = 0.9D;
  42.                 for (int i=0; i<channels.length; i++) {
  43.                     channels[i].controlChange(7, (int)(gain * 127.0));
  44.                 }
  45.             }
  46.         // Start playing
  47.         sequencer.start();
  48.         //Determining the Position of a Midi Sequencer
  49.         Thread.currentThread().sleep(5000);
  50.         seconds = sequencer.getMicrosecondPosition() / 1000000.0;
  51.         System.out.println("the Position of this audio is "+seconds+"secs.");
  52.         //Add a listener for meta message events
  53.         sequencer.addMetaEventListener(
  54.         new MetaEventListener() {
  55.             public void meta(MetaMessage event) {
  56.                 // Sequencer is done playing
  57.                 if (event.getType() == 47) {
  58.                     System.out.println("Sequencer is done playing.");
  59.                 }
  60.             }
  61.         });
  62.     }catch (MalformedURLException e) {
  63.     }catch (IOException e) {
  64.     }catch (MidiUnavailableException e) {
  65.     }catch (InvalidMidiDataException e) {
  66.     }catch (InterruptedException e){
  67.     }
  68.   }
  69.   public static void main(String[] args) {
  70.     MidiMain midi = new MidiMain();
  71.   }
  72. }
关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有