我在页面上有一个滑块,显示音频的播放进度。然而,它并没有动。此外,它不能通过拖动移动,它始终保持在0.我使用滑块的PointerPressed和PointerReleased事件来做到这一点。
奇怪的是,如果我在slider上放置一个断点.Maximum = mediaPlayer.PlaybackSession.NaturalDuration.TotalSeconds;在PlayMedia方法中,在点击并恢复后,滑块工作正常!!!
以下是一些相关代码。
<Slider x:Name="slider" Width="Auto" Height="30" Margin="10,0,10,0" VerticalAlignment="Center" />
public sealed partial class MoreRuqya : Page
{
private bool bMediaStop = false;
private MediaPlayer mediaPlayer = new MediaPlayer();
private int playIndex = 0; //media index
private DispatcherTimer timer = new DispatcherTimer();
private DownloadFile _download = new DownloadFile();
private void Page_Loaded(object sender, RoutedEventArgs e)
{
if (App.lstMoreRuqya == null)
{
App.lstMoreRuqya = DataLoader.LoadMoreRuqya();
}
var result = from rq in App.lstMoreRuqya group rq by rq.Category into grp orderby grp.Key select grp;
cvsMoreRuqya.Source = result;
mediaPlayer.MediaEnded += MediaPlayer_MediaEnded;
mediaPlayer.BufferingStarted += MediaPlayer_BufferingStarted;
mediaPlayer.BufferingEnded += MediaPlayer_BufferingEnded;
//*************needed for PointerPressed & PointerReleased events to trigger**********************
slider.AddHandler(PointerPressedEvent, new PointerEventHandler(Slider_PointerPressed), true);
slider.AddHandler(PointerReleasedEvent, new PointerEventHandler(slider_PointerReleased), true);
//************************************************************************************************
_download.Status += _download_Status;
if (mediaPlayer.PlaybackSession.PlaybackState == MediaPlaybackState.Playing)
{
Play.Symbol = Symbol.Pause;
}
else
{
Play.Symbol = Symbol.Play;
}
InitializeTimer();
}
private void MediaPlayer_BufferingStarted(MediaPlayer sender, object args)
{
lblMediaInfo.Text = "Buffering...";
}
private void MediaPlayer_BufferingEnded(MediaPlayer sender, object args)
{
slider.Maximum = mediaPlayer.PlaybackSession.NaturalDuration.TotalSeconds;
}
private void MediaPlayer_MediaEnded(MediaPlayer sender, object args)
{
PlayNext();
}
private void btnPlay_Click(object sender, RoutedEventArgs e)
{
//filter list for only selected items
var selected = from rq in App.lstMoreRuqya where rq.isSelected == true select rq;
App.lstSelectedMoreRuqya = selected.ToList();
if (Play.Symbol.Equals(Symbol.Play))
{
if (mediaPlayer.PlaybackSession.PlaybackState == MediaPlaybackState.Paused & !bMediaStop)
{
mediaPlayer.Play();
}
else
{
bMediaStop = false;
PlayMedia(playIndex);
}
Play.Symbol = Symbol.Pause;
timer.Start();
}
else if (Play.Symbol.Equals(Symbol.Pause))
{
mediaPlayer.Pause();
Play.Symbol = Symbol.Play;
timer.Stop();
}
}
private void PlayMedia(int index)
{
//if past last item in playlist & last item has been repeated
if (index > App.lstSelectedMoreRuqya.Count || App.lstSelectedMoreRuqya.Count == 0)
{
Play.Symbol = Symbol.Play;
return;
}
string uri = App.lstSelectedMoreRuqya[playIndex].urlPath;
mediaPlayer.Source = MediaSource.CreateFromUri(new Uri(uri));
mediaPlayer.AutoPlay = true;
slider.Maximum = mediaPlayer.PlaybackSession.NaturalDuration.TotalSeconds;
}
private void timer_Tick(object sender, object e)
{
if (mediaPlayer.PlaybackSession.PlaybackState == MediaPlaybackState.None)
{
lblMediaInfo.Text = "";
}
if (mediaPlayer.PlaybackSession.PlaybackState == MediaPlaybackState.Playing)
{
slider.Value = Convert.ToDouble(mediaPlayer.PlaybackSession.Position.Seconds);
lblMediaInfo.Text = App.lstSelectedMoreRuqya[playIndex].RuqyaName + " (" + FormatDuration(mediaPlayer.PlaybackSession.Position) + "/" + FormatDuration(mediaPlayer.PlaybackSession.NaturalDuration) + ")";
}
}
private void InitializeTimer()
{
timer.Interval = TimeSpan.FromMilliseconds(1000);
timer.Tick += timer_Tick;
timer.Start();
}
private void Slider_PointerPressed(object sender, PointerRoutedEventArgs e)
{
timer.Stop();
}
private void slider_PointerReleased(object sender, PointerRoutedEventArgs e)
{
mediaPlayer.PlaybackSession.Position = TimeSpan.FromSeconds(slider.Value);
timer.Start();
}
}
答案 0 :(得分:2)
此外,它无法通过拖动移动,它始终保持为0
由以下代码行
引起slider.Maximum = mediaPlayer.PlaybackSession.NaturalDuration.TotalSeconds;
将滑块的最大值设置为0,这样就不能将滑块的值设置为大于0.您可以添加System.Diagnostics.Debug.WriteLine(slider.Maximum);
来测试它。 NaturalDuration.TotalSeconds
获得0结果的原因应该是PlaybackSession
的MediaPlayer
未完成打开。
解决此问题的更好方法是在源OpenOperationCompleted
之后获得MediaSource
的持续时间。例如:
private void PlayMedia(int index)
{
MediaSource mediasource = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/test.mp3"));
mediasource.OpenOperationCompleted += Mediasource_OpenOperationCompleted;
mediaPlayer.Source = mediasource;
mediaPlayer.AutoPlay = true;
//slider.Maximum = mediaPlayer.PlaybackSession.NaturalDuration.Seconds;
//System.Diagnostics.Debug.WriteLine(slider.Maximum);
}
private async void Mediasource_OpenOperationCompleted(MediaSource sender, MediaSourceOpenOperationCompletedEventArgs args)
{
var _duration = sender.Duration.GetValueOrDefault();
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
slider.Minimum = 0;
slider.Maximum = _duration.TotalSeconds;
slider.StepFrequency = 1;
});
}
更多详情请参阅Use MediaTimelineController to synchronize content across multiple players。此部分包含滑块示例。