Example #1
0
        /// <summary>
        /// Parses the M3U playlist.
        /// </summary>
        /// <param name="textReader">TextReader representing the M3U playlist.</param>
        /// <returns>Parsed M3U playlist.</returns>
        private IPlaylist Parse(TextReader textReader)
        {
            if (textReader == null)
            {
                throw new ArgumentNullException("textReader");
            }

            string line = textReader.ReadLine();

            if (line == null)
            {
                throw new ArgumentException("Invalid M3U playlist.");
            }

            M3uPlaylist playlist = new M3uPlaylist();
            ICollection<IPlaylistItem> items = playlist.Items;

            bool isExtended = line.Equals(M3uParser.M3uExtendedHeader);

            if (isExtended)
            {
                while ((line = textReader.ReadLine()) != null)
                {
                    string extendedDetail = line;
                    string detail = textReader.ReadLine();

                    if ((extendedDetail == null) || (detail == null))
                    {
                        throw new Exception("File is malformed");
                    }

                    Match match = M3uParser.extendedDetailRegex.Match(extendedDetail);
                    if (!match.Success)
                    {
                        throw new Exception("Invalid m3u extended detail line");
                    }

                    items.Add(new M3uPlaylistItem()
                    {
                        DisplayName = match.Groups["file"].Value,
                        Path = detail,
                        Length = new TimeSpan(0, 0, int.Parse(match.Groups["seconds"].Value))
                    });
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(line))
                {
                    items.Add(new M3uPlaylistItem()
                    {
                        DisplayName = line,
                        Path = line,
                        Length = new TimeSpan(0, 0, -1)
                    });
                }

                while ((line = textReader.ReadLine()) != null)
                {
                    if (string.IsNullOrEmpty(line))
                    {
                        continue;
                    }

                    items.Add(new M3uPlaylistItem()
                    {
                        DisplayName = line,
                        Path = line,
                        Length = new TimeSpan(0, 0, -1)
                    });
                }
            }

            return playlist;
        }
Example #2
0
        /// <summary>
        /// Parses the M3U playlist.
        /// </summary>
        /// <param name="textReader">TextReader representing the M3U playlist.</param>
        /// <returns>Parsed M3U playlist.</returns>
        private IPlaylist Parse(TextReader textReader)
        {
            if (textReader == null)
            {
                throw new ArgumentNullException("textReader");
            }

            string line = textReader.ReadLine();

            if (line == null)
            {
                throw new ArgumentException("Invalid M3U playlist.");
            }

            M3uPlaylist playlist = new M3uPlaylist();
            ICollection <IPlaylistItem> items = playlist.Items;

            bool isExtended = line.Equals(M3uParser.M3uExtendedHeader);

            if (isExtended)
            {
                while ((line = textReader.ReadLine()) != null)
                {
                    string extendedDetail = line;
                    string detail         = textReader.ReadLine();

                    if ((extendedDetail == null) || (detail == null))
                    {
                        throw new Exception("File is malformed");
                    }

                    Match match = M3uParser.extendedDetailRegex.Match(extendedDetail);
                    if (!match.Success)
                    {
                        throw new Exception("Invalid m3u extended detail line");
                    }

                    items.Add(new M3uPlaylistItem()
                    {
                        DisplayName = match.Groups["file"].Value,
                        Path        = detail,
                        Length      = new TimeSpan(0, 0, int.Parse(match.Groups["seconds"].Value))
                    });
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(line))
                {
                    items.Add(new M3uPlaylistItem()
                    {
                        DisplayName = line,
                        Path        = line,
                        Length      = new TimeSpan(0, 0, -1)
                    });
                }

                while ((line = textReader.ReadLine()) != null)
                {
                    if (string.IsNullOrEmpty(line))
                    {
                        continue;
                    }

                    items.Add(new M3uPlaylistItem()
                    {
                        DisplayName = line,
                        Path        = line,
                        Length      = new TimeSpan(0, 0, -1)
                    });
                }
            }

            return(playlist);
        }