What I’m trying to do now is to make media requests with specific cookies. Here’s the current situation: Since background playback must be possible, I referred to the documentation and retrieved the MediaController as follows. (Here, PlaybackService
is an object that inherits from MediaSessionService
.)
SessionToken sessionToken =
new SessionToken(this.themedReactContext, new ComponentName(this.themedReactContext, PlaybackService.class));
ListenableFuture<MediaController> controllerFuture =
new MediaController.Builder(this.themedReactContext, sessionToken).buildAsync();
controllerFuture.addListener(() -> {
try {
player = controllerFuture.get();
} catch (Exception error) {
Log.e(TAG, error.getMessage());
}
MediaItem mediaItem = ALibrary.getMediaItem();
player.setMediaItem(mediaItem);
player.prepare();
// ... play with the 'player'
});
Now, to embed cookies in the media request, I created a mediaSource using OkHttp as shown below.
private final OkHttpClient httpClient = new OkHttpClient.Builder().build();
// CookieUtil is what I made
List<Cookie> cookies = CookieUtil.convertListToCookies(listOfCookieMap);
OkHttpClient client = httpClient
.newBuilder()
.cookieJar(new CookieJar() {
@Override
public void saveFromResponse(@NonNull HttpUrl httpUrl, @NonNull List<Cookie> list) {
//
}
@NonNull
@Override
public List<Cookie> loadForRequest(@NonNull HttpUrl httpUrl) {
return cookies;
}
})
.build();
OkHttpDataSource.Factory dataSourceFactory = new OkHttpDataSource.Factory(client);
MediaItem mediaItem = ALibrary.getMediaItem(); // The code shown above
MediaSource mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(mediaItem);
Now, I need to use the mediaSource
, but the MediaController can only use mediaItem
with setMediaItem
and cannot use mediaSource
(there is no setMediaSource
). I’ve looked for various solutions but couldn’t resolve this issue.
How can I include specific cookies in a media request?
(Note that I have to obtain mediaItem through an external library.)