为什么接口类型的这些变量不用于实例化新对象?

时间:2013-01-03 03:59:16

标签: java

我正在使用this api,并且遇到了一些令我困惑的代码示例。我知道我可以使用“new”将对象分配给接口,因为接口是一种数据类型。我从下面的代码中无法理解的是为什么变量:“cc”和“audioDecoder”被分配了它们已被分配的值。据我所知,这些变量应该分配给新对象。有人能解释一下这里发生了什么吗?

try {
// open media file 
DefaultMediaPlayer player = new DefaultMediaPlayer("/home/me/walking.wav");

// get some properties of the first audio stream 
IDecoder audioDecoder = player.getAudioStreamDecoder(0);
ICodecContextWrapper cc = audioDecoder.getCodecContext();

int sampleFormat = cc.getSampleFormat();
int sampleRate = cc.getSampleRate();
int bytesPerSample = AVSampleFormat.getBytesPerSample(sampleFormat);
long channelLayout = cc.getChannelLayout();
int channelCount = AVChannelLayout.getChannelCount(channelLayout);
AudioFormat.Encoding encoding;

if (AVSampleFormat.isPlanar(sampleFormat) || AVSampleFormat.isReal(sampleFormat))
    throw new LibavException("unsupported output sample format");
else if (AVSampleFormat.isSigned(sampleFormat))
    encoding = AudioFormat.Encoding.PCM_SIGNED;
else
    encoding = AudioFormat.Encoding.PCM_UNSIGNED;

// create Java InputStream for audio stream raw data
SampleInputStream sis = new SampleInputStream(sampleRate * bytesPerSample * channelCount, true);

// create AudioInputStream from the SampleInputStream
AudioInputStream audioStream = new AudioInputStream(sis, new AudioFormat(encoding, sampleRate,
    bytesPerSample * 8, channelCount, bytesPerSample * channelCount, sampleRate, 
    ByteOrder.BIG_ENDIAN.equals(ByteOrder.nativeOrder())), -1);

// create adapter between Libav audio frames and the SampleInputStream
Frame2AudioFrameAdapter resampler = new Frame2AudioFrameAdapter(channelLayout, channelLayout, sampleRate, 
    sampleRate, sampleFormat, sampleFormat);

// get audio mixer for the audio stream format
PlaybackMixer audioMixer = PlaybackMixer.getMixer(audioStream.getFormat());

// connect all streams
audioDecoder.addFrameConsumer(resampler);
resampler.addAudioFrameConsumer(sis);
audioMixer.addInputStream(audioStream); 

// enable audio stream decoding
player.setAudioStreamDecodingEnabled(0, true);

// start playback
audioMixer.play();
player.play();

// wait until the playback stops
player.join();

// release system resources
player.close();
resampler.dispose();
PlaybackMixer.closeAllMixers();
} catch (Exception ex) {
    Logger.getLogger(PlaybackSample.class.getName()).log(Level.WARNING, "unable to play audio", ex);
}

3 个答案:

答案 0 :(得分:1)

您不需要像这样一直创建对象

SomeClass obj=new SomeClass();

你可以有这样的案例

public class OtherClass
{
  public SomeClass getSomeClassObject()
 {
    return new SomeClass();
 }
}

given that SomeClass is accesible within OtherClass

您可以按照以下方式使用

OtherClass other=new OtherClass();
SomeClass come=other.getSomeClassObject();

答案 1 :(得分:1)

如果您已阅读API文档。方法DefaultMediaPlayer.getAudioStreamDecoder

返回 IDecoder 类型的实例。这就是为什么在src中他们将返回类型分配给 IDecoder 类型的 audioDecoder 变量。

// get some properties of the first audio stream 
IDecoder audioDecoder = player.getAudioStreamDecoder(0);
ICodecContextWrapper cc = audioDecoder.getCodecContext();

没有规则说您只能使用new将对象分配给接口类型。您可以从方法返回类型中分配对象实例。

同样,方法IDecoder.getCodecContext()返回实例ICodecContextWrapper的对象,该对象被分配给变量cc

答案 2 :(得分:0)

ICodecContextWrapper cc = audioDecoder.getCodecContext();

getCodecContext可能是这样的:

ICodecContextWrapper getCodecContext() {

return new ICodecContextWrapper() {
  //override methods
};

}