DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 6th August 2015
mattthumper mattthumper is offline
Real Name: Matt Morrow
Port Guard
 
Join Date: Jul 2015
Location: Virginia, USA
Posts: 20
Default Volume Control (in Java)

Here's a Java application for OpenBSD which I just wrote that presents a simple volume control which uses mixerctl behind-the-scenes. OK, OK, the code's not ENTIRELY mine. It is, however, MOSTLY mine! Yes, I'm one of those lazy Java coders that 1st goes to Rose (India) looking for good, simple and very straightforward examples. (At least when I want to try something completely new).

Code:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.net.*;

public class CreateSlider{
     JSlider slider;
     JLabel label;
     public static String strCommand = new String();
     public static String str = new String();

     public static void main(String[] args){
        CreateSlider cs = new CreateSlider();
     }

  public CreateSlider(){
     JFrame frame = new JFrame("OpenBSD Volume");
     slider = new JSlider();
     slider.setMinimum(1);
     slider.setMaximum(255);
     slider.setValue(128);
     slider.addChangeListener(new MyChangeAction());
     label = new JLabel("By Matt");
     JPanel panel = new JPanel();
     panel.add(slider);
     panel.add(label);
     frame.add(panel, BorderLayout.CENTER);
     frame.setSize(400, 90);
     frame.setVisible(true);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

   public class SubmitProcess {
      public SubmitProcess (String strUserCommand) {
         try {
             // Create a runtime process (our script)
             Process proc = Runtime.getRuntime().exec(strUserCommand);

             // for the scripts output:
          InputStream in = proc.getInputStream();
             InputStreamReader reader_in = new InputStreamReader( in );
             BufferedReader buffered_reader_in = new BufferedReader( reader_in );
             String        inStr;


             // read and display script output (which should be what we entered above)
             // Here's where we write the output to a log - when we decide to incorporate that functionality
             try {
                while ( (inStr = buffered_reader_in.readLine()) != null)
                   System.out.println(inStr);
                   if (proc.waitFor() != 0) // if the command failed
                      // NOTE: You might receive this if you mistakenly use a valid command and you pass it bad args
                      //       For example the GUI submitted "Replace this string with your command"
                      //       to the system. System ran Replace.exe which puked with the bad parameters.
                      System.out.println(" waitfor command failed to execute");
             }
             catch (InterruptedException e) {
                System.out.println("SubmitProcess Error reading stream, see log for more detail: " + e);
             }
         }
         catch (IOException e) {
            System.out.println("SubmitProcess Error, see log for more detail: " + e);
         }
     }
  }

  public class MyChangeAction implements ChangeListener{
     public void stateChanged(ChangeEvent ce){
        int value = slider.getValue();
        str = Integer.toString(value);
        label.setText(str);
        strCommand = new String("mixerctl -q outputs.master=" + str + "," + str );
        SubmitProcess UserCommand = new SubmitProcess(strCommand); 
     }
   }
   


}
Enjoy!
Matt

Last edited by mattthumper; 6th August 2015 at 06:45 PM.
Reply With Quote
  #2   (View Single Post)  
Old 6th August 2015
ocicat ocicat is online now
Administrator
 
Join Date: Apr 2008
Posts: 3,319
Default

Originally posted in OpenBSD General.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Volume Control Graphical Utility mattthumper OpenBSD Packages and Ports 21 31st July 2015 01:05 AM
Systray Audio Volume control shep OpenBSD Packages and Ports 2 30th April 2013 02:29 PM
Rescuing broken RAID5 gvinum volume morfran FreeBSD General 1 26th May 2010 09:49 AM
MS DOS volume labels? bigearsbilly FreeBSD General 2 1st April 2009 10:50 PM
mplayer volume cannot increase. bsdnewbie999 OpenBSD General 3 19th June 2008 03:16 PM


All times are GMT. The time now is 10:25 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick