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); } }
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) { // As we are still playing we need to stop MSS from requesting for more samples otherwise we'll crash if (_mss != null) { _mss.NotifyError(MediaStreamSourceErrorStatus.Other); mediaPlayer.Stop(); } }
private async void MediaStreamSource_SampleRequested(Windows.Media.Core.MediaStreamSource sender, Windows.Media.Core.MediaStreamSourceSampleRequestedEventArgs args) { var request = args.Request; if (!IsInternetConnected() || !connected) { connected = false; Disconnect(); sender.NotifyError(MediaStreamSourceErrorStatus.ConnectionToServerLost); return; } var deferral = request.GetDeferral(); try { MediaStreamSample sample = null; uint sampleLength = 0; //request.ReportSampleProgress(25); //if metadataPos is less than mp3_sampleSize away from metadataInt if (metadataInt - metadataPos <= (AudioInfo.AudioFormat == StreamAudioFormat.MP3 ? mp3_sampleSize : aac_adts_sampleSize) && metadataInt - metadataPos > 0) { //parse part of the frame. byte[] partialFrame = new byte[metadataInt - metadataPos]; var read = await socketReader.LoadAsync(metadataInt - metadataPos); if (read == 0) { Disconnect(); MediaStreamSource.NotifyError(MediaStreamSourceErrorStatus.ConnectionToServerLost); return; } socketReader.ReadBytes(partialFrame); metadataPos += metadataInt - metadataPos; switch (AudioInfo.AudioFormat) { case StreamAudioFormat.MP3: { Tuple <MediaStreamSample, uint> result = await ParseMP3SampleAsync(partial : true, partialBytes : partialFrame); sample = result.Item1; sampleLength = result.Item2; } break; case StreamAudioFormat.AAC_ADTS: case StreamAudioFormat.AAC: { Tuple <MediaStreamSample, uint> result = await ParseAACSampleAsync(partial : true, partialBytes : partialFrame); sample = result.Item1; sampleLength = result.Item2; } break; } } else { await HandleMetadata(); //request.ReportSampleProgress(50); switch (AudioInfo.AudioFormat) { case StreamAudioFormat.MP3: { //mp3 Tuple <MediaStreamSample, uint> result = await ParseMP3SampleAsync(); sample = result.Item1; sampleLength = result.Item2; } break; case StreamAudioFormat.AAC_ADTS: case StreamAudioFormat.AAC: { Tuple <MediaStreamSample, uint> result = await ParseAACSampleAsync(); sample = result.Item1; sampleLength = result.Item2; } break; } try { if (sample == null || sampleLength == 0) //OLD bug: on RELEASE builds, sample.Buffer causes the app to die due to a possible .NET Native bug { MediaStreamSource.NotifyError(MediaStreamSourceErrorStatus.DecodeError); deferral.Complete(); return; } else { metadataPos += sampleLength; } } catch (Exception) { } } if (sample != null) { request.Sample = sample; } //request.ReportSampleProgress(100); } catch (Exception) { MediaStreamSource.NotifyError(MediaStreamSourceErrorStatus.Other); } deferral.Complete(); }