How to get audio current play time using XAudio2?


  • I used to use following method to calculate music playback time

    int YHMAudio::GetMusicPlayTime(size_t index)
    	double totalTime = (double) mYHMVoiceEngine[index].musicStreamer->GetMaxStreamLengthInBytes() / (double) mYHMVoiceEngine[index].musicStreamer->GetAvgBytesPerSec() * 1000.0f;
    	double totalTimeSecond = totalTime / 1000.0l;
    	UINT64 totalSample = (UINT64) (totalTimeSecond * (double) mYHMVoiceEngine[index].musicStreamer->GetSamplesPerSec());
    	return (int) ((double) (state.SamplesPlayed % totalSample) / (double) totalSample * totalTime);

    But when I call IMFSourceReader::SetPosition, the state.SamplesPlayed won't update.

    I want to know is there other method to get current music playback time?Any help will be appreciated!

    Tuesday, April 28, 2015 5:51 AM

All replies

  • You haven't specified, but based on the use of GetState without a second parameter, you are using XAudio 2.7 on Windows desktop rather than XAudio 2.8 on a Windows 8.x Store, Windows phone 8.x, or Xbox One, correct?
    Tuesday, April 28, 2015 6:17 PM
  • No, I'm using xaudio 2.8 on universal app!The GetState only have two parameters(state and flags). 
    Wednesday, April 29, 2015 1:57 AM