0

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

3 回答 3

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();
于 2013-01-03T04:08:07.967 回答
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()的实例对象。ICodecContextWrappercc

于 2013-01-03T04:08:30.960 回答
0
ICodecContextWrapper cc = audioDecoder.getCodecContext();

getCodecContext 可能是这样的:

ICodecContextWrapper getCodecContext() {

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

}
于 2013-01-03T04:09:33.283 回答