Detecting .wav file duration in resources

I'm currently looking for a solution to my problem, as my audio file (.wav) is in the resources of my solution. I need to determine how long he will play.

How to calculate the duration of sound in my resources? Is there a way to determine how long it will take to play a specific audio file in my resource?

+6
source share
2 answers

You can use NAudio to read the stream of resources and get the duration

using NAudio.Wave; ... WaveFileReader reader = new WaveFileReader(MyProject.Resource.AudioResource); TimeSpan span = reader.TotalTime; 

You can also do what Matthew Watson suggested and just keep the length as an extra string resource.

In addition, this SO question has many ways to determine duration, but most of them are for files, not for streams captured by resources.

+4
source

I have a better solution for your problem, without any library. First read all the bytes from the file. (you can also use FileSteam to read the necessary bytes only if you wish)

 byte[] allBytes = File.ReadAllBytes(filePath); 

After you have received all the bytes from your file, you need to receive bits per second. This value is usually stored in bytes with indices 28-31.

 int bitrate = BitConverter.ToInt32(new[] { allBytes[28], allBytes[29], allBytes[30], allBytes[31] }, 0) * 8; 

Now you can already get the duration in seconds.

 int duration = (allBytes.Length - 8) * 8 / bitrate; 
+2
source

Source: https://habr.com/ru/post/1200402/


All Articles