技术开发 频道

用Java调用VC音量控制程序

  【IT168 技术文档】

  前言

  本文通过Java的Runtime接口来实现调用其他语言实现的应用程序,进而来实现对计算机硬件信息的监控和控制。本文是多媒体信息系统的一个部分,就是调整计算机音量。

  使用VC编写音量控制程序

  本控制示例使用VC6.0编写,主要是调用系统的API来实现,

  mixerGetLineInfo 获取Master Volume Control.

  mixerGetControlDetails 获取 Volume Control 信息

  mixerSetControlDetails 设置 Volume Control 信息

  最终编译成Console类型的Dos执行的程序VolumeControl.exe。这个程序实现三个功能:

  1.获取音量 VolumeControl.exe 0

  2.增加音量 VolumeControl.exe 1

  3.减少音量 VolumeControl.exe 2

  下面我们用Java设计创建一个Panel用于显示音量并调用应用程序实现对音量的实际控制,本例中使用自定义Progress显示VolumeTracker.java

  实现原理如下:

  使用一个线程动态刷新页面,主线程用来实现对音量的控制.其实现代码如下:

  import java.awt.*;   import java.awt.font.*;   import java.awt.geom.*;   import java.awt.event.*;   import java.text.AttributedString;   import java.text.AttributedCharacterIterator;   import javax.swing.*;   import javax.swing.border.*;   import javax.swing.table.*;   import javax.swing.event.*;   import java.io.*;   public class VolumeTracker extends JPanel implements Runnable   {   String welcomeStr = "Welcome to Java Sound";   Thread pbThread;   Color background = Color.white;   //new Color(20, 20, 20);   Color jfcBlue = Color.blue;   //new Color(204, 204, 255);   Color jfcDarkBlue = jfcBlue.darker();   Font font24 = new Font("serif", Font.BOLD, 24);   Font font28 = new Font("serif", Font.BOLD, 28);   Font font42 = new Font("serif", Font.BOLD, 42);   FontMetrics fm28, fm42;   String errStr=null;   String currentName=null;   double duration = 100.0;   double seconds = 82.0;   boolean midiEOM, audioEOM;   public VolumeTracker()   {   fm28 = getFontMetrics(font28);   fm42 = getFontMetrics(font42);   initVolume();   start();   }   private void initVolume()   {   try   {   //这一段小程序实现对VC创建程序的调用   Runtime rt = Runtime.getRuntime(); //Time and Date.   //mngPathTool类,提供了一个获取当前路径的方法   mngPathTool tool = new mngPathTool();   String sexec = tool.getCurPath()+ "\\binex\\VolumeControl.exe 0";   Process child = rt.exec(sexec);   //获取控制台输出的内容,进而获得音量的大小   InputStreamReader reader = new InputStreamReader(child.getInputStream());   char[] chr = new char[5];   reader.read(chr) ;   String s="";   for(int i=0;i<5;i++)   {   if(chr[i]>='0' && chr[i]<='9') s+=chr[i];   }   //System.out.println(s);   Integer nVolume = new Integer(s);   seconds = nVolume.intValue();   child.waitFor();   //这一段小程序实现对VC创建程序的调用   }   catch(Exception e1)   {   e1.printStackTrace();   }   }   public void paint(Graphics g)   {   //画图来实现百分比Tracker   Graphics2D g2 = (Graphics2D) g;   Dimension d = getSize();   g2.setBackground(background);   g2.clearRect(0, 0, d.width, d.height);   g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);   g2.setColor(jfcBlue);   double tseconds = duration-seconds;   if (tseconds > 0.0)   {   int num = 20;   int progress = (int) (tseconds / duration * num);   double hh = ((double) (d.height - 4) / (double) num);   double ww = (int)(d.width-4);   double x = 0.0;   for ( ; x < progress; x+=1.0)   {   g2.fill(new Rectangle2D.Double(d.width-ww-2, x*hh+2, ww, hh));   g2.fill3DRect((int)(d.width-ww-2),(int) (x*hh+2),(int) ww, (int)hh,true);   }   g2.setColor(jfcDarkBlue);   for ( ; x < num; x+=1.0)   {   g2.fill(new Rectangle2D.Double(d.width-ww-2, x*hh+2, ww, hh));   g2.fill3DRect((int)(d.width-ww-2),(int) (x*hh+2),(int) ww, (int)hh,true);   }   }   }   public void start()   {   pbThread = new Thread(this);   pbThread.setName("PlaybackMonitor");   pbThread.start();   }   public void stop()   {   if (pbThread != null)   {   pbThread.interrupt();   }   pbThread = null;   }   public void run()   {   while (pbThread != null)   {   try   {   pbThread.sleep(99);   }   catch (Exception e)   {   break;   }   repaint();   }   pbThread = null;   }   public void addVolume()   {   changeVolume(false);   initVolume();   }   public void minusVolume()   {   changeVolume(true);   initVolume();   }   //control sound volume.   private void changeVolume(boolean bIsMinus)   {   try   {   Runtime rt = Runtime.getRuntime();   //Sound Control mngPathTool   tool = new mngPathTool();   String sexec;   if(bIsMinus)   sexec= tool.getCurPath()+ "\\binex\\VolumeControl.exe 2";   else   sexec= tool.getCurPath()+ "\\binex\\VolumeControl.exe 1";   rt.exec(sexec);   }catch(Exception e1){e1.printStackTrace(); }   }   }   // End VolumeTracker

  创建一个JFrame用于显示 VolumeControl.java

  设置页面背景,创建显示上述Panel的容器.

  创建对话框用于弹出显示音量控制界面 JVolumeDlg.java

  创建一个对话框来显示上个步骤生成的Frame,并提供事件控制容器.

  事件流向---> JVolumeDlg -- VolumeControl --- VolumeTracker

  总结

  Java Runtime 接口提供了调用其他应用程序的接口,通过这个接口,可以实现对计算机硬件的控制和监控. 同时通过界面线程可以实现相对复杂的应用程序界面的开发。

0
相关文章