Skip to content

Commit 14bf3fb

Browse files
committed
Add ability to know the locale of an audio stream
Getting audio tracks locales by parsing their ID or their label, should not be done by clients, but by the extractor. This commit adds the ability to store the Locale of an AudioStream, which is used to compare similar AudioStreams (in the equalStats method).
1 parent f924265 commit 14bf3fb

1 file changed

Lines changed: 38 additions & 2 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/stream/AudioStream.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import javax.annotation.Nonnull;
2727
import javax.annotation.Nullable;
28+
import java.util.Locale;
2829
import java.util.Objects;
2930

3031
public final class AudioStream extends Stream {
@@ -47,6 +48,8 @@ public final class AudioStream extends Stream {
4748
private final String audioTrackId;
4849
@Nullable
4950
private final String audioTrackName;
51+
@Nullable
52+
private final Locale audioLocale;
5053
private final boolean isDescriptive;
5154

5255
@Nullable
@@ -70,6 +73,8 @@ public static final class Builder {
7073
private String audioTrackId;
7174
@Nullable
7275
private String audioTrackName;
76+
@Nullable
77+
private Locale audioLocale;
7378
private boolean isDescriptive;
7479
@Nullable
7580
private ItagItem itagItem;
@@ -240,6 +245,21 @@ public Builder setIsDescriptive(final boolean isDescriptive) {
240245
return this;
241246
}
242247

248+
/**
249+
* Set the {@link Locale} of the audio which represents its language.
250+
*
251+
* <p>
252+
* The default value is {@code null}, which means that the {@link Locale} is unknown.
253+
* </p>
254+
*
255+
* @param audioLocale the {@link Locale} of the audio, which could be {@code null}
256+
* @return this {@link Builder} instance
257+
*/
258+
public Builder setAudioLocale(@Nullable final Locale audioLocale) {
259+
this.audioLocale = audioLocale;
260+
return this;
261+
}
262+
243263
/**
244264
* Set the {@link ItagItem} corresponding to the {@link AudioStream}.
245265
*
@@ -293,7 +313,8 @@ public AudioStream build() {
293313
}
294314

295315
return new AudioStream(id, content, isUrl, mediaFormat, deliveryMethod, averageBitrate,
296-
manifestUrl, audioTrackId, audioTrackName, isDescriptive, itagItem);
316+
manifestUrl, audioTrackId, audioTrackName, audioLocale, isDescriptive,
317+
itagItem);
297318
}
298319
}
299320

@@ -313,6 +334,7 @@ public AudioStream build() {
313334
* {@link #UNKNOWN_BITRATE})
314335
* @param audioTrackId the id of the audio track
315336
* @param audioTrackName the name of the audio track
337+
* @param audioLocale the {@link Locale} of the audio stream, representing its language
316338
* @param itagItem the {@link ItagItem} corresponding to the stream, which cannot be null
317339
* @param manifestUrl the URL of the manifest this stream comes from (if applicable,
318340
* otherwise null)
@@ -327,6 +349,7 @@ private AudioStream(@Nonnull final String id,
327349
@Nullable final String manifestUrl,
328350
@Nullable final String audioTrackId,
329351
@Nullable final String audioTrackName,
352+
@Nullable final Locale audioLocale,
330353
final boolean isDescriptive,
331354
@Nullable final ItagItem itagItem) {
332355
super(id, content, isUrl, format, deliveryMethod, manifestUrl);
@@ -344,6 +367,7 @@ private AudioStream(@Nonnull final String id,
344367
this.averageBitrate = averageBitrate;
345368
this.audioTrackId = audioTrackId;
346369
this.audioTrackName = audioTrackName;
370+
this.audioLocale = audioLocale;
347371
this.isDescriptive = isDescriptive;
348372
}
349373

@@ -355,7 +379,8 @@ public boolean equalStats(final Stream cmp) {
355379
return super.equalStats(cmp) && cmp instanceof AudioStream
356380
&& averageBitrate == ((AudioStream) cmp).averageBitrate
357381
&& Objects.equals(audioTrackId, ((AudioStream) cmp).audioTrackId)
358-
&& isDescriptive == ((AudioStream) cmp).isDescriptive;
382+
&& isDescriptive == ((AudioStream) cmp).isDescriptive
383+
&& Objects.equals(audioLocale, ((AudioStream) cmp).audioLocale);
359384
}
360385

361386
/**
@@ -470,6 +495,17 @@ public String getAudioTrackName() {
470495
return audioTrackName;
471496
}
472497

498+
/**
499+
* Get the {@link Locale} of the audio representing the language of the stream, which is
500+
* {@code null} if the audio language of this stream is not known.
501+
*
502+
* @return the {@link Locale} of the audio or {@code null}
503+
*/
504+
@Nullable
505+
public Locale getAudioLocale() {
506+
return audioLocale;
507+
}
508+
473509
/**
474510
* Returns whether this stream is a descriptive audio.
475511
*

0 commit comments

Comments
 (0)