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
DefaultHttpDataSourceFactorywith aCacheDataSourceFactory) or DRM configuration (usingsetDrmSessionManager()). - 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
相关产品推荐
相关产品推荐

