what (clip) and DataLine.Info represents...?
I got this code from one of my friend.
import java.io.*;
import javax.sound.sampled.*;
public class xx
{
public static void main(String args[])
{
try
{
File f=new File("mm.wav");
AudioInputStream a=AudioSystem.getAudioInputStream(f);
AudioFormat au=a.getFormat();
DataLine.Info di=new DataLine.Info(Clip.class,au);
Clip c=(Clip)AudioSystem.getLine(di);
c.open(a);
开发者_如何学Python c.start();
}
catch(Exception e)
{
System.out.println("Exception caught ");
}
}
}
But i didn't understand what this line means Cilp c=(Clip)AudioSystem.getLine(di); what (clip) represents....? And my 2nd problem is what is the DataLine is it an interface and what is the meaning of this statement DataLine.Info....?
DataLine is an interface that contains a nested class "Info". Here the statement :
DataLine.Info di = ...
creates a new instance of the class Info, that is defined in the class DataLine.
The statement (Clip) is what we call a cast. It is used to convert an object from a type to another. The method AudioSystem.getLine(di) returns an object of type Line. So basically, your friend converted the returned object into a Clip, to be used to instantiate the object c. It is allowed and won't generate an error because c is of type Clip, which extends DataLine, and DataLine itself extends Line.
Hope this helps !
精彩评论