简体   繁体   中英

Creating audio player on WPF using NAudio. Binding doesn't work

I am creating my audio player on WPF using NAudio .
I am adding a slider that will be scroll the song.
XAML:

<Slider Height="30" 
                Value="{Binding Path=MediaReader.Position, Mode=TwoWay}" 
                Maximum="{Binding Path=MediaReader.Length, Mode=OneWay}"/>

Note . MediaReader - it's a property that returns the object of type MediaFoundationReader :

MediaFoundationReader mediaReader;
public MediaFoundationReader MediaReader => mediaReader;

Problem : while the song is plaing the slider property Value doesn't change! But by scrolling the thumb of the slider the property Position of the MediaReader changes.
Why does it work so and how can I solve that?

Just take a look to this tutorial:

https://www.pluralsight.com/guides/building-a-wpf-media-player-using-naudio

The author uses also a slider control and binds the current track position, eg:

<Slider Grid.Column="0" Minimum="0" Maximum="{Binding CurrentTrackLenght, Mode=OneWay}" Value="{Binding CurrentTrackPosition, Mode=TwoWay}" x:Name="SeekbarControl" VerticalAlignment="Center">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="PreviewMouseDown">
                        <i:InvokeCommandAction Command="{Binding TrackControlMouseDownCommand}"></i:InvokeCommandAction>
                    </i:EventTrigger>
                    <i:EventTrigger EventName="PreviewMouseUp">
                        <i:InvokeCommandAction Command="{Binding TrackControlMouseUpCommand}"></i:InvokeCommandAction>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Slider>

public double CurrentTrackPosition
    {
        get { return _currentTrackPosition; }
        set
        {
            if (value.Equals(_currentTrackPosition)) return;
            _currentTrackPosition = value;
            OnPropertyChanged(nameof(CurrentTrackPosition));
        }
    }

I think you can get the idea behind this...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM