public static byte[] ReadStreamToByteArray(this IRandomAccessStream oriStream) { var stream = oriStream.AsStreamForRead(); stream.Seek(0, SeekOrigin.Begin); var data = new byte[stream.Length]; stream.Read(data, 0, (int)stream.Length); return data; }
/// <summary> /// Read off the Id3Data from the stream and return the first MpegFrame of the audio stream. /// This assumes that the first bit of data is either an ID3 segment or an MPEG segment. Calls a separate thread /// to read past ID3v2 data. /// </summary> /// <param name="randomAccessStream"></param> /// <returns>The first MpegFrame.</returns> public static MpegFrame ReadPastId3V2Tags(this IRandomAccessStream randomAccessStream) { MpegFrame mpegFrame = null; var audioStream = randomAccessStream.AsStreamForRead(); if (audioStream != null) { // Read and (throw out) any Id3 data if present. byte[] data = new byte[10]; audioStream.Position = 0; if (audioStream.Read(data, 0, 3) != 3) { goto cleanup; } if (data[0] == 73 /* I */ && data[1] == 68 /* D */ && data[2] == 51 /* 3 */) { // Need to update to read the is footer present flag and account for its 10 bytes if needed. if (audioStream.Read(data, 3, 7) != 7) { goto cleanup; } int id3Size = BitTools.ConvertSyncSafeToInt32(data, 6); int bytesRead = 0; // Read through the ID3 Data tossing it out.) while (id3Size > 0) { bytesRead = (id3Size - buffer.Length > 0) ? audioStream.Read(buffer, 0, buffer.Length) : audioStream.Read(buffer, 0, id3Size); id3Size -= bytesRead; } mpegFrame = new MpegFrame(audioStream); } else { // No ID3 tag present, presumably this is streaming and we are starting right at the Mp3 data. // Assume the stream isn't seekable. if (audioStream.Read(data, 3, 1) != 1) { goto cleanup; } mpegFrame = new MpegFrame(audioStream, data); } } return mpegFrame; // Cleanup and quit if you couldn't even read the initial data for some reason. cleanup: throw new Exception("Could not read intial audio stream data"); }