前言
本文通过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 |
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器