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

Expo-AV在React Native中didJustFinish播放后始终不置true求助

解决expo-av音频播放didJustFinish不触发的问题
  • 检查事件监听绑定时机
    必须在调用playAsync()前绑定setOnPlaybackStatusUpdate监听,避免播放过程中才注册导致错过完成事件。同时要注意动态创建的Sound实例需每次重新绑定,防止实例回收后监听失效:

    import { Audio } from 'expo-av';
    
    let soundInstance = new Audio.Sound();
    
    async function startPlayback() {
      await soundInstance.loadAsync(require('./assets/audio.mp3'));
      // 先绑定监听再启动播放
      soundInstance.setOnPlaybackStatusUpdate((status) => {
        if (status.didJustFinish && !status.isLooping) {
          console.log('播放完成');
          soundInstance.unloadAsync();
        }
      });
      await soundInstance.playAsync();
    }
    
  • 排查音频文件问题

    • 确认音频文件完整无损坏,用本地播放器验证能正常播放至结束;
    • 优先使用MP3、AAC等expo-av官方兼容格式,避免编码异常的小众格式;
    • 远程音频需确保下载完整,流式播放可能导致状态判断偏差,可先下载到本地再播放。
  • 补充状态判断逻辑
    不要仅依赖didJustFinish,结合播放位置和时长做兜底判断,避免SDK的状态判断遗漏:

    soundInstance.setOnPlaybackStatusUpdate((status) => {
      if (!status.isLoaded) return;
      // 双重校验:官方状态或播放位置接近结束且已停止
      const isPlaybackCompleted = status.didJustFinish || 
        (!status.isPlaying && status.positionMillis >= status.durationMillis - 100);
      if (isPlaybackCompleted && !status.isLooping) {
        console.log('播放完成');
        soundInstance.unloadAsync();
      }
    });
    
  • 升级依赖并清除缓存
    旧版expo-av存在didJustFinish相关bug,升级到最新稳定版并清除项目缓存:

    expo install expo-av@latest
    expo r -c
    
  • 排除项目干扰因素
    新建极简Expo项目,只保留expo-av播放逻辑,验证是否能正常触发事件。如果极简案例正常,说明问题出在原项目的其他代码(如状态管理冲突、重复监听等)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 20:41:02