带有 MediaElement Windows Phone 8.1 的滑块进度条

本文关键字:MediaElement Windows Phone 带有 | 更新日期: 2023-09-27 17:56:54

我在Windows Phone 8.1(非SilverLight)应用程序中工作。我有一个播放音频文件的MediaElement AudioPlayer。我还有一个滑块AudioPlayerSeek,我想"挂钩"到MediaElement,以便它在播放音频时移动。

我已经逐字逐句地浏览了本教程,但仍然无法使其正常工作。当我播放音频(单击 ListView 中的项目)时,音频会播放,但滑块不会移动!

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 Windows Phone 8.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();
}

并将这些调用移动到 MediaOpen 处理程序:

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