async void MSS_Starting(Windows.Media.Core.MediaStreamSource sender, MediaStreamSourceStartingEventArgs args) { MediaStreamSourceStartingRequest request = args.Request; if ((request.StartPosition != null) && request.StartPosition.Value <= MSS.Duration) { UInt64 sampleOffset = (UInt64)request.StartPosition.Value.Ticks / (UInt64)sampleDuration.Ticks; timeOffset = new TimeSpan((long)sampleOffset * sampleDuration.Ticks); byteOffset = sampleOffset * sampleSize; } // create the RandomAccessStream for the input file for the first time if (mssStream == null) { MediaStreamSourceStartingRequestDeferral deferal = request.GetDeferral(); try { mssStream = await inputMP3File.OpenAsync(FileAccessMode.Read); request.SetActualStartPosition(timeOffset); deferal.Complete(); } catch (Exception) { MSS.NotifyError(MediaStreamSourceErrorStatus.FailedToOpenFile); deferal.Complete(); } } else { request.SetActualStartPosition(timeOffset); } }
void mkvStreamSource_Starting(MediaStreamSource sender, MediaStreamSourceStartingEventArgs args) { MediaStreamSourceStartingRequest request = args.Request; if (request.StartPosition.HasValue && sender.CanSeek) { var deferal = request.GetDeferral(); var mkvFs = mediaStreamFileSource as CCPlayer.HWCodecs.Matroska.MKV.MKVFileSource; if (mkvFs != null) { var newTime = mkvFs.Seek((ulong)request.StartPosition.Value.Ticks / 10000); request.SetActualStartPosition(newTime); } deferal.Complete(); } }
private void flvStreamSource_Starting(MediaStreamSource sender, MediaStreamSourceStartingEventArgs args) { MediaStreamSourceStartingRequest request = args.Request; if (request.StartPosition.HasValue && sender.CanSeek) { var deferal = request.GetDeferral(); var flvFile = mediaStreamFileSource as FlvFile; if (flvFile != null) { var newTime = SeekForFlv(flvFile, (long)(request.StartPosition.Value.TotalMilliseconds * 10000)); request.SetActualStartPosition(TimeSpan.FromTicks(newTime)); } deferal.Complete(); } }
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); }