androidvideo-streamingmime-typeshttp-live-streamingexoplayer

ExoPlayer in Android not load with M3U8 + QueryParameter(auth)


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


Solution

  • 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!