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

ExoPlayer升级后无法播放MP3/MP4文件的问题求助

ExoPlayer 2.18.4 播放自有MP3/MP4文件失败问题解决

问题概述

  • 上线近3年的旧应用升级ExoPlayer到2.18.4版本后,示例MPD(DASH)文件可正常播放,但自有MP3/MP4文件播放报错
  • 旧版ExoPlayer可正常播放这些自有文件,已排查网络问题无进展

错误根源分析

从异常日志中可定位核心问题:

  1. 解析逻辑错误:日志中出现XmlPullParserException: unterminated entity ref,说明ExoPlayer错误地将普通MP3文件当作XML格式的DASH manifest(MPD)解析,导致解析失败
  2. 媒体源类型不匹配:代码中强制使用DashMediaSource.Factory创建媒体源,而DashMediaSource仅适用于DASH流媒体协议(MPD格式文件),不能直接加载普通MP3/MP4这类渐进式媒体文件

解决方案

1. 匹配媒体类型选择正确的MediaSource

针对普通MP3/MP4文件,需使用ProgressiveMediaSource.Factory替代DashMediaSource.Factory;若需同时支持DASH和普通媒体,可根据URL后缀自动判断:

private fun preparePlayer() {
    exoPlayer = ExoPlayer.Builder(this).build()
    exoPlayer?.playWhenReady = playWhenReady
    binding.playerView.player = exoPlayer
    val defaultHttpDataSourceFactory = DefaultHttpDataSource.Factory()
    
    val mediaItem = MediaItem.fromUri(URL)
    val mediaSource = if (URL.endsWith(".mpd")) {
        // 处理DASH流媒体
        DashMediaSource.Factory(defaultHttpDataSourceFactory).createMediaSource(mediaItem)
    } else {
        // 处理普通MP3/MP4等渐进式媒体
        ProgressiveMediaSource.Factory(defaultHttpDataSourceFactory).createMediaSource(mediaItem)
    }
    
    exoPlayer?.setMediaSource(mediaSource)
    exoPlayer?.seekTo(playbackPosition)
    exoPlayer?.prepare()
}

2. 清理Gradle依赖冲突

当前Gradle配置同时引入了androidx.media3:media3-exoplayer-ima:1.0.0和com.google.android.exoplayer:extension-ima:2.18.4,两者存在依赖冲突,需统一依赖版本:

// 保留ExoPlayer 2.18.4的依赖,移除media3相关冲突项
implementation 'com.google.android.exoplayer:exoplayer:2.18.4'
implementation 'com.google.android.exoplayer:extension-ima:2.18.4'

额外验证建议

  • 替换代码中的URL为自有MP3地址,测试播放是否正常
  • 检查自有媒体文件的服务器响应头,确保Content-Type正确(如MP3对应audio/mpeg,MP4对应video/mp4)
  • 确认自有媒体文件未损坏,可直接下载到本地测试播放

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 18:00:30