Windows Phone 8 audioplayer例外

时间:2013-06-27 06:29:05

标签: windows-phone-7 windows-phone-8 windows-phone

我在System.Windows.ni.dll中遇到System.Windows.Markup.XamlParseException异常, System.ArgumentException发生在mscorlib.ni.dll中,未在托管/本地边界

之前处理

我的MainPage.xaml

<Slider Name="timelineSlider" Margin="0,185,0,376" ValueChanged="SeekToMediaPosition"/>
<TextBlock Height="51" HorizontalAlignment="Left" Margin="27,334,0,0" x:Name="TitleTextBlock" Text="Title:" VerticalAlignment="Top" FontSize="28" Foreground="{StaticResource PhoneAccentBrush}" Width="400" />

<TextBlock Height="39" HorizontalAlignment="Left" Margin="27,383,0,0" x:Name="ArtistTextBlock" Text="Artist:" VerticalAlignment="Top" FontSize="24" FontStyle="Italic" Foreground="White" Width="400" />

<TextBlock Height="30" HorizontalAlignment="Left" Margin="37,559,0,0" x:Name="StartTextBlock" Text="00:00:00" VerticalAlignment="Top" Width="100" />
<TextBlock Height="30" HorizontalAlignment="Left" TextAlignment="Right" Margin="327,559,0,0" x:Name="EndTextBlock" Text="00:00:00" VerticalAlignment="Top" Width="88" />

<Slider HorizontalAlignment="Left" Margin="43,200,0,0" VerticalAlignment="Top" Width="395" Name="SongSlider" ManipulationCompleted="SoundSlider_ManipulationCompleted"/>

我的MainPage.xaml.cs文件是:

private void NotProvided()
{
    if (BackgroundAudioPlayer.Instance.Track != null)
    {
        // show soung info
        TitleTextBlock.Text = BackgroundAudioPlayer.Instance.Track.Title
        ArtistTextBlock.Text = BackgroundAudioPlayer.Instance.Track.Artist;
        // handle slider and texts
        SongSlider.Minimum = 0;
        SongSlider.Maximum = BackgroundAudioPlayer.Instance.Track.Duration.TotalMilliseconds;
        string text = BackgroundAudioPlayer.Instance.Track.Duration.ToString();
        EndTextBlock.Text = text.Substring(0, 8);

    }

    if (null != BackgroundAudioPlayer.Instance.Track)
    {
        txtCurrentTrack.Text = BackgroundAudioPlayer.Instance.Track.Title +
                                " by " +
                                BackgroundAudioPlayer.Instance.Track.Artist;

    }
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if (PlayState.Playing == BackgroundAudioPlayer.Instance.PlayerState)
    {
        playButton.Content = "pause";
        txtCurrentTrack.Text = BackgroundAudioPlayer.Instance.Track.Title +
        " by " +
        BackgroundAudioPlayer.Instance.Track.Artist;

    }
    else
    {
        playButton.Content = "play";
        txtCurrentTrack.Text = "";
    }
}

private DispatcherTimer dispatcherTimer = new DispatcherTimer();

private void startTimer()
{
    // start timer to check position of the song
    dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 500);
    dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
    dispatcherTimer.Start();
}

private void dispatcherTimer_Tick(object sender, EventArgs e)
{
    // song is playing
    if (PlayState.Playing == BackgroundAudioPlayer.Instance.PlayerState)
    {
        // handle slider
        SongSlider.Minimum = 0;
        SongSlider.Value = BackgroundAudioPlayer.Instance.Position.TotalMilliseconds;
        SongSlider.Maximum = BackgroundAudioPlayer.Instance.Track.Duration.TotalMilliseconds;
        // display text
        string text = BackgroundAudioPlayer.Instance.Position.ToString();
        StartTextBlock.Text = text.Substring(0, 8);
        text = BackgroundAudioPlayer.Instance.Track.Duration.ToString();
        EndTextBlock.Text = text.Substring(0, 8);
    }
}

private void SoundSlider_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
    // get slider value
    int sliderValue = (int)SongSlider.Value;
    // create timespan object with milliseconds (from slider value)
    TimeSpan timeSpan = new TimeSpan(0, 0, 0, 0, sliderValue);
    // set a new position of the song
    BackgroundAudioPlayer.Instance.Position = timeSpan;
}

private void PrevButton_Click(object sender, RoutedEventArgs e)
{
    // skip to previous song
    BackgroundAudioPlayer.Instance.SkipPrevious();
    // handle text and slider
    playButton.Content = "Pause";
    SongSlider.Value = 0;
}

private void NextButton_Click(object sender, RoutedEventArgs e)
{
    // skip to next song
    BackgroundAudioPlayer.Instance.SkipNext();
    // handle text and slider
    playButton.Content = "Pause";
    SongSlider.Value = 0;
}

0 个答案:

没有答案