Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set defaultPositionUs of a Window in the Timeline? #1129

Closed
akshdeep-singh opened this issue Feb 24, 2024 · 2 comments
Closed

How to set defaultPositionUs of a Window in the Timeline? #1129

akshdeep-singh opened this issue Feb 24, 2024 · 2 comments
Assignees

Comments

@akshdeep-singh
Copy link

I want to set default start positions of my playlist items (basically to resume from where the user left), to use when playlist item auto transitions to next item.

As per my understanding (I am not sure), the default start position is of the window in the timeline.

From Docs: Timeline.Window.defaultPositionUs. But, I can't find a way to set this defaultPositionUs parameter.

I use this function to set playlist items: setMediaItems. My playlist items are mp3 files (local and https source).

@marcbaechinger
Copy link
Contributor

marcbaechinger commented Feb 27, 2024

I think you can do this only if you own the media. This is AFAIK used with live streams.

The feature request for VOD is a duplicate of #6373 and #1041 I think.

You can use MediaItem.ClippingProperties to clip a media item. However, this removes the part before the clipping start position. A user can't seek back beyond this because it's clipped. So this isn't exactly the same as a start position but about the same as a default position. :)

If this isn't helpful you need to use a seek for this. This was discussed in #1041

@akshdeep-singh
Copy link
Author

I got a solution based on this google/ExoPlayer#9122 (comment):

class MyMediaSource(mediaSource: MediaSource, private val startPositionMs: Long): WrappingMediaSource(mediaSource) {
    override fun onChildSourceInfoRefreshed(newTimeline: Timeline) {
        super.onChildSourceInfoRefreshed(MyTimeline(newTimeline, startPositionMs))
    }

    class MyTimeline(timeline: Timeline, private val startPositionMs: Long): ForwardingTimeline(timeline) {
        override fun getWindow(windowIndex: Int, window: Window, defaultPositionProjectionUs: Long): Window {
            return super.getWindow(windowIndex, window, defaultPositionProjectionUs).apply {
                defaultPositionUs = Util.msToUs(startPositionMs)
            }
        }
    }
}
val defaultMediaSourceFactory = DefaultMediaSourceFactory(this)
val mediaSourceFactory = object : MediaSource.Factory {
    override fun setDrmSessionManagerProvider(drmSessionManagerProvider: DrmSessionManagerProvider): MediaSource.Factory {
        return defaultMediaSourceFactory.setDrmSessionManagerProvider(drmSessionManagerProvider)
    }

    override fun setLoadErrorHandlingPolicy(loadErrorHandlingPolicy: LoadErrorHandlingPolicy): MediaSource.Factory {
        return defaultMediaSourceFactory.setLoadErrorHandlingPolicy(loadErrorHandlingPolicy)
    }

    override fun getSupportedTypes(): IntArray {
        return defaultMediaSourceFactory.supportedTypes
    }

    override fun createMediaSource(mediaItem: MediaItem): MediaSource {
        return MyMediaSource(
            defaultMediaSourceFactory.createMediaSource(mediaItem),
            mediaItem.mediaMetadata.extras?.getLong("startPositionMs") ?: 0,
        )
    }
}

player = ExoPlayer.Builder(this)
    .setMediaSourceFactory(mediaSourceFactory)
    .build()

@androidx androidx locked and limited conversation to collaborators Apr 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants