Пример #1
0
        private void CreateTableOfContents()
        {
            try
            {
                // Just a guess at how many entries we'll need so the internal array need not resize very much
                // 400 bytes per frame is probably a good enough approximation.
                TableOfContents = new List <Mp3Index>();
                Mp3Frame frame = null;
                do
                {
                    var index = new Mp3Index();
                    index.FilePosition   = Mp3Stream.Position;
                    index.SamplePosition = totalSamples;
                    frame = ReadNextFrame(false);
                    if (frame != null)
                    {
                        ValidateFrameFormat(frame);

                        totalSamples      += frame.SampleCount;
                        index.SampleCount  = frame.SampleCount;
                        index.ByteCount    = (int)(Mp3Stream.Position - index.FilePosition);
                        tableCreatPosition = index.FilePosition + index.ByteCount;
                        TableOfContents.Add(index);
                    }
                } while (frame != null);
            }
            catch (EndOfStreamException)
            {
                // not necessarily a problem
            }
        }
Пример #2
0
        private void CreateTableBackground(object obj)
        {
            while (!this.tableCreated)
            {
                lock (repositionLock)
                {
                    Mp3Index lastIndex = this.TableOfContents.Last();
                    if (lastIndex != null)
                    {
                        var lastStreamPosition = Mp3Stream.Position;
                        Mp3Stream.Position = tableCreatPosition;

                        Mp3Frame frame = null;
                        try
                        {
                            do
                            {
                                var index = new Mp3Index();
                                index.FilePosition   = Mp3Stream.Position;
                                index.SamplePosition = totalSamples;
                                frame = ReadNextFrame(false);
                                if (frame != null)
                                {
                                    ValidateFrameFormat(frame);

                                    totalSamples      += frame.SampleCount;
                                    index.SampleCount  = frame.SampleCount;
                                    index.ByteCount    = (int)(Mp3Stream.Position - index.FilePosition);
                                    tableCreatPosition = index.FilePosition + index.ByteCount;
                                    TableOfContents.Add(index);
                                }
                            } while (frame != null);
                        }
                        catch (Exception)
                        {
                        }

                        if (!Mp3Stream.CanRead)
                        {
                            if (frame == null)
                            {
                                this.tableCreated = true;
                            }
                        }

                        Mp3Stream.Position = lastStreamPosition;
                    }
                }

                Thread.Sleep(1000);
            }
        }