MediaElement AutoPlay =“False”错误

时间:2012-01-11 20:25:32

标签: c# silverlight windows-phone-7.1 windows-phone-7

我正在从XML解析多个音频文件uri并将它们绑定到MediaElement的Source属性。我有八个按钮可以触发音频文件。当AutoPlay属性设置为True时,一切都很好。每个按钮都会触发正确的音频文件。但是因为我不希望音频在页面加载后立即开始播放我将自动播放设置为False。现在什么都不行。页面加载但按钮不会触发音频文件。我该如何解决这个错误?

代码:

public partial class MainPage : PhoneApplicationPage
{
    string name = "C";

    public MainPage()
    {
        InitializeComponent();
    }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        SetPlayerSource();
        base.OnNavigatedTo(e);
    }

    private void SetPlayerSource()
    {
        XDocument audioPlayer = XDocument.Load("Audio.xml");

        var aani = (from audio in audioPlayer.Descendants("Note")
                    where audio.Attribute("id").Value == name
                    select new AudioClass
                    {
                        Audio = (string)audio.Element("url").Value

                    }).SingleOrDefault();

        player.Source = new Uri(aani.Audio, UriKind.RelativeOrAbsolute);
    }


    private void C_Key_Click(object sender, RoutedEventArgs e)
    {
        var buttonName = (sender as Button).Name;
        var underscorePos = buttonName.IndexOf('_');
        name = buttonName.Substring(0, underscorePos);
        SetPlayerSource();
        player.Play();

    }

    private void D_Key_Click(object sender, RoutedEventArgs e)
    {
        var buttonName = (sender as Button).Name;
        var underscorePos = buttonName.IndexOf('_');
        name = buttonName.Substring(0, underscorePos);
        SetPlayerSource();
        player.Play();
    }

XAML:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="41,-8,-17,8">
        <Button x:Name="C_key" Content="" HorizontalAlignment="Left" Height="220" Margin="8,0,0,8" Style="{StaticResource C}" VerticalAlignment="Bottom" Width="75" Click="C_Key_Click"/>
        <Button x:Name="D_key" Content="" HorizontalAlignment="Left" Height="220" Margin="87,0,0,8" Style="{StaticResource D}" VerticalAlignment="Bottom" Width="75" Click="D_Key_Click"/>
        <Button x:Name="E_key" Content="" HorizontalAlignment="Left" Height="220" Margin="166,0,0,8" Style="{StaticResource E}" VerticalAlignment="Bottom" Width="75" Click="E_Key_Click"/>
        <Button x:Name="F_key" Content="" HorizontalAlignment="Left" Height="220" Margin="245,0,0,8" Style="{StaticResource F}" VerticalAlignment="Bottom" Width="75" d:LayoutOverrides="Width" Click="F_Key_Click"/>
        <Button x:Name="G_key" Content="" Height="220" Margin="324,0,305,8" Style="{StaticResource G}" VerticalAlignment="Bottom" Click="G_Key_Click"/>
        <Button x:Name="A_key" Content="" HorizontalAlignment="Right" Height="220" Margin="0,0,226,8" Style="{StaticResource A}" VerticalAlignment="Bottom" Width="75" Click="A_Key_Click"/>
        <Button x:Name="B_key" Content="" HorizontalAlignment="Right" Height="220" Margin="0,0,147,8" Style="{StaticResource B}" VerticalAlignment="Bottom" Width="75" Click="B_Key_Click"/>
        <Button x:Name="C2_key" Content="" HorizontalAlignment="Right" Height="220" Margin="0,0,68,8" Style="{StaticResource C2}" VerticalAlignment="Bottom" Width="75" Click="C2_Key_Click"/>
        <MediaElement Height="120" HorizontalAlignment="Left" Margin="8,6,0,0" Name="player" VerticalAlignment="Top" Width="160" Source="{Binding Audio}" Volume="1" AutoPlay="False"/>
     </Grid>

2 个答案:

答案 0 :(得分:6)

根据MSDN,您应该在设置AutoPlay属性之前将false设置为Source

Source中设置SetPlayerSource时,可以从XAML中删除设置器。除此之外,代码看起来还不错。

但是,我刚刚意识到您在致电Play后正在呼叫SetPlayerSource。在尝试播放媒体之前,这不会让您的代码有时间实际加载媒体。

您需要在Play活动上致电MediaOpened

答案 1 :(得分:3)

首先,将Name=player中的MediaElement替换为x:Name=player。这有时会引起其他控制问题。

如果这不起作用,您可能会尝试在音频加载完毕之前调用Play方法。相反,请尝试处理MediaOpened事件并在那里播放文件。

private void player_MediaOpened(object sender, RoutedEventArgs e)
{
     player.Play();
}