Пример #1
0
        async void MSS_SampleRequested(Windows.Media.Core.MediaStreamSource sender, MediaStreamSourceSampleRequestedEventArgs args)
        {
            MediaStreamSourceSampleRequest request = args.Request;

            // check if the sample requested byte offset is within the file size

            if (byteOffset + sampleSize <= mssStream.Size)
            {
                MediaStreamSourceSampleRequestDeferral deferal = request.GetDeferral();
                IInputStream inputStream = mssStream.GetInputStreamAt(byteOffset);

                // create the MediaStreamSample and assign to the request object.
                // You could also create the MediaStreamSample using createFromBuffer(...)

                MediaStreamSample sample = await MediaStreamSample.CreateFromStreamAsync(inputStream, sampleSize, timeOffset);

                sample.Duration = sampleDuration;
                sample.KeyFrame = true;

                // increment the time and byte offset

                byteOffset    += sampleSize;
                timeOffset     = timeOffset.Add(sampleDuration);
                request.Sample = sample;
                deferal.Complete();
            }
        }
Пример #2
0
        public override void GetSampleAsync(MediaStreamType mediaStreamType)
        {
            Task.Run(() =>
            {
                MediaStreamSample sample;
                try
                {
                    IBuffer winrtbuf = System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBuffer.Create(_pSample, 0, _pSample.Length, _pSample.Length);
                    sample           = new MediaStreamSample(null, winrtbuf, 0, _pSample.Length, nextTimestamp, SampleDuration, null);
                    nextTimestamp   += SampleDuration;
                }
                catch (Exception e)
                {
                    ErrorOccurred(e.Message);
                    return;
                }

                ReportGetSampleCompleted(sample);
            });
        }
Пример #3
0
        protected override void GetSampleAsync(MediaStreamType mediaStreamType)
        {
            Mp3Frame          frame;
            MediaStreamSample sample;
            Dictionary <MediaSampleAttributeKeys, string> attribs = new Dictionary <MediaSampleAttributeKeys, string> ();

            //string format = "HH:mm:ss.ffff";
            //if (opened == DateTime.MinValue)
            //    opened = DateTime.Now;
            //Debug.WriteLine ("{0} GetSampleAsync stamp: {1}", (DateTime.Now - opened).ToString (), TimeSpan.FromMilliseconds (current_pts / 10000).ToString ());

            try {
                if (this.frame != null)
                {
                    frame      = this.frame;
                    this.frame = null;
                }
                else
                {
                    frame = Mp3Frame.Read(stream);
                }

                sample = new MediaStreamSample(description, new MemoryStream(frame.data), 0, frame.data.Length, current_pts, attribs);

                current_pts += frame.Duration;

                ReportGetSampleCompleted(sample);
            } catch (System.IO.EndOfStreamException ex) {
                Console.WriteLine(ex);
                sample = new MediaStreamSample(description, null, 0, 0, 0, attribs);
                ReportGetSampleCompleted(sample);
            } catch (Exception ex) {
                Console.WriteLine(ex);
                ReportGetSampleCompleted(null);
            }
        }
 //
 // Summary:
 //     Developers call this method in response to System.Windows.Media.MediaStreamSource.GetSampleAsync(System.Windows.Media.MediaStreamType)
 //     to give the System.Windows.Controls.MediaElement the next media sample to
 //     be rendered, or to report the end of a stream.
 //
 // Parameters:
 //   mediaStreamSample:
 //     The description of the media stream that this sample came from. Passing nullSystem.Windows.Media.MediaStreamSample
 //     object indicates that the stream has ended.
 protected void ReportGetSampleCompleted(MediaStreamSample mediaStreamSample)
 {
     _service.ReportGetSampleCompleted(mediaStreamSample.InnerSample);
 }