I am trying to get the duration of a song loaded in load content. Whenever I try, the result is always zero. The song itself plays fine, but I cannot get the duration. Googling indicated that this problem could occur loading outside content manager, such as Song.FromURI, but I am using the content manager and it is still occuring. Here is the code I was using to test inside LoadContent:
var sng = Content.Load(“MenuSong0.wav”);
var v = sng.Duration;
Duration is always zero. It happens for all songs I try, not just this one.
What is going on here? How can I fix this?
I’ve been looking into the MonoGame source, and came across this in MonoGame.Framework/Content/ContentReaders/SongReader.cs line 76.
var durationMs = input.ReadObject<int>();
return new Song(path, durationMs);
I believe the problem might lie here. Looking at wav file specifications, the first 32 bits are not the duration of the file in milliseconds. This is not the case in mp3 files either, so I am unsure what this code is attempting to do.
The way you are loading the song is not actually loading as content. Using a file extension on the name makes it behave the same as Song.FromUri(), which does not calculate the song duration.
This backwards compatibility of ContentManager will be removed soon since the content pipeline is mostly complete and the backwards compatibility is no longer necessary.
Ahh, I see. Thank you for the help.