0

I am using com.google.android.exoplayer2 ( exoPlayer Version = 'r2.5.2')and I had to load / streaming videos like

> https://something.com/test/something.m3u8

It was working nicely.

But the requirement change and according to that changed the video ' URL's format' by adding some authenticate related parts to query parameter of the URL.

> https://something.com/test/something.m3u8?media-auth=exp=1623782763942~acl=/test/7dede44-djnjcndncj/*~hmac=3232434242

now the player is not loading this videos.

Error log shows this error.

2021-05-06 08:42:12.395 7020-7220/? E/ExoPlayerImplInternal: Source error. com.google.android.exoplayer2.upstream.HttpDataSource$InvalidResponseCodeException: Response code: 403 at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.open(DefaultHttpDataSource.java:211) at com.google.android.exoplayer2.upstream.DefaultDataSource.open(DefaultDataSource.java:141) at com.google.android.exoplayer2.upstream.DataSourceInputStream.checkOpened(DataSourceInputStream.java:102) at com.google.android.exoplayer2.upstream.DataSourceInputStream.open(DataSourceInputStream.java:65) at com.google.android.exoplayer2.upstream.ParsingLoadable.load(ParsingLoadable.java:125) at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:315) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:764) 2021-05-06 08:42:12.396 7020-7020/? E/VideoPlayerView: onError

HlsSource sourceHLS = new HlsSource(videoQuiz.video.id(),
                    videoQuiz.video.title(),
                    VideoHelper.prepareHlsVideoUriWithQuery("video URL"),
                    HlsSource.TIME_UNSET,
                    0,
                    HlsSource.TIME_UNSET,
                    0L, null);
            videoPlayerView.start(sourceHLS,
                    toUri(thumbnailUrl),
                    autoPlay,
                    getCurrentSegmentStartPosition());
            showQuestionAt(currentQuestionPosition);

And below I mentioned how I changed "prepareHlsVideoUriWithQuery" method.

public static Uri prepareHlsVideoUriWithQuery(String thisUrl) {
    URL url = null;
    try {
        url = new URL("video URL");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    Uri.Builder builder = new Uri.Builder()
            .scheme(url.getProtocol())
            .encodedAuthority(url.getAuthority())
            .encodedPath(url.getPath().substring(1))
            .encodedQuery("video-auth=exp=24244~test=/test/232323-3232323/*~test=24242c0232n3223");

    return builder.build();
}

So,which place I have to change to load m3u8+authstring in exo player?

and any thought about set mimetype describe here Android HLS video mime type

2 Answers2

1

I could find a solution,

When Initialize the class, I added

private static final CookieManager DEFAULT_COOKIE_MANAGER;
static
{
    DEFAULT_COOKIE_MANAGER = new CookieManager();
    DEFAULT_COOKIE_MANAGER.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
}

In OnViewCreated(),

    if (CookieHandler.getDefault() != DEFAULT_COOKIE_MANAGER)
    {
        CookieHandler.setDefault(DEFAULT_COOKIE_MANAGER);
    }

And when Data source create to load, Instead of

final DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(..)

I added

    DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory(MediaHelper.USER_AGENT);
    dataSourceFactory.getDefaultRequestProperties().set("Cookie", "cookieValue");

Then it Works!

0

As described in the ExoPlayer docs, InvalidResponseCodeException is thrown when an attempt to open a connection results in a response code not in the 2xx range.

The error message is telling you that the server has rejected the request with response code 403, meaning you are forbidden from accessing the resource.

Given it used to work before authentication was enabled, it seems most likely that your token generation code isn't working properly and is generating a token that the server deems invalid.

Anonymous Coward
  • 1,096
  • 11
  • 22
  • as per the document you mention,(exo player doc) you are correct, but the thing is I tested the same development in IOS app, this URL is working and query parameter seems okay. Then there should be a another reason in android exo player I have, for m3u8 + "auth query" url. – charitha amarasinghe May 07 '21 at 06:27
  • In your builder, you have two keys (`exp`, `test`) and you appear to be setting the `test` key twice (`test=/test/232323-3232323/*`, `test=24242c0232n3223`), whereas the example you give at the top has three keys (`exp`, `acl`, `test`). Is the builder definitely doing the right thing here? Surely you need to set the `acl` key in the builder? Also, the endpoints differ (`test-auth` vs `video-auth`), but I assume that is on purpose. – Anonymous Coward May 07 '21 at 09:35
  • " exp=14637437e~acl=/msnt/4837483-bb1e3ee-323-832e23-2323cnds/*~hmad=65b64d32323370e " this is the correct format. – charitha amarasinghe May 10 '21 at 15:07
  • I edited the queryparameter which is in my question..with the correct elements. – charitha amarasinghe May 13 '21 at 16:56