Giter Site home page Giter Site logo

Comments (2)

yschimke avatar yschimke commented on May 16, 2024

Specifically one difficulty I encountered is that HLS uses the file extension.

https://exoplayer.dev/hls.html

If your URI doesn’t end with .m3u8, you can pass MimeTypes.APPLICATION_M3U8 to setMimeType of MediaItem.Builder to explicitly indicate the type of the content.

It seems quite awkward to configure this, using a MediaItemFiller that knows to set mimetype based on the app specific urls.

from media.

yschimke avatar yschimke commented on May 16, 2024

I've got this working via a couple of mechanisms.

fun streamInterceptor(moshi: Moshi): Interceptor {
    val adapter = moshi.adapter(Stream::class.java)

    return Interceptor { chain ->
        val request = chain.request()
        var response = chain.proceed(request)

        val body = response.body
        if (isStreamJson(response) && body != null) {
            val gzipped = "gzip".equals(response.header("Content-Encoding"), ignoreCase = true)
            val source = if (gzipped) GzipSource(body.source()).buffer() else body.source()

            val stream = adapter.fromJson(source)

            source.close()

            response = response.newBuilder()
                .code(307)
                .header("Location", stream!!.url)
                .body(ByteArray(0).toResponseBody())
                .build()
        }

        response
    }
}

private fun isStreamJson(response: Response) =
    response.body?.contentType()?.subtype == "json"
            && response.code == 200
            && response.request.url.host == "api.xxx.com"
            && response.request.url.pathSegments.firstOrNull() == "media"

and MediaSourceFactory

    override fun createMediaSource(mediaItem: MediaItem): MediaSource {
        val uri = mediaItem.localConfiguration?.uri

        return when (uri?.lastPathSegment) {
            "hls" -> HlsMediaSource.Factory(dataSourceFactory).createMediaSource(uri)
            "progressive" -> ProgressiveMediaSource.Factory(dataSourceFactory, extractorsFactory)
                .createMediaSource(uri)
            else -> delegate.createMediaSource(mediaItem)
        }
    }

from media.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.