public static async Task <MediaStreamSample> CreateVideoSample(this VideoTag tag, VideoTag FirstTag, bool hasHead) { var stream = tag.createVideoStream(FirstTag, hasHead);// flv.createVideoStream(stream.AsStream(), vi); var sample = await MediaStreamSample.CreateFromStreamAsync( stream.AsInputStream(), (uint)stream.Length, tag.TimeSpan); //每一段的数据大小 //sample.Duration = tag.TimeSpan;//BUG return(sample); #region MyRegion //Debug.WriteLine("CreateAudioSample:" + tag.Count + " " + tag.GetDataStream().Length); //Debug.WriteLine(BitConverter.ToString( tag.GetDataInputStream().AsStreamForRead().ReadBytes(30) )); /* * var stream = tag.createVideoStream(Videos[0], vi <=2); * var si = stream.AsInputStream(); * var sample = await MediaStreamSample.CreateFromStreamAsync(si, (uint)stream.Length, tag.TimeSpan);//每一段的数据大小 */ #endregion }
public static async Task <MediaStreamSample> CreateAudioSample(this AudioTag tag) { var stream = tag.GetDataStream(); var sample = await MediaStreamSample.CreateFromStreamAsync( stream.AsInputStream(), (uint)stream.Length, tag.TimeSpan);//每一段的数据大小 //sample.Duration = tag.TimeSpan;//BUG return(sample); #region MyRegion //Debug.WriteLine(tag.GetDataStream().toString(30)); //var ss = tag.GetDataInputStream(); //var sample = await MediaStreamSample.CreateFromStreamAsync(ss, tag.Count, tag.TimeSpan);//每一段的数据大小 //return sample; /* * var stream = tag.GetDataStream();//.GetDataStream().AsRandomAccessStream().GetInputStreamAt(); * var si = stream.AsInputStream(); * var sample = await MediaStreamSample.CreateFromStreamAsync(si, (uint)stream.Length, tag.TimeSpan); */ //stream.Position = (long)a.Offset;//问题的并发式或交错操作改变了对象的状态,无效此操作 //var ss = stream.AsInputStream(); //A concurrent or interleaved operation changed the state of the object, invalidating this operation. (Exception from HRESULT: 0x8000000C) //var ss = stream.AsRandomAccessStream().GetInputStreamAt(a.Offset);//success //var ss = stream.GetInputStreamAt(a.Offset);//success //sample.Duration = a.TimeSpan - at; //每一段进度条移动距离 //sample.KeyFrame = true; #endregion }
async Task <MediaStreamSource> createMediaStream2(string url = "ms-appx:///Assets/test.mp3") { var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new Uri(url)); var stream = await file.OpenReadAsync(); MusicProperties mp3FileProperties = await file.Properties.GetMusicPropertiesAsync(); List <string> encodingPropertiesToRetrieve = new List <string>(); encodingPropertiesToRetrieve.Add("System.Audio.SampleRate"); encodingPropertiesToRetrieve.Add("System.Audio.ChannelCount"); encodingPropertiesToRetrieve.Add("System.Audio.EncodingBitrate"); var encodingProperties = await file.Properties.RetrievePropertiesAsync(encodingPropertiesToRetrieve); uint sampleRate = (uint)encodingProperties["System.Audio.SampleRate"]; uint channelCount = (uint)encodingProperties["System.Audio.ChannelCount"]; uint bitRate = (uint)encodingProperties["System.Audio.EncodingBitrate"]; /* * 44100 2 128000 * 44100 2 171896 music: 00:03:33.2114285 * 44100 2 130000 music: 00:00:30.0930000 * SampleRate/SamplesPerSec * Stereo/Channels * SR*SO*SS/BA * * 130.3 16 2 * DataRate * SampleSize/BitsPerSample * BlockAlign */ // var audiodesc = new AudioStreamDescriptor(AudioEncodingProperties.CreateMp3(44100, 2, 128000)); var audiodesc = new AudioStreamDescriptor(AudioEncodingProperties.CreateMp3(sampleRate, channelCount, bitRate)); var c = new MediaStreamSource(audiodesc); c.Duration = mp3FileProperties.Duration; c.CanSeek = true; Debug.WriteLine("music: " + c.Duration); Debug.WriteLine(mp3FileProperties.Title + " " + sampleRate + " " + channelCount + " " + bitRate); UInt32 sampleSize = 300; //每一段 TimeSpan sampleDuration = new TimeSpan(0, 0, 0, 0, 70); //每一段进度条移动距离 ulong byteOffset = 0; TimeSpan timeOffset = new TimeSpan(0); c.Starting += (s, e) => { Debug.WriteLine("==Starting=="); MediaStreamSourceStartingRequest request = e.Request; if ((request.StartPosition != null) && request.StartPosition.Value <= c.Duration) { UInt64 sampleOffset = (UInt64)request.StartPosition.Value.Ticks / (UInt64)sampleDuration.Ticks; timeOffset = new TimeSpan((long)sampleOffset * sampleDuration.Ticks); byteOffset = sampleOffset * sampleSize; } request.SetActualStartPosition(timeOffset); }; c.SampleRequested += async(s, e) => { //Debug.WriteLine(timeOffset); var deferal = e.Request.GetDeferral(); if (byteOffset + sampleSize <= stream.Size) { Debug.WriteLine(sampleSize + " " + timeOffset); var sample = await MediaStreamSample.CreateFromStreamAsync(stream.GetInputStreamAt(byteOffset), sampleSize, timeOffset); //每一段的数据大小 sample.Duration = sampleDuration; //每一段进度条移动距离 sample.KeyFrame = true; e.Request.Sample = sample; byteOffset += sampleSize; timeOffset = timeOffset.Add(sampleDuration); } deferal.Complete(); }; return(c); }