<< Chapter < Page
  Accessible objected-oriented     Page 15 / 17
Chapter >> Page >


Listing 25 . The class named AudioPlayOrFile01.
/*File AudioPlayOrFile01.java Copyright 2014, R.G.BaldwinRevised 08/16/14 ******************************************************************************/import javax.sound.sampled.*; import java.io.*;import java.util.*; public class AudioPlayOrFile01{//An object of this class is used to either play the sound in the array // named melody or to write it into an audio file of type AU.//The following are general instance variables used to create a// SourceDataLine object. AudioFormat audioFormat;AudioInputStream audioInputStream; SourceDataLine sourceDataLine;AudioFormatParameters01 audioParams; byte[]melody; String playOrFile;//"play" to play immediately or a fileName to write// an output file of type AU. //-------------------------------------------------------------------------//public AudioPlayOrFile01(AudioFormatParameters01 audioParams,byte[] melody,String playOrFile){//constructor this.audioParams = audioParams;this.melody = melody; this.playOrFile = playOrFile;}//end constructor //-------------------------------------------------------------------------////This method plays or files the synthetic audio data that has been generated // and saved in an array.void playOrFileData() { try{//Get an input stream on the byte array containing the data InputStream byteArrayInputStream = new ByteArrayInputStream(melody);//Get the required audio format audioFormat = new AudioFormat(audioParams.sampleRate,audioParams.sampleSizeInBits, audioParams.channels,audioParams.signed, audioParams.bigEndian);//Get an audio input stream from the ByteArrayInputStreamaudioInputStream = new AudioInputStream( byteArrayInputStream,audioFormat, melody.length/audioFormat.getFrameSize());//Get info on the required data line DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class,audioFormat); //Get a SourceDataLine objectsourceDataLine = (SourceDataLine)AudioSystem.getLine(dataLineInfo);//Decide whether to play the audio data immediately, or to write it // into an audio file of type AU based on the incoming parameter named// playOrFile. if(playOrFile.toUpperCase().equals("PLAY")){//Create a thread to play back the data and start it running. It will // run until all the data has been played backnew PlayAudioThread().start(); }else{//Write the data to an output file with the name provided by the // incoming parameter named playOrFile.try{ AudioSystem.write(audioInputStream,AudioFileFormat.Type.AU, new File(playOrFile + ".au"));}catch (Exception e) { e.printStackTrace();System.exit(0); }//end catch}//end else }catch (Exception e) {e.printStackTrace(); System.exit(0);}//end catch }//end playOrFileData//===========================================================================// //Inner class to play back the data that was saved.class PlayAudioThread extends Thread{ //This is a working buffer used to transfer the data between the// AudioInputStream and the SourceDataLine. The size is rather arbitrary. byte playBuffer[]= new byte[16384];public void run(){try{ //Open and start the SourceDataLinesourceDataLine.open(audioFormat); sourceDataLine.start();int cnt;//Get beginning of elapsed time for playback long startTime = new Date().getTime();//Transfer the audio data to the speakerswhile((cnt = audioInputStream.read( playBuffer,0,playBuffer.length)) != -1){//Keep looping until the input read method returns -1 for empty // stream.if(cnt>0){ //Write data to the internal buffer of the data line where it will// be delivered to the speakers in real time sourceDataLine.write(playBuffer, 0, cnt);}//end if }//end while//Block and wait for internal buffer of the SourceDataLine to become// empty. sourceDataLine.drain();//Get and display the elapsed time for the previous playback. int elapsedTime = (int)(new Date().getTime() - startTime);System.out.println("Elapsed time: " + elapsedTime);//Finish with the SourceDataLine sourceDataLine.stop();sourceDataLine.close(); }catch (Exception e) {e.printStackTrace(); System.exit(0);}//end catch}//end run }//end inner class PlayAudioThread//=========================================================================// }//end AudioPlayOrFile01 class

Questions & Answers

what is the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
scanning tunneling microscope
how nano science is used for hydrophobicity
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
what is differents between GO and RGO?
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
The nanotechnology is as new science, to scale nanometric
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
for teaching engĺish at school how nano technology help us
How can I make nanorobot?
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
how can I make nanorobot?
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

Source:  OpenStax, Accessible objected-oriented programming concepts for blind students using java. OpenStax CNX. Sep 01, 2014 Download for free at https://legacy.cnx.org/content/col11349/1.17
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Accessible objected-oriented programming concepts for blind students using java' conversation and receive update notifications?