18. Sockets and Networking

18.3. Using Multimedia Network Resources for a Graphical Program

 

 

 

 

 

 

 

Problem statement

 

 

 

Specifying Web resources


Suppose you want to write an graphical program that will display a cat- alog consisting of images or documents that you’ve prepared and stored on your Web site. Perhaps you can use such a program to give people who visit your site a downloadable tour of your campus as a slide show (Fig. 15.8). Or perhaps a company might use such a program to advertise its products. In addition to making the catalog available through its main Web site, you can imagine it running continuously as a slide show on a computer kiosk in the company’s lobby.

In order to solve this problem we have to be able to download and display Web resources. As you know, Web resources are multimedia. That is, they could be documents, images, sounds, video clips, and so on. All Web resources are specified in terms of their Uniform Resource Locators (URLs). Thus, to download an image (or an HTML file or audio clip), we usually type its URL into a Web browser. We want our program to know beforehand the URLs of the images it will display, so there won’t be any need for inputting the URL. We want to implement something like the following algorithm:

,,

 

 

 

 

J

 

A URL specification is just a String, such as,

,,

 

J

which describes how to retrieve the resource. First, it specifies the protocol or method that should be used to download the resource (http). Then, it provides the domain name of the server that runs the protocol and the port number where the service is running (www.cs.trincoll.edu:80). Next, the URL specifies the resource’s file name (˜ram/jjj/slideshow/slide1.gif).

 

 

 

 

From the Java Library: java.net.URL

 

GIVEN SUCH a URL specification, how can we download its associated resource? Are there Java classes that can help us solve this problem? For- tunately, there are. First, the java.net.URL class contains methods to help retrieve the resource associated with a particular URL (Fig. 15.9). The URL class represents a Uniform Resource Locator. The URL() constructor shown here (there are others) takes a URL specification as a String and,

assuming it specifies a valid URL, it creates a URL object. If the URL speci-

fication is invalid, a MalformedURLException is thrown. A URL might

 

be invalid if the protocol were left off or if it is not a known protocol. The following simple code creates a URL for the home page of our companion Web site:


Figure 15.9: The java.net.URL

class.

 

,,


java.sun.com/j2se/1.5.0/docs/api/

 

 

 

 

 

 

 

 

 

Note how we catch the MalformedURLException when we create a new URL.

Once we have a valid URL instance, it can be used to download the data or object associated with it. There are different ways to do this. The openConnection() method creates a URLConnection, which can then be used to download the resource. You would only use this method if your application required extensive control over the download process. A much simpler approach would use the openStream() method. This method will open an InputStream, which you can then use to read the associated URL data the same way you would read a file. This method is especially useful for writing Java applications (in addition to applets). As you might guess, downloading Web resources is particularly easy from a Java applet. Now let’s search around for other methods that we can use.


J

 

 

 

 

 

 

 

 

 

URLs and streams

 

Code Reuse: The java.applet.Applet Class

 

 

 

 

 

 

The java.applet.Applet class itself contains several useful methods for downloading and displaying Web resources. These methods are in- herited by javax.swing.JApplet:

,,

 

 

 

 

 

J

As you see, both the getImage() and getAudioClip() methods use a URL to download a resource. An AudioClip is a sound file encoded in AU format, a special type of encoding for sound files. The getImage() method can return files in either GIF or JPEG format, two popular image file formats. The play() method downloads and plays an audio file in one easy step. For example, to download and play an audio clip within an applet requires just two lines of code:

,,

 

 

 

 

 

J

Similarly, to download (and store a reference to) an image is just as simple:

,,

 

 

 

 

 

J

So, if applets were still in fashion, then these would be the methods we need to implement our slide show. For an application, to load an image

 

you need to declare the ImageRef and the call to read the image url is different:

,,

 

 

 

 

 

 

J

For sound we need the javax.sound.sampled package. After con- structing the URL, we need to set up an AudioInputStream, put the format of the stream into a DataLine.info object, and then get the line as a Clip, which we can then use to play the sound. The AudioSystem class is used heavily for this.

,,

Clip c l i p ; URL ur l = null ;

t r y {u r l = new

URL( http :// cooplogic . com/cheyney/sound/ l i f t M u s i c . wav” ) ; AudioInputStream audio =

AudioSystem . getAudioInputStream ( ur l ) ; // g e t s t r e a m f r o m u r l

DataLine . Info in fo =

new DataLine . Info ( Clip . class , audio . getFormat ( ) ) ; // i n f o n e e d e d f o r l i n e

i f ( ! AudioSystem . is Line Supported ( in fo ) )

System . e r r . p r i n t l n ( ”Audio f i l e not supported : + in fo ) ;

return ;

}t r y

c l i p = ( Clip ) AudioSystem . get Line ( info ) ; // t h e c l i p d o e s t h e w o r k

c l i p . open ( audio ) ; // o p e n t h e s t r e a m .

c l i p . s t a r t ( ) ; // s t a r t t h e s t r e a m o n a s e p a r a t e t h r e a d .

// l o o p u n t i l c l i p h a s f i n i s h e d

while ( c l i p . get Frame Position ( ) < c l i p . getFrameLength ( ) ) {

t r y {Thread . s leep ( 1 0 ) ;

catch ( Exception e )

e . pr int St ac k Tr ac e ( ) ;

}

}

catch ( Line Unavailable Exception ex ) ex . pr int St ac k Tr ac e ( ) ;

 

catch ( MalformedURLException e )

System . out . p r i n t l n ( ”Malformed URL: + ur l . t o S t r i n g ( ) ) ;

catch ( UnsupportedAudioFile Exception ae ) System . out . p r i n t l n ( not supported : + ae ) ;

catch ( IOException ioex )

ioex . pr int St ac k Tr ac e ( ) ;

\} J

 

What methods can we use?


We’ll use the URL() constructor to create a URL from a String, and we’ll use the javax.imageio.ImageIO.read(url) method to retrieve the images from the Web.