1282 Java Media Framework and Java Sound (on CD) Chapter 22 portedAudioFileException if the specified sound file is a non-audio file or if it contains a format that is not supported by Java Sound. Next the program must provide a line through which audio data can be processed. Line 52 invokes method loadClip (lines 57 106) to open a Clip line and load the audio stream for playback. Line 81 invokes AudioSystem static method getLine to obtain a Clip line for audio playback. Method getLine requires a Line.Info object as an argument, to specify the attributes of the line that the AudioSystem should return. The line must be able to process audio clips of all supported sampled audio formats, so the DataLine.Info object must specify a Clip data line and a general encoding format. A buffer range should also be specified so the program can determine the best buffer size. The DataLine.Info constructor receives four arguments. The first two are the format (of type AudioFormat.Encoding) into which the program should convert the audio data and the AudioFormat of the audio source. The AudioFormat sets the format supported by the line, according to the audio format of the stream. Line 63 obtains the Audio- Format of the AudioInputStream, which contains format specifications that the underlying system uses to translate the data into sounds. Lines 68 69 call AudioSystem method getTargetFormatsto obtain an array of the supported AudioFormats. The third argument of the DataLine.Info constructor, which specifies the minimum buffer size, is set to the number of bytes in each frame of the audio stream. Line 70 invokes AudioFormat method getFrameSize to obtain the size of each frame in the audio stream. The maximum buffer size should be equivalent to two frames of the audio stream (line 71). Using the DataLine.Info object, line 74 checks if the underlying audio system supports the specified line. If it does, line 81 obtains the line from the audio system. When an audio clip starts playing and when it finishes, the program needs to be alerted. Line 84 registers a LineListener for the Clip s LineEvents. If a LineEvent occurs, the program calls LineListener method update (lines 115 133) to process it. The four LineEvent types, as defined in class LineEvent.Type, are OPEN, CLOSE, START and STOP. When the event type is LineEvent.Type.STOP and variable replayis false, line 120 calls ClipPlayer s close method (lines 142 148) to stop audio playback and close the Clip. All audio resources obtained previously by the Clip are released when audio playback stops. When the event type is LineEvent.Type.STOP and variable replay is true, line 131 calls Clip method loop with parameter Clip.LOOP_CONTINUOUSLY, causing the Clipto loop until the user terminates the application. Invoking method stop of interface Clip stops data activity in the Line. Invoking method start resumes data activity. Once the program finishes validating the Clip, line 87 calls Clip method open with the AudioInputStreamsoundStream as an argument. The Clip obtains the system resources required for audio playback. AudioSystem method getLine and Clip method open throw LineUnavailableExceptions if another application is using the requested audio resource. Clip method open also throws an IOException if the Clip cannot read the specified AudioInputStream. When the test program (Fig. 22.6) calls ClipPlayer method play (lines 109 112), the Clip method start begins audio playback. Class ClipPlayerTest (Fig. 22.6) enables users to specify an audio file to play by clicking the Open Audio Clip button. When users click the button, method action- Performed(lines 37 58) prompts an audio file name and location (line 39) and creates
Check Tomcat Web Hosting services for best quality webspace to host your web application.