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

使用ExoPlayer 2.7.1播放缓存目录.tmp文件失败求助

解决ExoPlayer 2.7.1无法播放缓存目录.tmp文件的问题

嘿,我来帮你搞定这个问题!首先得明确一点:你直接去播放缓存目录里的.tmp文件肯定行不通,原因很简单——这些临时文件是ExoPlayer在下载/缓存过程中生成的不完整分片或临时存储文件,它们没有标准媒体文件的完整元数据和容器结构,ExoPlayer根本没法正常解析它们。

下面给你两个针对性的解决方案,根据你的使用场景选就行:

场景1:你用的是ExoPlayer的Download功能(下载媒体到本地)

如果是通过ExoPlayer的DownloadManager下载的内容,别直接碰.tmp文件,应该通过DownloadManager来获取已完成下载的媒体Uri,再交给播放器播放。示例代码如下:

// 初始化DownloadManager(要和你下载时用的是同一个实例)
SimpleCache downloadCache = new SimpleCache(new File(getCacheDir(), "downloads"), new NoOpCacheEvictor());
DownloadManager downloadManager = DownloadManager.getInstance(this);
DownloadManager.Request request = DownloadManager.Request.createFromUri(Uri.parse("你的媒体原地址"));

// 查询已完成的下载任务
Download download = downloadManager.getDownload(request.id);
if (download != null && download.state == Download.STATE_COMPLETED) {
    // 获取可播放的Uri
    Uri playableUri = downloadManager.getDownloadUri(download.id);
    
    // 初始化ExoPlayer并设置媒体源
    SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(this);
    MediaSource mediaSource = new ExtractorMediaSource.Factory(DATA_SOURCE_FACTORY)
            .createMediaSource(playableUri);
    player.prepare(mediaSource);
    player.setPlayWhenReady(true);
}

场景2:你用的是ExoPlayer的缓存功能(边播边存)

如果是播放器自动缓存的内容,应该用CacheDataSourceFactory来加载缓存的媒体,而不是直接读取.tmp文件。这样ExoPlayer会自动处理缓存文件的拼接和解析:

// 创建缓存实例
SimpleCache cache = new SimpleCache(new File(getCacheDir(), "media_cache"), new LeastRecentlyUsedCacheEvictor(100 * 1024 * 1024)); // 100MB缓存
CacheDataSourceFactory cacheDataSourceFactory = new CacheDataSourceFactory(
        cache,
        DATA_SOURCE_FACTORY,
        CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR
);

// 创建媒体源并播放
SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(this);
MediaSource mediaSource = new ExtractorMediaSource.Factory(cacheDataSourceFactory)
        .createMediaSource(Uri.parse("你的媒体原地址"));
player.prepare(mediaSource);
player.setPlayWhenReady(true);

额外提醒

如果你是手动下载的.tmp文件,那得先确认下载是否完全完成——很多下载工具会把未完成的文件命名为.tmp,完成后才会改成对应的媒体格式(比如.mp3、.mp4)。这种情况下你需要先重命名文件,再尝试播放。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:24:31