0

I am searching a way to get the bitrate and duration from a audio stream in C# on Windows Phone 7 platform.

First, I, tried to get it by calculate using formula (let's say that i can have duration of stream, that i am going to download as number)

bitrate = ((((filesize)/duration)/1024)*8)

I can download the audio (mp3) in stream from webclient and store or play it but i want to check for some rules first, for theese rules i need the bitrate that i can calculate without duration and bytes using some kind of formula as the audio stream may has variable bitrate and deeper calculations will be wrong.

Also sometimes, a part of the stream is downloaded within the app, so i do not have the full filesize and duration, so the bitrate is not right calculated, so i need a sure way.

So how can i get bitrate or duration from a stream using some function of Windows Phone 7 framework without actualy play it?

Stavros Koureas
  • 1,126
  • 12
  • 34
  • i used NAudio support for NVorbis in wp7 project within Nuget. and i used this code to get Bitrate(the significant) and Time and Bytes `using (Mp3FileReader reader = new Mp3FileReader(data)) { using (WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(reader)) { file_bytes = (long)pcmStream.Length; file_time = (int)pcmStream.TotalTime.TotalSeconds; file_rate = (int)pcmStream.WaveFormat.AverageBytesPerSecond; } }` But it crasing every time i run it even i call only: `using (Mp3FileReader reader = new Mp3FileReader(data)) { }` – Stavros Koureas Oct 24 '13 at 18:42
  • i also used id3 in wp7 project within Nuget and the code using: `Mp3Stream mp3 = new Mp3Stream(ex.Result, Mp3Permissions.Read)) { //Get mp3 stream info file_bytes = (long)ex.Result.Length; file_time = (int)(mp3.Audio.Duration.TotalSeconds); file_rate = (int)(mp3.Audio.Bitrate); }` but the file_rate for a song of 192kbps is 48kbps (4 times smaller) some other song of 192 is displayed as 128, why this isn't giving right results? – Stavros Koureas Oct 25 '13 at 10:20
  • i found a more elegant way to retrieve real duration do i then could find the bitrate(i would say more nearly to Microsoft sdk) which is: `AudioTrack temp = new AudioTrack(new Uri(file_in + ext + ".mp3", UriKind.Relative), name_in, null, null, null, null, EnabledPlayerControls.None); file_time = Convert.ToInt32(temp.Duration.TotalSeconds); file_rate = Convert.ToInt32(Math.Round((decimal)((file_bytes / file_time) / constant.bit) * 8));` but i get "COM Exception was unhandled" – Stavros Koureas Oct 26 '13 at 08:53

1 Answers1

0

The only solution seems to be, a well written class that i had made whitch reads the bytes of the song's header (mp3 container) and extracts details like duration and bitrate.

An another elegant solution i found whitch that does not meet my requirement is to be played first and use the AudioBackroundAgent to take theese details, then we can do the math with filesize and duration of track to get the bitrate!

Stavros Koureas
  • 1,126
  • 12
  • 34