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

升级Expo SDK 51后Android端onPlaybackStatusUpdate回调触发异常

Expo SDK51 Android端expo-av onPlaybackStatusUpdate回调异常解决方案

针对你升级Expo SDK至51后,Android端onPlaybackStatusUpdate仅在播放开始/停止时触发的问题,提供以下可行方案:

方案1:手动定时查询播放状态

由于Android端回调触发逻辑变更,可通过主动定时调用getStatusAsync()替代被动回调,模拟原有的100ms更新频率:

import { Audio } from 'expo-av';
import { useEffect, useRef } from 'react';

const loadAudio = async () => {
  const soundObject = new Audio.Sound();
  try {
    await soundObject.loadAsync(require('./path/to/audio.mp3'));
    return soundObject;
  } catch (error) {
    console.error('Error loading audio:', error);
    return null;
  }
};

const AudioPlayer = () => {
  const soundRef = useRef(null);
  const statusIntervalRef = useRef(null);

  useEffect(() => {
    const initAudio = async () => {
      const sound = await loadAudio();
      if (sound) soundRef.current = sound;
    };
    initAudio();

    // 组件卸载时清理资源
    return () => {
      if (statusIntervalRef.current) clearInterval(statusIntervalRef.current);
      if (soundRef.current) soundRef.current.unloadAsync();
    };
  }, []);

  const startPlaying = async () => {
    if (!soundRef.current) return;
    await soundRef.current.playAsync();
    
    // 每100ms查询一次播放状态
    statusIntervalRef.current = setInterval(async () => {
      const status = await soundRef.current.getStatusAsync();
      console.log('Playback status:', status);
      
      // 播放停止时自动清除定时器
      if (!status.isPlaying) clearInterval(statusIntervalRef.current);
    }, 100);
  };

  const stopPlaying = async () => {
    if (!soundRef.current) return;
    await soundRef.current.stopAsync();
    if (statusIntervalRef.current) clearInterval(statusIntervalRef.current);
  };

  return (
    <div>
      <button onClick={startPlaying}>播放</button>
      <button onClick={stopPlaying}>停止</button>
    </div>
  );
};

方案2:确认expo-av版本兼容性

检查项目中expo-av的版本是否与SDK51完全兼容,尝试升级至同SDK通道的最新补丁版本,部分版本可能已修复该回调触发异常问题。

方案3:配置Android音频模式

SDK51可能调整了Android端默认音频模式,尝试显式设置播放模式以激活状态更新:

// 在初始化音频前调用
await Audio.setAudioModeAsync({
  allowsRecordingIOS: false,
  playsInSilentModeIOS: true,
  staysActiveInBackground: false,
  shouldDuckAndroid: true,
  playThroughEarpieceAndroid: false,
});

说明

该问题属于Expo SDK51与expo-av在Android端的适配问题,官方后续补丁可能会修复。上述方案可作为临时解决手段,优先推荐方案1,能稳定模拟原回调的更新频率。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 20:05:53