使用MediaElement Windows Phone 8.1的滑块进度条

时间:2015-06-18 16:54:36

标签: c# xaml slider windows-phone-8.1 mediaelement

我在Windows Phone 8.1(非SilverLight)应用程序中工作。我有一个播放音频文件的MediaElement AudioPlayerSeek。我还有一个滑块private DispatcherTimer _timer; private bool _sliderpressed = false; // Constructor public MainPage() { InitializeComponent(); NavigationCacheMode = NavigationCacheMode.Required; AudioPlayerSeek.ValueChanged += AudioPlayerSeek_ValueChanged; InitializeAudioRecording(); DataContext = App.ViewModel; } // Called when I select my Audio file in the ListView private async void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e) { // Cast to XAML ListView var listView = sender as ListView; // Verify we have ListView if (listView == null) { return; } var listVmItem = listView.SelectedItem as RecordFileVm; if (listVmItem != null) { var file2 = await _finalStorageFolder.GetFileAsync(listVmItem.FileName); var stream = (await file2.OpenReadAsync()).AsStream().AsRandomAccessStream(); AudioPlayer.SetSource(stream, file2.ContentType); TimeSpan recordingTime = AudioPlayer.NaturalDuration.TimeSpan; AudioPlayerSeek.Maximum = recordingTime.TotalSeconds; AudioPlayerSeek.SmallChange = 1; AudioPlayerSeek.LargeChange = Math.Min(10, recordingTime.Seconds / 10); AudioPlayer.MediaOpened += AudioPlayer_MediaOpened; AudioPlayer.CurrentStateChanged += AudioPlayer_CurrentStateChanged; AudioPlayer.Play(); } // Clear selection listView.SelectedItem = null; } private double SliderFrequency(TimeSpan timevalue) { double stepfrequency = -1; double absvalue = (int)Math.Round(timevalue.TotalSeconds, MidpointRounding.AwayFromZero); stepfrequency = (int)(Math.Round(absvalue / 100)); if (timevalue.TotalMinutes >= 10 && timevalue.TotalMinutes < 30) { stepfrequency = 10; } else if (timevalue.TotalMinutes >= 30 && timevalue.TotalMinutes < 60) { stepfrequency = 30; } else if (timevalue.TotalHours >= 1) { stepfrequency = 60; } if (stepfrequency == 0) stepfrequency += 1; if (stepfrequency == 1) { stepfrequency = absvalue / 100; } return stepfrequency; } void AudioPlayerSeek_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) { if (!_sliderpressed) { AudioPlayer.Position = TimeSpan.FromSeconds(e.NewValue); } } private void SetupTimer() { _timer = new DispatcherTimer(); _timer.Interval = TimeSpan.FromSeconds(AudioPlayerSeek.StepFrequency); StartTimer(); } private void _timer_Tick(object sender, object e) { if (!_sliderpressed) { AudioPlayerSeek.Value = AudioPlayer.Position.TotalSeconds; } } private void StartTimer() { _timer.Tick += _timer_Tick; _timer.Start(); } ,我希望“挂钩”到MediaElement,以便它在播放音频时移动。

我逐字逐句地了解了this tutorial,但仍无法让它发挥作用。当我播放音频(点击ListView中的某个项目)时,播放音频,但滑块不会移动!

myHolder.sb_songProgress.setProgress((int) timeElapsed);

我相信这是相关的代码。有人可以告诉我如何同步滑块以使其随播放媒体(音频)一起移动。

谢谢。

1 个答案:

答案 0 :(得分:1)

您正在以错误的顺序进行设置。尝试将其更改为:

 if (listVmItem != null)
{
    var file2 = await _finalStorageFolder.GetFileAsync(listVmItem.FileName);
    var stream = (await file2.OpenReadAsync()).AsStream().AsRandomAccessStream();
    AudioPlayer.MediaOpened += AudioPlayer_MediaOpened;
    AudioPlayer.CurrentStateChanged += AudioPlayer_CurrentStateChanged;
    AudioPlayer.SetSource(stream, file2.ContentType);
    AudioPlayer.Play();
}

并将这些调用移至MediaOpened处理程序:

    TimeSpan recordingTime = AudioPlayer.NaturalDuration.TimeSpan;
    AudioPlayerSeek.Maximum = recordingTime.TotalSeconds;
    AudioPlayerSeek.SmallChange = 1;
    AudioPlayerSeek.LargeChange = Math.Min(10, recordingTime.Seconds / 10);