gpt4 book ai didi

java - 如何实现包装 SubtitleDecoderFactory.DEFAULT 的自定义 SubtitleDecoderFactory,这是一个常量实例

转载 作者:行者123 更新时间:2023-11-30 05:21:30 26 4
gpt4 key购买 nike

我正在学习Exoplayer,发现了一个帖子https://github.com/google/ExoPlayer/issues/4657 (在 erdemguven 于 2018 年 8 月 24 日评论的帖子末尾)关于创建包装 SubtitleDecoderFactory.DEFAULT 的包装器 SubtitleDecoderFactory(位于 https://github.com/google/ExoPlayer/blob/release-v2/library/core/src/main/java/com/google/android/exoplayer2/text/SubtitleDecoderFactory.java )。

在 stackoverflow 的另一篇文章中,我提出了一个有关 SubtitleDecoderFactory.DEFAULT 的问题,从中我了解到 SubtitleDecoderFactory.DEFAULT 是一个常量实例。 (位于Interface implements overriding its own methods to create an object of itself as DEFAULT)

所以我的问题是:

  1. 由于常量实例在程序中的任何位置都可以看到和引用,是否有必要包装常量实例?

  2. 如果需要包装这个常量实例SubtitleDecoderFactory.DEFAULT,代码会怎样?

这是基本接口(interface) SubtitleDecoderFactory(位于 https://github.com/google/ExoPlayer/blob/release-v2/library/core/src/main/java/com/google/android/exoplayer2/text/SubtitleDecoderFactory.java ):

public interface SubtitleDecoderFactory {


boolean supportsFormat(Format format);


SubtitleDecoder createDecoder(Format format);


SubtitleDecoderFactory DEFAULT =
new SubtitleDecoderFactory() {

@Override
public boolean supportsFormat(Format format) {
@Nullable String mimeType = format.sampleMimeType;
return MimeTypes.TEXT_VTT.equals(mimeType)
|| MimeTypes.TEXT_SSA.equals(mimeType)
|| MimeTypes.APPLICATION_TTML.equals(mimeType)
|| MimeTypes.APPLICATION_MP4VTT.equals(mimeType)
|| MimeTypes.APPLICATION_SUBRIP.equals(mimeType)
|| MimeTypes.APPLICATION_TX3G.equals(mimeType)
|| MimeTypes.APPLICATION_CEA608.equals(mimeType)
|| MimeTypes.APPLICATION_MP4CEA608.equals(mimeType)
|| MimeTypes.APPLICATION_CEA708.equals(mimeType)
|| MimeTypes.APPLICATION_DVBSUBS.equals(mimeType)
|| MimeTypes.APPLICATION_PGS.equals(mimeType);
}

@Override
public SubtitleDecoder createDecoder(Format format) {
@Nullable String mimeType = format.sampleMimeType;
if (mimeType != null) {
switch (mimeType) {
case MimeTypes.TEXT_VTT:
return new WebvttDecoder();
case MimeTypes.TEXT_SSA:
return new SsaDecoder(format.initializationData);
case MimeTypes.APPLICATION_MP4VTT:
return new Mp4WebvttDecoder();
case MimeTypes.APPLICATION_TTML:
return new TtmlDecoder();
case MimeTypes.APPLICATION_SUBRIP:
return new SubripDecoder();
case MimeTypes.APPLICATION_TX3G:
return new Tx3gDecoder(format.initializationData);
case MimeTypes.APPLICATION_CEA608:
case MimeTypes.APPLICATION_MP4CEA608:
return new Cea608Decoder(mimeType, format.accessibilityChannel);
case MimeTypes.APPLICATION_CEA708:
return new Cea708Decoder(format.accessibilityChannel, format.initializationData);
case MimeTypes.APPLICATION_DVBSUBS:
return new DvbDecoder(format.initializationData);
case MimeTypes.APPLICATION_PGS:
return new PgsDecoder();
default:
break;
}
}
throw new IllegalArgumentException(
"Attempted to create decoder for unsupported MIME type: " + mimeType);
}
};
}

我的自定义 SubtitleDecoderFactory 看起来像:

public class mSubtitleDecoderFactory implements SubtitleDecoderFactory{

@Override
public boolean supportsFormat(Format format) {
@Nullable String mimeType = format.sampleMimeType;
return MimeTypes.TEXT_VTT.equals(mimeType)
|| MimeTypes.TEXT_SSA.equals(mimeType)
|| MimeTypes.APPLICATION_TTML.equals(mimeType)
|| MimeTypes.APPLICATION_MP4VTT.equals(mimeType)
|| MimeTypes.APPLICATION_SUBRIP.equals(mimeType)
|| MimeTypes.APPLICATION_TX3G.equals(mimeType)
|| MimeTypes.APPLICATION_CEA608.equals(mimeType)
|| MimeTypes.APPLICATION_MP4CEA608.equals(mimeType)
|| MimeTypes.APPLICATION_CEA708.equals(mimeType)
|| MimeTypes.APPLICATION_DVBSUBS.equals(mimeType)
|| MimeTypes.APPLICATION_PGS.equals(mimeType);
}

@Override
public SubtitleDecoder createDecoder(Format format){

@Nullable String mimeType = format.sampleMimeType;
if (mimeType != null) {
switch (mimeType) {
case MimeTypes.TEXT_VTT:
return new WebvttDecoder();
case MimeTypes.TEXT_SSA:
return new SsaDecoder(format.initializationData);
case MimeTypes.APPLICATION_MP4VTT:
return new Mp4WebvttDecoder();
case MimeTypes.APPLICATION_TTML:
return new TtmlDecoder();
case MimeTypes.APPLICATION_SUBRIP:
return new SubripDecoder();
case MimeTypes.APPLICATION_TX3G:
return new Tx3gDecoder(format.initializationData);
case MimeTypes.APPLICATION_CEA608:
case MimeTypes.APPLICATION_MP4CEA608:
return new Cea608Decoder(mimeType, format.accessibilityChannel);
case MimeTypes.APPLICATION_CEA708:
return new Cea708Decoder(format.accessibilityChannel, format.initializationData);
case MimeTypes.APPLICATION_DVBSUBS:
return new DvbDecoder(format.initializationData);
case MimeTypes.APPLICATION_PGS:
return new PgsDecoder();
default:
break;
}
}
throw new IllegalArgumentException(
"Attempted to create decoder for unsupported MIME type: " + mimeType);
};


};

基本上它重写了两个抽象方法“booleansupportsFormat(Format format)”和 基接口(interface)SubtitleDecoderFactory中声明的“SubtitleDecoder createDecoder(Format format)”

但是在哪里将 SubtitleDecoderFactory.DEFAULT 作为参数放在 oder 中进行包装?

感谢您提前提供的帮助。

最佳答案

据我了解 https://github.com/google/ExoPlayer/issues/4657#issuecomment-415739157 ,对于您的情况,您需要创建一个具有以下行为的 SubtitleDecoderFactory 实例:

supportsFormat 调用委托(delegate)给 SubtitleDecoderFactory.DEFAULT

createDecoder 调用使用 SubtitleDecoderFactory.DEFAULT 创建 SubtitleDecoder 实例并将其包装到另一个 SubtitleDecoder 实例中(我们将为其使用匿名类),这会将除 dequeueOutputBuffer 之外的所有方法委托(delegate)给包装的实例,对于 dequeueOutputBuffer 它将使用您的自定义逻辑.

您的代码可能如下所示:

public class mSubtitleDecoderFactory implements SubtitleDecoderFactory{

@Override
public boolean supportsFormat(Format format) {
return SubtitleDecoderFactory.DEFAULT.supportsFormat(format);
}

@Override
public SubtitleDecoder createDecoder(Format format){
SubtitleDecoder wrapped = SubtitleDecoderFactory.DEFAULT.createDecoder(format);
SubtitleDecoder wrapper = new SubtitleDecoder() {

@Override
O dequeueOutputBuffer() throws E {
O resultFromWrapped = wrapped.dequeueOutputBuffer()
// Do what you want with resultFromWrapped and return it modified
}

@Override
String getName() {
return wrapped.getName();
}

@Override
void queueInputBuffer(I inputBuffer) throws E {
wrapped.queueInputBuffer(inputBuffer);
}

// Delegate all other methods

};
return wrapper
}

}

关于java - 如何实现包装 SubtitleDecoderFactory.DEFAULT 的自定义 SubtitleDecoderFactory,这是一个常量实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59505271/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com