我正在使用这个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);
}