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

Android中GraphView与MediaPlayer不同步问题求助

Fixing GraphView and MediaPlayer Sync Issues

Hey Richard, let's tackle this sync problem between your pitch visualization in GraphView and MediaPlayer—this is a common pain point with audio visualizations, so let's break down actionable fixes and best practices:

1. Align Time Units and Sources

First, make sure your time references are consistent:

  • Your Pitch.occuranceTime is parsed as seconds (e.g., 0,14 becomes 0.14f), but MediaPlayer.getCurrentPosition() returns milliseconds. Always convert to the same unit—either convert MediaPlayer's value to seconds (currentPosition / 1000f) or your pitch times to milliseconds.
  • Be aware that getCurrentPosition() can have slight drift or delay, especially after seeking. Use MediaPlayer.OnSeekCompleteListener to reset your GraphView's position whenever a seek finishes, ensuring both are aligned after user interactions.

2. Optimize GraphView Updates for Smooth Sync

Avoid jank that breaks sync with these tweaks:

  • Use a Handler or coroutine (if using Kotlin) to update the GraphView at regular intervals (e.g., every 50-100ms) instead of updating on every pitch data point. This matches typical UI refresh rates and reduces overhead.
  • Pre-sort your listOfPitches by occuranceTime (add this to your getListOfPitches() method):
    Collections.sort(listOfPitches, Comparator.comparing(Pitch::getOccuranceTime));
    
    Then use binary search to quickly find the pitch data corresponding to the current MediaPlayer time—this avoids looping through the entire list every update, which causes lag.

3. Account for Audio Playback Latency

System-level audio latency can make getCurrentPosition() feel out of sync with what the user hears:

  • Record the exact timestamp when playback starts (e.g., long playbackStartTime = System.currentTimeMillis(); right after calling mediaPlayer.start()).
  • Calculate the actual playback time using this timestamp instead of relying solely on getCurrentPosition():
    float actualPlaybackTime = (System.currentTimeMillis() - playbackStartTime) / 1000f;
    
    Use this value to match your pitch data—this compensates for any delay between calling start() and the audio actually reaching the speaker.

4. Validate Pitch Data Parsing

Double-check that your pitch time parsing is correct:

  • Confirm your raw .txt file uses commas as decimal separators (your code replaces commas with dots, which is correct for parsing floats in Java). If some entries have typos or incorrect formatting, it could create time gaps or wrong positions.
  • Add debug logs to verify parsed values:
    Log.d("PitchParser", "Time: " + pitch.getOccuranceTime() + ", Pitch: " + pitch.getPitch());
    
    Compare these logs with your raw data to ensure no parsing errors.

5. Debug and Test with Visual Feedback

Add temporary UI elements to diagnose sync issues:

  • Display the current MediaPlayer time (converted to seconds) and the GraphView's currently focused pitch time side by side. If they drift apart over time, you're dealing with cumulative error (likely from inconsistent update intervals).
  • Test with a short, well-calibrated audio clip where you know exact pitch timings (e.g., a tone at 0.5s, another at 1.0s) to confirm your sync logic works for simple cases before scaling to full songs.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:10:59