private void SetupAudioVisualizer()
        {
            _audioVisualizer = new AudioVisualizer(Window.AudioVisualizerView);
            _timerWaveform = new System.Timers.Timer(50);
            _timerWaveform.Elapsed += (object sender, System.Timers.ElapsedEventArgs e) =>
            {
                InvokeOnMainThread(() =>
                    {
                        _timerWaveform.Stop();

                        System.Threading.Thread.Sleep(100);
                        if (_videoFileName != null && _audioVisualizer != null)
                        {
                            int index = -1;
                            var selectedIndices = Window.SubtitleTable.SelectedRows.ToList();
                            if (selectedIndices.Count > 0)
                                index = (int)selectedIndices.First();

                            if (_audioVisualizer.Locked)
                            {
                                double startPos = _videoPlayer.Position - ((_audioVisualizer.EndPositionSeconds - _audioVisualizer.StartPositionSeconds) / 2.0);
                                if (startPos < 0)
                                    startPos = 0;
                                SetWaveformPosition(startPos, _videoPlayer.Position, index);
                            }
//                                else if (_videoPlayer.Position > _audioVisualizer.EndPositionSeconds || _videoPlayer.Position < _audioVisualizer.StartPositionSeconds)
//                                {
//                                    double startPos = _videoPlayer.Position - 0.01;
//                                    if (startPos < 0)
//                                        startPos = 0;
//                                    _audioVisualizer.ClearSelection();
//                                    SetWaveformPosition(startPos, _videoPlayer.Position, index);
//                                }
                                else
                            {
                                SetWaveformPosition(_videoPlayer.Position, index);
                            }
                            _audioVisualizer.AudioVisualizerPaint();
                        }
                        _timerWaveform.Start();
                    });
            };
            _audioVisualizer.OnTimeChanged += (object sender, AudioVisualizerBase.ParagraphEventArgs e) =>
            {
                InvokeOnMainThread(() =>
                    {
                        ReloadDataKeepSelection();
                    });
            };

            _audioVisualizer.OnPositionSelected += (object sender, AudioVisualizerBase.ParagraphEventArgs e) =>
            {
                if (string.IsNullOrEmpty(_videoFileName) || e.Paragraph == null)
                {
                    return;
                }
                int index = _subtitle.GetIndex(e.Paragraph);
                if (index < 0)
                {
                    return;
                }
                InvokeOnMainThread(() =>
                    {
                        ShowSubtitleRow(index);
                        _videoPlayer.Position = _selectedParagraph.StartTime.TotalSeconds + 0.1;
                    });                    
            };

            _audioVisualizer.OnDoubleClickNonParagraph += (object sender, AudioVisualizerBase.ParagraphEventArgs e) =>
            {
                if (string.IsNullOrEmpty(_videoFileName))
                {
                    return;
                }
                int index = _subtitle.GetIndex(e.Seconds);
                InvokeOnMainThread(() =>
                    {
                        if (index >= 0)
                        {
                                
                            ShowSubtitleRow(index);
                        }
                        _videoPlayer.Position = e.Seconds;
                    });     
            };
        }
 public void NewSubtitle()
 {
     if (Window.SubtitleTable.DataSource == null || ContinueIfChanged())
     {
         _subtitleFileName = null;
         SetNewSubtitle(new Subtitle());
         Window.SetEncoding(Encoding.UTF8.BodyName);
         Window.SubtitleText.StringValue = string.Empty;
         Window.SetTimeCode(new Paragraph());
         if (_videoPlayer != null)
         {
             _videoPlayer.DisposeVideoPlayer();
         }
         if (_audioVisualizer != null)
         {
             _audioVisualizer.WavePeaks = null;
         }
         _videoFileName = null;
         _audioVisualizer = null;
     }
 }