You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ExoPlayer 2.6.1中HlsMediaSource()已废弃,求推荐HLS媒体替代方法

ExoPlayer 2.6.1: Replacement for Deprecated HlsMediaSource()

Hey there! I’ve run into this exact deprecation issue while working with older ExoPlayer versions, so let me break down the recommended replacement for you.

Starting from ExoPlayer 2.6.x, the direct HlsMediaSource() constructor was deprecated in favor of the HlsMediaSource.Factory pattern. This factory approach gives you more flexibility to configure HLS-specific settings and aligns with ExoPlayer's updated media source creation flow.

Step-by-Step Replacement

Here's how to switch from the old deprecated code to the recommended approach:

Old (Deprecated) Code:

// Don't use this anymore!
HlsMediaSource hlsSource = new HlsMediaSource(
    Uri.parse("your-hls-url.m3u8"),
    new DefaultHttpDataSourceFactory("user-agent"),
    mainHandler,
    null
);

New (Recommended) Code:

// 1. Create a default HTTP data source factory
DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("exoplayer-user-agent");

// 2. Initialize the HlsMediaSource.Factory
HlsMediaSource.Factory hlsFactory = new HlsMediaSource.Factory(dataSourceFactory);

// 3. Optional: Configure HLS-specific settings (e.g., playlist refresh interval, buffer settings)
hlsFactory.setPlaylistParserFactory(new DefaultHlsPlaylistParserFactory())
          .setAllowChunklessPreparation(true);

// 4. Create the final HlsMediaSource
HlsMediaSource hlsSource = hlsFactory.createMediaSource(Uri.parse("your-hls-url.m3u8"));

Key Notes

  • Flexibility: The factory pattern lets you easily add customizations like cache support (by wrapping the DefaultHttpDataSourceFactory with a CacheDataSourceFactory) or DRM configuration (using setDrmSessionManager()).
  • Backward Compatibility: Even in 2.6.1, this factory method is fully supported and is the path forward—if you plan to upgrade ExoPlayer to newer versions later, this code will still work without major changes.
  • Handler & Listener: If you previously used a handler or event listener in the old constructor, you can attach them to the player instance directly instead of passing them to the media source.

内容的提问来源于stack exchange,提问作者Algar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:31:05