示例#1
0
 public FlvParse(Stream stream)
 {
     Head = new FlvHead(stream);
     if (Head.Signature != "FLV")
     {
         throw new Exception("格式错误:" + Head.Signature);
     }
     while (true)
     {
         var    PreviousTagSize = stream.ReadUInt32();
         FlvTag flvTag          = FlvTag.createTag(stream);
         if (flvTag == null)
         {
             break;
         }
         var      data = flvTag.LoadTagData(stream);
         VideoTag v    = flvTag as VideoTag;
         if (v != null)
         {
             Videos.Add(v);
         }
         AudioTag a = flvTag as AudioTag;
         if (a != null)
         {
             Audios.Add(a);
         }
         ScriptDataTag d = flvTag as ScriptDataTag;
         if (d != null)
         {
             MediaData = d;
         }
         tags.Add(flvTag);
     }
 }
示例#2
0
        static async void play(string src, MediaElement media)
        {
            var c   = new HttpClient();
            var req = new HttpRequestMessage(HttpMethod.Get, new Uri(src));
            var res = await c.SendRequestAsync(req, HttpCompletionOption.ResponseHeadersRead);

            var url = req.RequestUri.ToString();

            var sr = await res.Content.ReadAsInputStreamAsync();

            var buf = await sr.ReadBuffer(11);

            FlvHead head = new FlvHead(buf.AsStream());

            if (head.Signature == "FLV")
            {
                Debug.WriteLine("FLV ## " + url);
                media.SetMediaStreamSource(new FlvReader(() => new HttpStream(url)).CreateSource());
                return;
            }

            if (url.IndexOf("mp4") != -1)
            {
                Debug.WriteLine("MP4 ## " + url);
                media.Source = new Uri(url);
                return;
            }

            new MessageDialog("不支持数据的格式").ShowAsync().GetResults();
            Debug.WriteLine("ERRRRRRRERRRRRRRERRRRRRRERRRRRRRERRRRRRRERRRRRRR");
        }
示例#3
0
        public FlvReader(Func <Stream> s)
        {
            this.CreateStream = s;

            Stream stream = this.CreateStream(); // new HttpStream(url);
            var    Head   = new FlvHead(stream); //9

            if (Head.Signature != "FLV")
            {
                throw new Exception("格式错误:" + Head.Signature);
            }
            while (FirstAudio == null || FirstVideo == null || MediaData == null)
            {
                this.readPreviousTag(stream);
            }


            //加载keyframes
            Task.Run(() => {
                double nxf = 0;
                double off = MediaData.Duration / 250;


                var obj = MediaData["keyframes"]?.GetObject();
                if (obj != null)
                {
                    times = obj["times"].GetArray().Select((d) => d.GetNumber()).ToList();
                    kfs   = obj["filepositions"].GetArray().Select((d) => (long)d.GetNumber()).ToList();
                    Debug.WriteLine("@@@ keyframes:ALL");
                    Debug.WriteLine("@@@ keyframes:ALL");
                }
                else
                {
                    Debug.WriteLine("@@@ keyframes:LOADSTART");
                    while (true)
                    {
                        var PreviousTagSize = stream.ReadUInt32();
                        FlvTag flvTag       = FlvTag.createTag(stream);
                        if (flvTag == null)
                        {
                            break;
                        }
                        var data = flvTag.LoadTagData(stream);
                        //关键帧
                        VideoTag tag = flvTag as VideoTag;
                        if (tag != null)
                        {
                            if (flvTag.TimeSpan.TotalSeconds >= nxf)
                            {
                                nxf += off;
                                times.Add(flvTag.TimeSpan.TotalSeconds);
                                kfs.Add(flvTag.Offset); //PreviousTagSize:4
                            }
                        }
                    }
                    Debug.WriteLine("@@@ keyframes:LOAdEND");
                }
            });
        }