Пример #1
0
        private async void Open_Click(object sender, RoutedEventArgs e)
        {
            var ofn = new OpenFileDialog {Filter = CodecFactory.SupportedFilesFilterEn};
            if (ofn.ShowDialog() == true)
            {
                _soundOut.Stop();
                if (_notificationSource != null)
                    _notificationSource.Dispose();

                var source = CodecFactory.Instance.GetCodec(ofn.FileName);
                
                //since the mediafoundationdecoder isn't really accurate what position and length concerns
                //read the whole file into a cache
                if (source is MediaFoundationDecoder)
                {
                    if (source.Length < 10485760) //10MB
                    {
                        source = new CachedSoundSource(source);
                    }
                    else
                    {
                        Stopwatch stopwatch = Stopwatch.StartNew();
                        source = new FileCachedSoundSource(source);
                        stopwatch.Stop();
                        Debug.WriteLine(stopwatch.Elapsed.ToString());
                    }
                }
                source.Position = 0;
                //load the waveform
                await LoadWaveformsAsync(source);
                source.Position = 0;

                _sampleSource = source.ToSampleSource();
                _notificationSource = new NotificationSource(_sampleSource) {Interval = 100};
                _notificationSource.BlockRead += (o, args) => { UpdatePosition(); };
                _soundOut.Initialize(_notificationSource.ToWaveSource());
                _soundOut.Play();
            }
        }
Пример #2
0
        public void ThrowsInvalidCallerThread()
        {
            Exception exception = null;
            using (var source = new NotificationSource(GetLoopingWaveSource().ToSampleSource()))
            {
                using (var waitHandle = new ManualResetEvent(false))
                {
                    _soundOut.Initialize(source.ToWaveSource());
                    //the play method sometimes won't cause an InvalidOperationException
                    //for example the waveout class will read from the source which fires up the BlockRead event BUT not on the playbackthread.
                    _soundOut.Play();
                    source.BlockRead += (s, e) =>
                    {
                        try
                        {
                            _soundOut.Pause(); //hopefully throws InvalidOperationException
                        }
                        catch (InvalidOperationException ex)
                        {
                            exception = ex;
                        }
                        finally
                        {
            // ReSharper disable once AccessToDisposedClosure
                            waitHandle.Set();
                        }
                    };
                    waitHandle.WaitOne();

                    _soundOut.Stop();
                }
            }
            if (exception != null)
                throw exception;
        }